Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
nicolas-grekas merged 1 commit intosymfony:masterfromjderusse:env-url
Mar 18, 2019
Merged

Conversation

@jderusse
Copy link
Member

@jderussejderusse commentedOct 25, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Doc PRsymfony/symfony-docs#11128

This PR add a new env processorurl 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)

# 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:3000connections: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:3000connections:default:database_name:'%env(key:path:url:MONGODB_DSN)%'

Added also aquery_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)%'

ro0NL, apfelbox, sstok, Koc, hylke94, dkarlovi, bigfoot90, emr, and noniagriconomie reacted with hooray emoji
@nicolas-grekas
Copy link
Member

Before reading the code, I was expecting this to be a processor to do aparse_str().
Now that I read it I see it's forparse_url().
I think we should ship both in this PR because they are complementary - that will force us to find the least confusing names for each :)

@nicolas-grekasnicolas-grekas added this to thenext milestoneOct 25, 2018
@javiereguiluz
Copy link
Member

@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.

Before

mongo_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'],        ]    ]];
damienalexandre reacted with thumbs up emojisstok reacted with laugh emoji

@ro0NL
Copy link
Contributor

parse_url(env('MONGODB_DSN'));

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.

sstok reacted with thumbs up emoji

@andersonamuller
Copy link
Contributor

andersonamuller commentedOct 25, 2018
edited
Loading

@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
Copy link
Contributor

ro0NL commentedOct 25, 2018
edited
Loading

@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
Copy link
MemberAuthor

@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
Copy link
MemberAuthor

@nicolas-grekas done. Tookurl andquery.

@javiereguiluz
Copy link
Member

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.

sstok reacted with hooray emoji

@nicolas-grekas
Copy link
Member

@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.

@javiereguiluz
Copy link
Member

@jderusse your before/after example is perfect for Symfony Docs. Just asking about the missing parts:

  • Would this feature require to enable/configure anything when using it in a Symfony full-stack app? (I guess it won't)
  • Would this require some changes when using it as a stand-alone component? I guess it does ... please show some example of how to activate this. Thanks!

@jderusse
Copy link
MemberAuthor

@javiereguiluz This feature uses exactly the same "workflow" thankey,json,int,bool, orfile processor. It don't use new Class or thing to instantiate.

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
Copy link
Member

@jderusse thanks for the added examples. That's all we need for the docs. Cheers!

@nicolas-grekas
Copy link
Member

(rebase needed)
@symfony/deciders OK for you?

@nicolas-grekas
Copy link
Member

Please rebase.

@jderussejderusseforce-pushed theenv-url branch 2 times, most recently fromfc01bbc to891f1d3CompareJanuary 27, 2019 18:00
@jderussejderusseforce-pushed theenv-url branch 3 times, most recently fromea2546d tocfb5cbcCompareMarch 9, 2019 18:31
@jderussejderusse changed the titleAdd an url EnvProcessor[DI] Add an url EnvProcessorMar 10, 2019
@javiereguiluz
Copy link
Member

Reading the updated description of this PR, I think this:

charset: '%env(key:charset:query_string:key:query:url:DATABASE_DSN)%'

could be easier to understand as follows:

charset: '%env(key:charset:query_string:DATABASE_DSN)%'

query_string would extract the query string from the given URL and explode it into a PHP array.

ro0NL reacted with heart emoji

@ro0NL
Copy link
Contributor

can we try both? query string obtained from URL and provided as-is.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 15, 2019
edited
Loading

@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 betweenparse_url andparse_str means not being able to use it only as aparse_str unit. 👎 to your proposal for this reason on my side.

@ro0NL
Copy link
Contributor

ro0NL commentedMar 15, 2019
edited
Loading

Here's an approach that might work:https://3v4l.org/hLlIg, i like the proposed convenience ofkey:charset:query_string:VAR

@nicolas-grekas
Copy link
Member

Thanks@ro0NL
@jderusse OK to implement this? Works for me (canceling my previous comment)

@jderusse
Copy link
MemberAuthor

Done

}

if ('query_string' ===$prefix) {
$queryString =parse_url($env,PHP_URL_QUERY) ??$env;

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

Copy link
Contributor

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.

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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

😭

@nicolas-grekas
Copy link
Member

Thank you@jderusse.

@nicolas-grekasnicolas-grekas merged commitf253c9b intosymfony:masterMar 18, 2019
nicolas-grekas added a commit that referenced this pull requestMar 18, 2019
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
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestMar 25, 2019
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
@nicolas-grekasnicolas-grekas modified the milestones:next,4.3Apr 30, 2019
@fabpotfabpot mentioned this pull requestMay 9, 2019
@jderussejderusse deleted the env-url branchAugust 2, 2019 12:15
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

+2 more reviewers

@stloydstloydstloyd requested changes

@ro0NLro0NLro0NL left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

9 participants

@jderusse@nicolas-grekas@javiereguiluz@ro0NL@andersonamuller@fabpot@stloyd@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp