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 "inherit-tags" with configurable defaults + same for "public", "tags" & "autowire"#21071

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
fabpot merged 3 commits intosymfony:masterfromnicolas-grekas:di-pragma
Jan 7, 2017

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedDec 27, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#20048
LicenseMIT
Doc PR-

Instead of making services private by default (#20048), which is going to create a big migration burden that might not be worth it, I'd like to propose the idea of changing the default for the current file.

Having a place to redefine some defaults, this can also be used to enable autowiring for a file, making it much lighter in the end.

This PR handles defaults for "public", "autowired" and "tags". Not sure the other options need a configurable default. Please comment if you think otherwise.

Short example (the feat is more interesting with a longer list of services):

services:_defaults:public:falseautowire:['_construct', 'set*']foo:class:Foo
<?xml version="1.0" encoding="utf-8"?><containerxmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">    <services>        <defaultspublic="false">            <autowire>__construct</autowire>            <tagname="foo"/>        </defaults>    </services></container>

ping@dunglas@weaverryan

GuilhemN, linaori, ogizanagi, unkind, chalasr, jvasseur, hason, tjaari, enleur, grachevko, and simshaun reacted with thumbs up emojiHeahDude reacted with hooray emoji
@ogizanagi
Copy link
Contributor

ogizanagi commentedDec 27, 2016
edited
Loading

Looks like a good idea to me. What about aliases, though?
Should we provide a way to let them public by default while services will be private?

service_defaults:public:['aliases']# aliases are public whereas services are privateservice_defaults:public:true# default: aliases and services are public <=> ['aliases', 'services']service_defaults:public:false# aliases and services are private <=> []

(It'll need a special treatment for theservice_defaults.public key, but as we won't generalize theservice_defaults for every definition keys, I'd rather see this as aloader_config key than a "parent definition" one)

nicolas-grekas, jvasseur, and enleur reacted with thumbs down emoji

@linaori
Copy link
Contributor

What about adding tags like this? I often find myself with a specific service definition file that contains a specific type of services. Think of a file for form types, they all have the same tag. It would be really nice if I could use this feature to automatically tag all definitions in that file by this implicit parent definition.

nicolas-grekas, GuilhemN, fancyweb, jvasseur, sstok, and grachevko reacted with thumbs up emoji

@GuilhemN
Copy link
Contributor

👍, you should add this new sectionhere or the ContainerBuilder will throw an exception.

@nicolas-grekasnicolas-grekasforce-pushed thedi-pragma branch 2 times, most recently fromac66912 tocd74550CompareDecember 27, 2016 18:40
@javiereguiluz
Copy link
Member

Minor comment: could we please take some time to think about this proposal before merging it? This kind of changes can have a deep impact in how developers use Symfony. Personally I don't have an opinion about the idea yet ... but I don't like the implementation. It doesn't feel "natural" for Symfony.

chalasr, lyrixx, and leftdevel reacted with thumbs up emoji

@ogizanagi
Copy link
Contributor

ogizanagi commentedDec 27, 2016
edited
Loading

As soon as it doesn't behaves blindly as a pseudo parent definition, but rather as a context/configuration exploitable by the loaders, it looks good to me. Hence, I don't really like theservice_defaults naming, but rather see something likeloader_config/context or whatever.

sstok reacted with thumbs up emoji

if (!is_array($content['services'])) {
thrownewInvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.',$file));
}
$defaults =isset($content['service_defaults']) ?$content['service_defaults'] :array();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think unsupported keys should throw an exception to be consistent with the rest of the loader.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

done

GuilhemN reacted with thumbs up emoji
@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedDec 27, 2016
edited
Loading

@javiereguiluz don't worry we have a process for that, this PR is no exception.

@ogizanagi I'm 👎 onloader_config because this is just leaking internal vocabulary that users don't necessarily know about.

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedDec 27, 2016
edited
Loading

PR completed for YamlLoader. If someone would like to work on XmlLoader, I'd happily accept a PR againstmy branch!

Copy link
Contributor

@ro0NLro0NL left a comment

Choose a reason for hiding this comment

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

👍


if (isset($service['alias'])) {
$public =!array_key_exists('public',$service)|| (bool)$service['public'];
$public =array_key_exists('public',$service)? (bool)$service['public'] : !empty($defaults['public']);
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to define this variable twice? One doingarray_key_exists otherisset?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

these are not on the same path, and don't have the same semantic (don't forget about ChildDefinition on the other path.)

ro0NL reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

I had services defined: (Symfony 3.1.8):

foo:class:My\FooServicebar:alias:foo

I could accessbar service in my controller because it was public by default. After this change here my aliased service is private by default, thus the service is not accessible in controller.

Shouldn't this be mentioned somewhere? I couldn't find any info about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch. That's a bug. See#21219

thrownewInvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".',gettype($defaults),$file));
}
foreach ($defaultsas$key =>$default) {
if (!in_array($key,$defaultKeys)) {
Copy link
Contributor

@ro0NLro0NLDec 27, 2016
edited
Loading

Choose a reason for hiding this comment

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

Perhaps go strict here?

Copy link
MemberAuthor

@nicolas-grekasnicolas-grekasDec 27, 2016
edited
Loading

Choose a reason for hiding this comment

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

done this way for consistency with the many otherin_array in this file, good enough anyway

@dunglas
Copy link
Member

dunglas commentedDec 28, 2016
edited
Loading

I like the idea!

What about using justdefaults as key?service_defaults is a bit redundant withservice:

defaults:public:falseautowire:['_construct', 'set*']services:foo:class:Foo

The best looking config is:

services:defaults:public:falseautowire:['_construct', 'set*']'Foo\Bar':{}

But I'm not sure that it's ok to deprecate some services names likedefault for this example.

chalasr, sstok, and TomasVotruba reacted with thumbs up emoji

@ro0NL
Copy link
Contributor

ro0NL commentedDec 28, 2016
edited
Loading

The best looking config is:

Really? The one above looks more intuitive to me...

defaults:#...services:#...

that is.

Looking at it like this looks good... then again; if parameters are involved (parameters: #...) it may work counter-intuitive.

Meaning eitherservice_defaults (:+1:), or indeedservice.defaults.

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedDec 28, 2016
edited
Loading

The root namespace is used for container extensions configuration, that means there is a potential for collision with an existing bundle that uses this "defaults" name.
This leads to another reason pro-service_defaults: the defaults we're talking about are related toservices only.
If you have eg:

framework:#...services:#...defaults:#...

it's not intuitive at all thatdefaults applies only toservices.
That's why I agree with@dunglas thatdefaults insideservices is the best of the two proposals.
But is it worth the potential collision?

@nicolas-grekas
Copy link
MemberAuthor

What about adding an underscore in this way?

services:_defaults:#...
ro0NL and enleur reacted with thumbs up emoji

@ro0NL
Copy link
Contributor

Perhaps* works out as well? It looks nicer then_defaults at least :) and seems to fit the case perfectly?

@nicolas-grekas
Copy link
MemberAuthor

I doubt* would be friendly to anyone not familiar with what it does.

@ro0NL
Copy link
Contributor

Think you're right.. just typed it out to visualize in fact. It looks good (imo.) but indeed..you need to know it.

service._defaults 👍

@GuilhemN
Copy link
Contributor

What about using something that sounds more specific to yaml such as!defaults (as a key ofservices)?
It would also decrease the risk of conflicts.

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedDec 28, 2016
edited
Loading

!defaults must be quoted in yaml, not very friendly. And we already use_ prefixes for special keys in eg routing yaml.

@GuilhemN
Copy link
Contributor

@nicolas-grekas I was thinking about a tag returning something similar to the JavaScriptSymbol but yes custom tags aren't supported yet.
Currently, unknown tags are considered as part of the scalar (e.g.!defaults remains!defaults as it is not known) so it's possible to use them and we could later migrate to a better tags system, wdyt?

@dunglas
Copy link
Member

+1 forservices._defaults. Btw we should take this occasion to deprecate any service name starting by a_.

@TomasVotruba
Copy link
Contributor

TomasVotruba commentedDec 30, 2016
edited
Loading

Just for inspiration from@nette, there is similar tool, that decorates specific classes/types of classes:

Seethis article - jump to "Decorator Extension to the Rescue" headline.

And this Tweet:https://twitter.com/VotrubaT/status/812802092356276224

decorator

I'm amazed, how similar these tools are :)

@grachevko
Copy link
Contributor

What about addfactory to list of _default options?
It can be useful to define doctrine repositories as service for example.

@TomasVotruba
Copy link
Contributor

@grachevko How would that look like? From example I understand it's global setup for all services.

@grachevko
Copy link
Contributor

grachevko commentedDec 31, 2016
edited
Loading

@TomasVotruba not a global. This PR add_default options which have affect only on file where it.
We can createsubscribers.yml and set_default.tags: [{ name: kernel.event_subscriber }] for all subscribers in this file without typing sametag to each.
Just the same we could createrepositories.yml and set_default.factory: ['@doctrine', 'getRepository'] without typing samefactory.

@TomasVotruba
Copy link
Contributor

@grachevko Ah. That's a pity. I was looking forward to get rid of Console, EventSubscriber, FormType etc. tags.

I'll work on that Decorator feature then :)

@fabpot
Copy link
Member

👍

1 similar comment
@dunglas
Copy link
Member

👍

@nicolas-grekasnicolas-grekasforce-pushed thedi-pragma branch 2 times, most recently from9545899 to88b99fbCompareJanuary 7, 2017 16:39
@fabpot
Copy link
Member

Thank you@nicolas-grekas.

@fabpotfabpot merged commitbeec1cf intosymfony:masterJan 7, 2017
fabpot added a commit that referenced this pull requestJan 7, 2017
…ame for "public", "tags" & "autowire" (nicolas-grekas, ogizanagi)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Add "inherit-tags" with configurable defaults + same for "public", "tags" & "autowire"| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#20048| License       | MIT| Doc PR        | -Instead of making services private by default (#20048), which is going to create a big migration burden that might not be worth it, I'd like to propose the idea of changing the default for the current file.Having a place to redefine some defaults, this can also be used to enable autowiring for a file, making it much lighter in the end.This PR handles defaults for "public", "autowired" and "tags". Not sure the other options need a configurable default. Please comment if you think otherwise.Short example (the feat is more interesting with a longer list of services):```yamlservices:    _defaults:        public: false        autowire: ['_construct', 'set*']    foo:        class: Foo``````xml<?xml version="1.0" encoding="utf-8"?><container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd">    <services>        <defaults public="false">            <autowire>__construct</autowire>            <tag name="foo"/>        </defaults>    </services></container>```ping@dunglas@weaverryanCommits-------beec1cf [DI] Allow definitions to inherit tags from parent context05f24d5 [DI] Add "defaults" tag to XML services configuration7b4a18b [DI] Add "_defaults" key to Yaml services configuration
@chalasr
Copy link
Member

The newinherit-tags option would have deserved its own line in the changelog imho, it's just great! Thanks

@nicolas-grekasnicolas-grekas deleted the di-pragma branchJanuary 9, 2017 14:01
fabpot added a commit that referenced this pull requestJan 9, 2017
…gizanagi)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Fixes aliases visibility with and without defaults| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | yes| New feature?  | no| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#21071 (comment)| License       | MIT| Doc PR        | N/ACommits-------f1cc090 [DI] Fixes aliases visibility with and without defaults
fabpot added a commit that referenced this pull requestJan 23, 2017
…izanagi)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Add Yaml syntax for short services definition| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | N/A| License       | MIT| Doc PR        | todoIn my experience, most (at least, a lot) of the service definitions in an end-application only require the class and constructor arguments.#21133 allows to get rid of the `class` attribute by using the service id.Which means only arguments remain for most use-cases. Hence, we could support this syntax:#### Before:```ymlservices:    App\Foo\Bar:        arguments: ['@baz', 'foo', '%qux%']```#### After:```ymlservices:    App\Foo\Bar: ['@baz', 'foo', '%qux%']```It works especially well along with services `_defaults` introduced in#21071 :```ymlservices:    _defaults:        public: false        autowire: ['set*']        tags: ['serializer.normalizer']    App\Serializer\FooNormalizer: ['@baz', 'foo', '%qux%']```Commits-------83b599c [DI] Add Yaml syntax for short services definition
@fabpotfabpot mentioned this pull requestMay 1, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas left review comments

@stofstofstof left review comments

+4 more reviewers

@takeittakeittakeit left review comments

@ro0NLro0NLro0NL left review comments

@ogizanagiogizanagiogizanagi left review comments

@GuilhemNGuilhemNGuilhemN left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

16 participants

@nicolas-grekas@ogizanagi@linaori@GuilhemN@javiereguiluz@dunglas@ro0NL@stof@sstok@weaverryan@TomasVotruba@grachevko@fabpot@chalasr@takeit@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp