Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DI] Add an url EnvProcessor#28975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedOct 25, 2018
Before reading the code, I was expecting this to be a processor to do a |
javiereguiluz commentedOct 25, 2018
@jderusse even I appreciate your contribution, I'm going to share an (unpopular?) opinion about this "env processor" trend. I think it's a mistake and makes the YAML config files even harder to understand. I know we need to support something like this ... but I prefer if we focused instead on switching to PHP config by default. Beforemongo_db_bundle:clients:default:hosts: -{ host: '%env(key:host:url:MONGODB_DSN)%', port: '%env(key:port:url:MONGODB_DSN)%' }username:'%env(key:user:url:MONGODB_DSN)%'password:'%env(key:pass:url:MONGODB_DSN)%'connections:default:database_name:'%env(key:path:url:MONGODB_DSN)%' After$mongo =parse_url(env('MONGODB_DSN'));return ['mongo_db_bundle' => ['clients' => ['default' => ['hosts' => ['host' =>$mongo['host'],'port' =>$mongo['port'], ],'username' =>$mongo['user'] ??null,'password' =>$mongo['pass'] ??null, ] ],'connections' => ['default' => ['database_name' =>$mongo['path'], ] ]]; |
ro0NL commentedOct 25, 2018
fundamentally the goal of %env()% is to not fetch envs during compile time. But for PHP config a env builder might be nice: 'database_name' =>env('key:path','url','MONGODB_DSN'), to at least make the strings more readable. |
andersonamuller commentedOct 25, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@javiereguiluz That is a very popular opinion to me :) @ro0NL Isn't it possible to have something like the code below? // services.phpuseSymfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;returnfunction (ContainerConfigurator$configurator) {$parameters =$configurator->parameters();$defaultMongoDbUrl ='mongodb://localhost:27017/symfony?ssl=false';$parameters->set('database_name',env('MONGODB_DSN',$defaultMongoDbUrl)->url()->path() );$parameters->set('database_secure',env('MONGODB_DSN',$defaultMongoDbUrl)->url()->query('ssl',true/* default */) );// other examples$parameters->set('security_secret',/* without default value, it will throw exception if does not exist at runtime */env('APP_SECRET_FILE')->file()->trim() );$parameters->set('locale',env('APP_LOCALE','pt_BR'));/* defaut values are always a string */$parameters->set('account_threshold',env('APP_THRESHOLD','10')->int());}; also for the other PR#28976 |
ro0NL commentedOct 25, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@andersonamuller sure, whatever API that can build the string for us :) the question is; how much code do we want to add/maintain only to generate a string. |
jderusse commentedOct 25, 2018
@ro0NL I once opened a RFC for this#28084 @javiereguiluz of course we can do everything with php config file. Does Symfony want to promote use of yaml config file for simple case and php for complex one. This is not my call ;-) But in that case, what is the purpose of envProcessor like 'key', 'json', 'file'? |
jderusse commentedOct 25, 2018
@nicolas-grekas done. Took |
javiereguiluz commentedOct 25, 2018
Just to clarify: I think that YAML is fantastic for config files. The problem is when you try to add PHP-like features to YAML. If we continue doing that, it's better to switch to PHP. |
nicolas-grekas commentedOct 25, 2018
@javiereguiluz please don't hijack the topic,@ro0NL is right here: your proposal misses a lot of context to have it work - maybe we can make it work, but that's a huge task, that's what I mean. |
Uh oh!
There was an error while loading.Please reload this page.
javiereguiluz commentedNov 7, 2018
@jderusse your before/after example is perfect for Symfony Docs. Just asking about the missing parts:
|
jderusse commentedNov 7, 2018
@javiereguiluz This feature uses exactly the same "workflow" than You can use it without the framework like this: useSymfony\Component\DependencyInjection\ContainerBuilder;$containerBuilder =newContainerBuilder();$containerBuilder->setParameter('es_url','%env(url:ES_HOST)%');$containerBuilder->compile(true);var_dump($containerBuilder->getParameter('es_url')); ES_HOST='http://www.google.com' php index.phparray(8) { ["scheme"]=> string(4)"http" ["host"]=> string(14)"www.google.com" ["port"]=> int(0) ["user"]=> string(0)"" ["pass"]=> string(0)"" ["path"]=> string(0)"" ["query"]=> string(0)"" ["fragment"]=> string(0)""} |
javiereguiluz commentedNov 7, 2018
@jderusse thanks for the added examples. That's all we need for the docs. Cheers! |
nicolas-grekas commentedDec 1, 2018
(rebase needed) |
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedJan 27, 2019
Please rebase. |
fc01bbc to891f1d3CompareUh oh!
There was an error while loading.Please reload this page.
ea2546d tocfb5cbcCompareUh oh!
There was an error while loading.Please reload this page.
javiereguiluz commentedMar 13, 2019
Reading the updated description of this PR, I think this: could be easier to understand as follows:
|
ro0NL commentedMar 13, 2019
can we try both? query string obtained from URL and provided as-is. |
nicolas-grekas commentedMar 15, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@javiereguiluz I'm not sure that's possible without restricting possibilities a lot. Power comes from combining single predictable tools, and have each tool do one thing well. Having query_string be a hybrid between |
ro0NL commentedMar 15, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Here's an approach that might work:https://3v4l.org/hLlIg, i like the proposed convenience of |
nicolas-grekas commentedMar 17, 2019
jderusse commentedMar 17, 2019
Done |
| } | ||
| if ('query_string' ===$prefix) { | ||
| $queryString =parse_url($env,PHP_URL_QUERY) ??$env; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should use?: I think as parse_url can return false also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
If the component parameter is specified, parse_url() returns a string (or an integer, in the case of PHP_URL_PORT) instead of an array. If the requested component doesn't exist within the given URL, NULL will be returned.
🤞 but i didnt spot a case which returns false when a component is set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
var_export(parse_url(':', PHP_URL_QUERY)); displays false :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
😭
nicolas-grekas commentedMar 18, 2019
Thank you@jderusse. |
This PR was merged into the 4.3-dev branch.Discussion----------[DI] Add an url EnvProcessor| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | -| License | MIT| Doc PR |symfony/symfony-docs#11128This PR add a new env processor `url` to convert an URL (or DSN) into an array.The main goal is to simplify the project configuration and reduce the number of env variable when working with bundle which are not able to deal with DSN(pick some random project in symfony/recipes-contrib:https://github.com/symfony/recipes-contrib/blob/master/facile-it/mongodb-bundle/0.6/manifest.json orhttps://github.com/symfony/recipes-contrib/blob/master/wouterj/eloquent-bundle/1.0/manifest.json)```yaml# beforeMONGODB_HOST=localhostMONGODB_PORT=27017MONGODB_USER=MONGODB_PASSWORD=MONGODB_DB=symfonymongo_db_bundle: data_collection: '%kernel.debug%' clients: default: hosts: - { host: '%env(MONGODB_HOST)%', port: '%env(int:MONGODB_PORT)%' } username: '%env(MONGODB_USER)%' password: '%env(MONGODB_PASSWORD)%' connectTimeoutMS: 3000 connections: default: database_name: '%env(MONGODB_DB)%'# afterMONGODB_DSN=mongodb://localhost:27017/symfonymongo_db_bundle: data_collection: '%kernel.debug%' clients: default: hosts: - { host: '%env(key:host:url:MONGODB_DSN)%', port: '%env(key:port:url:MONGODB_DSN)%' } username: '%env(key:user:url:MONGODB_DSN)%' password: '%env(key:pass:url:MONGODB_DSN)%' connectTimeoutMS: 3000 connections: default: database_name: '%env(key:path:url:MONGODB_DSN)%'```Added also a `query_string` processor to parse query string```DATABASE_DSN=mysql://localhost/db?charset=utf8foo: bar: charset: '%env(key:charset:query_string:key:query:url:DATABASE_DSN)%'```Commits-------f253c9b Add an url EnvProcessor
This PR was merged into the master branch.Discussion----------Add `url` and `query_string` processorsDocumentation forsymfony/symfony#28975Commits-------4fb0d0a Add `url` and `query_string` processors
Uh oh!
There was an error while loading.Please reload this page.
This PR add a new env processor
urlto convert an URL (or DSN) into an array.The main goal is to simplify the project configuration and reduce the number of env variable when working with bundle which are not able to deal with DSN
(pick some random project in symfony/recipes-contrib:https://github.com/symfony/recipes-contrib/blob/master/facile-it/mongodb-bundle/0.6/manifest.json orhttps://github.com/symfony/recipes-contrib/blob/master/wouterj/eloquent-bundle/1.0/manifest.json)
Added also a
query_stringprocessor to parse query string