Expand .env values conversion ability of BaseConfig - #5704
Conversation
edd76a4 to
2ada1e4
Compare
|
I vote for php. |
| SimpleConfig.shortie = '12.45' | ||
| SimpleConfig.longie = '250' |
There was a problem hiding this comment.
I think '12.45' or "12.45" is string, 12.45 is float.
There was a problem hiding this comment.
So you mean drop the trimming of quotes? And regard encapsed numbers as intended to be strings?
There was a problem hiding this comment.
As a PHP user, I think '12.45' is a string, not an integer.
It is quoted intentionally.
I think adding the following two cases are enough.
SimpleConfig.shortie = 12.45 as float, and SimpleConfig.longie = 250 as integer.
There was a problem hiding this comment.
However, inside a .env file where everything is string, quotes really serve no purpose other than capturing in-between spaces. That's why in DotEnv::sanitizeValue() we capture what's inside those quotes.
CodeIgniter4/system/Config/DotEnv.php
Line 163 in 968c027
So, in the absence of a whitespace in between, 12.45 === '12.45'
And in the default env, you can write strings with or without quotes. It doesn't matter as long there is no whitespace.
database.tests.password = root
contentsecuritypolicy.styleSrc = 'self'
There was a problem hiding this comment.
I think this is an opportunity to standardize, and I like the ability to retain numerical strings. I can't think off the top of my head where that might be important (maybe with strongly typed Config properties?) but my hunch is we need to support it and checking for the presence of quotes is a decent approach.
There was a problem hiding this comment.
The current code does not matter.
12.45 === '12.45' is wired.
And there is no way to set numerical strings.
There was a problem hiding this comment.
First of all, all environment variable's values are strings in PHP.
And we don't have to use DotEnv, we can set environment variables to web server.
So the DotEnv code does not matter.
Now we are going to change the interpretation of the values. We already changed when true/false.
We can decide the behavior for us.
There was a problem hiding this comment.
DotEnv matters because in the current application life cycle, DotEnv sets the env variables and BaseConfig fetches them. Well, any experienced developer can set the env variables themselves but I don't think majority of CI4 users will do that. They just rely on the default implementation.
Okay, since both of you want now to distinguish between integer 12 and string 12 by way of encapsulating quotes, I need to change the behavior of DotEnv (which I'll do in a separate PR) and adapt the code here accordingly.
There was a problem hiding this comment.
Yes, integer 12 and string 12 are different.
Not being able to use numeric strings for properties in the Config files is too big a restriction.
I just wanted to say, the following two are different things. They are related and must work with, but not one.
- DotEnv reads
.envand sets environment variables in PHP - BaseConfig fetches environment variables and sets properties
|
@iRedds See #5688 (comment) |
|
The type cast must be inside. |
What do you mean? At least, I disagree with the current Paul's implementation, which both |
$this->propertyExpectsInt = (int) env('property'); |
MGatner
left a comment
There was a problem hiding this comment.
One other thing you may have accounted for but just to be sure, from https://www.php.net/manual/en/function.is-numeric :
8.0.0 Numeric strings ending with whitespace ("42 ") will now return true. Previously, false was return instead
The current and proposed behavior do not remove edge whitespaces, only edge quotes. So, your example will have different values on php 8. |
The CodeIgniter4/system/Config/BaseConfig.php Line 72 in 968c027 CodeIgniter4/system/Config/BaseConfig.php Line 93 in 968c027 |
| @@ -1,4 +1,3 @@ | |||
| SimpleConfig.QZERO=0 | |||
| SimpleConfig.QZEROSTR="0" | |||
|
Please add test cases like these: |
|
@paulbalandan for database use case, is this will still works for password db: 123456 without quote? database.default.password = 123456 |
|
#5779 was merged. |
Description
Initial (current):
Additional (proposed):
Supersedes and closes #5691
Fixes #5688
Checklist: