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

Merged
fabpot merged 1 commit intosymfony:masterfromogizanagi:di/yaml_short_service_definition
Jan 23, 2017
Merged

[DI] Add Yaml syntax for short services definition#21313

fabpot merged 1 commit intosymfony:masterfromogizanagi:di/yaml_short_service_definition
Jan 23, 2017

Conversation

@ogizanagi
Copy link
Contributor

@ogizanagiogizanagi commentedJan 16, 2017
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsN/A
LicenseMIT
Doc PRtodo

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 theclass attribute by using the service id.
Which means only arguments remain for most use-cases. Hence, we could support this syntax:

Before:

services:App\Foo\Bar:arguments:['@baz', 'foo', '%qux%']

After:

services:App\Foo\Bar:['@baz', 'foo', '%qux%']

It works especially well along with services_defaults introduced in#21071 :

services:_defaults:public:falseautowire:['set*']tags:['serializer.normalizer']App\Serializer\FooNormalizer:['@baz', 'foo', '%qux%']

yceruto, jvasseur, chalasr, linaori, hason, theofidry, and TomasVotruba reacted with thumbs up emojijvasseur, ChadSikorra, theofidry, and eko reacted with hooray emoji
@javiereguiluz
Copy link
Member

👍 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
Copy link
ContributorAuthor

ogizanagi commentedJan 20, 2017
edited
Loading

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')
theofidry reacted with thumbs up emoji

return;
}

if (is_array($service) &&array_keys($service) ===range(0,count($service) -1)) {
Copy link
Member

Choose a reason for hiding this comment

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

array_values($service) === $service?

ogizanagi reacted with thumbs up emoji
@dunglas
Copy link
Member

👍

@fabpotfabpot merged commit83b599c intosymfony:masterJan 23, 2017
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
@ogizanagiogizanagi deleted the di/yaml_short_service_definition branchJanuary 23, 2017 21:44
@fabpotfabpot mentioned this pull requestMay 1, 2017
@TomasVotruba
Copy link
Contributor

TomasVotruba commentedMar 9, 2018
edited
Loading

Awesome feature 👍

Is there something similar for method calls?

I'd like to use it for service definitions

@xabbuh
Copy link
Member

services:App\Foo:calls:            -[method, [arg1, arg2]]

You mean this?

@TomasVotruba
Copy link
Contributor

TomasVotruba commentedMar 10, 2018
edited
Loading

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 (customYamlFileLoader?) I need to covert to 2 types based on 2 interfaces they can implement:

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 ofservices (for various reasons).

Any ideas for this?

@TomasVotruba
Copy link
Contributor

TomasVotruba commentedMar 10, 2018
edited
Loading

After many Locator, Provider, YamlFileLoader, cusom file parsing etc. I came to this solution:

https://github.com/Symplify/Symplify/blob/d27851e30db419b1960f7b98e7d91a62fbd7cb7b/packages/EasyCodingStandard/src/Yaml/CheckerTolerantYamlFileLoader.php#L27-L81

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,'#@#','@@');    }}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas left review comments

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

7 participants

@ogizanagi@javiereguiluz@dunglas@TomasVotruba@xabbuh@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp