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 Yaml syntax for short services definition#21313
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
javiereguiluz commentedJan 20, 2017
👍 I like this proposal a lot ... my only minor fear is that we may be introducing too many different ways/shortcuts to configure things. |
ogizanagi commentedJan 20, 2017 • 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.
I understand your fear, but to advocate even more this one, I find this way very intuitive as it echoes a php object instantiation: App\Serializer\FooNormalizer:['@baz', 'foo', '%qux%'] new \App\Serializer\FooNormalizer(newBaz(),'foo','qux_value') |
| return; | ||
| } | ||
| if (is_array($service) &&array_keys($service) ===range(0,count($service) -1)) { |
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.
array_values($service) === $service?
dunglas commentedJan 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
TomasVotruba commentedMar 9, 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.
Awesome feature 👍 Is there something similar for method calls? I'd like to use it for service definitions |
xabbuh commentedMar 10, 2018
services:App\Foo:calls: -[method, [arg1, arg2]] You mean this? |
TomasVotruba commentedMar 10, 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.
Yep, just smaller. I have following use case - I need to merge 3rd party classes in simple style in services: services:App\Foo:key:value But in the background (custom One to: services:App\FooTypeA:properties:key:value and other type: services:App\FooTypeB:calls: -['setKey', ['value']] Key is to keep configuration simple and make use of Any ideas for this? |
TomasVotruba commentedMar 10, 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.
After many Locator, Provider, YamlFileLoader, cusom file parsing etc. I came to this solution: With all the troubles I'm super happy. But any ideas are welcomed :) <?phpdeclare(strict_types=1);namespaceSymplify\EasyCodingStandard\Yaml;useNette\Utils\Strings;useSymfony\Component\Config\FileLocatorInterface;useSymfony\Component\DependencyInjection\ContainerBuilder;useSymfony\Component\DependencyInjection\Loader\YamlFileLoader;/** * The need: https://github.com/symfony/symfony/pull/21313#issuecomment-372037445 */finalclass CheckerTolerantYamlFileLoaderextends YamlFileLoader{/** * @var CheckerConfigurationGuardian */private$checkerConfigurationGuardian;publicfunction__construct(ContainerBuilder$containerBuilder,FileLocatorInterface$fileLocator) {$this->checkerConfigurationGuardian =newCheckerConfigurationGuardian();parent::__construct($containerBuilder,$fileLocator); }/** * @param string $file * @return array|mixed|mixed[] */protectedfunctionloadFile($file) {$decodedYaml =parent::loadFile($file);if (isset($decodedYaml['services'])) {return$this->moveArgumentsToPropertiesOrMethodCalls($decodedYaml); }return$decodedYaml; }/** * @param mixed[] $yaml * @return mixed[] */privatefunctionmoveArgumentsToPropertiesOrMethodCalls(array$yaml):array {foreach ($yaml['services']as$checker =>$serviceDefinition) {if (empty($serviceDefinition)) {continue; }// is checker service?if (! Strings::endsWith($checker,'Fixer') && ! Strings::endsWith($checker,'Sniff')) {continue; }if (Strings::endsWith($checker,'Fixer')) {$this->checkerConfigurationGuardian->ensureFixerIsConfigurable($checker,$serviceDefinition);// move parameters to "configure()" call$yaml['services'][$checker]['calls'] = [ ['configure', [$serviceDefinition]], ]; }if (Strings::endsWith($checker,'Sniff')) {// move parameters to property settersforeach ($serviceDefinitionas$key =>$value) {$this->checkerConfigurationGuardian->ensurePropertyExists($checker,$key);$yaml['services'][$checker]['properties'][$key] =$this->escapeValue($value); } }// cleanup parametersforeach ($serviceDefinitionas$key =>$value) { unset($yaml['services'][$checker][$key]); } }return$yaml; }/** * @param mixed $value * @return mixed */privatefunctionescapeValue($value) {if (is_numeric($value)) {return$value; }return Strings::replace($value,'#@#','@@'); }} |
Uh oh!
There was an error while loading.Please reload this page.
In 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
classattribute by using the service id.Which means only arguments remain for most use-cases. Hence, we could support this syntax:
Before:
After:
It works especially well along with services
_defaultsintroduced in#21071 :