This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml
Fabien Potencier 3fa8a0571d feature #21763 [DI] Replace wildcard-based methods autowiring by @required annotation (nicolas-grekas)
This PR was squashed before being merged into the 3.3-dev branch (closes #21763).

Discussion
----------

[DI] Replace wildcard-based methods autowiring by `@required` annotation

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no (affects things that are only on master)
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

While playing a bit with new features in master around DI configuration, several people around me got bitten by wildcard-based autowiring. The typical example is adding `autowire: [set*]` in `_defaults`: use that on `resource: ../src/Command/` PSR4-based loading and boom, `setApplication` and `setHelperSet` will now be wrongly called. You could tell me "of course, don't to that" - but being bitten so early on a master-only feature makes me really unconfident that this will be easy enough for people after the release.

If wildcard-based autowiring is removed, then I don't see anymore the need for allowing arrays as in `autowire: [setFoo,getBar]`. Moreover, this array syntax has a core DX issue: it's a dead end as far as the learning curve is concerned. You learn it, then when becoming a more advanced dev, someone teaches you that you'd better use another syntax: explicit wiring.

And in fact, we don't need it at all, because something else already exists: just declare a method call, but don't define its arguments. If `autowire: true` is set, then the AutowiringPass already fills in the holes. There is only one tweak required to make this work: don't autowire optional arguments for method calls - or that'd be a BC break. To my PoV that's even better: this makes autowiring fit a "do the minimum to make it work" strategy. A really good one to me.

But there is still an issue: wildcard-based autowiring fits a need. Namely, it allows one to define a convention (eg. `'set*'`), and have all such methods that follow the convention be autowired. To me, this looks like doing it reverse (the DI config should adapt to the code, not reverse). So, to fill this need, let the declaration be in the source: just use an annotation!

This PR adds support for the `@required` annotation, borrowed from the Spring framework:
https://www.tutorialspoint.com/spring/spring_required_annotation.htm

Using the annotation is totally optional of course. If you do, *and if autowiring is on*, then it'll be autowired. If you don't, nothing changes: do manual wiring.

Even when not using autowiring, the annotation is still a nice hint for the consumer of your classes: it tells the reader that this method needs to be called for correct instantiation - thus lowering one drawback of setter injection (discoverability).

The implementation of the annotation parsing is done using a few regexp (no dep on any complex parser) - and works with inheritance, by leveraging the `@inheritdoc` tag (the default behavior being to *not* inherit anything from parent methods).

All in all, looking at the diff stats, it makes everything simpler. Good sign, isn't it?

Commits
-------

f286fcc25f [DI] Replace wildcard-based methods autowiring by `@required` annotation
9081699980 Revert "minor #21315 [DI][FrameworkBundle] Show autowired methods in descriptors (ogizanagi)"
2017-03-01 12:47:24 -08:00
..
bad_calls.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_decorates.yml Improved the error message when using "@" in a decorated service 2016-03-03 14:15:12 +01:00
bad_format.yml Improve exception messages. 2015-09-11 08:57:18 +01:00
bad_import.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_imports.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_parameters.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_service.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_services.yml [DI] Added safeguards against invalid config in the YamlFileLoader 2014-09-02 09:31:18 +02:00
bad_types1.yml [DependencyInjection] Add autowiring capabilities 2015-10-03 08:39:08 +02:00
bad_types2.yml [DependencyInjection] Add autowiring capabilities 2015-10-03 08:39:08 +02:00
badtag1.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
badtag2.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
badtag3.yml [DependencyInjection] added tags validation when compiling a container 2013-09-12 12:52:36 +02:00
class_from_id.yml [DI] Add tests for class named services 2017-01-07 16:55:01 +01:00
legacy_invalid_alias_definition.yml [DependencyInjection] Deprecate unsupported attributes/elements for alias 2016-01-16 15:18:32 +01:00
legacy_invalid_definition.yml [DependencyInjection] make YamlFileLoader raise a deprecation notice if a service definition contains unsupported keywords. 2015-12-29 10:37:15 +01:00
nonvalid1.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
nonvalid2.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services1.yml [DependencyInjection] make the service container builder register the definition of its related service container service (and aliases) in order to make compiler passes be able to reference the special service_container service. 2017-02-27 17:26:21 +01:00
services2.yml [DI] added support for PHP constants in yaml configuration files 2016-06-23 15:44:47 +02:00
services3.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services4_bad_import.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services4.yml [DI] FileLoaders: Allow to explicit type to load 2017-01-10 12:54:54 +01:00
services6.yml [DI][DX] Do not map id to class for global classes 2017-01-29 23:41:20 +01:00
services7.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services8.yml [DependencyInjection] make the service container builder register the definition of its related service container service (and aliases) in order to make compiler passes be able to reference the special service_container service. 2017-02-27 17:26:21 +01:00
services9.yml [DependencyInjection] make the service container builder register the definition of its related service container service (and aliases) in order to make compiler passes be able to reference the special service_container service. 2017-02-27 17:26:21 +01:00
services10.yml [DependencyInjection] fix dumped YAML snytax 2016-02-15 22:04:15 +01:00
services11.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services13.yml moved component and bridge unit tests to the src/ directory 2012-03-29 08:37:22 +02:00
services14.yml [DependencyInjection] Tweaked factories 2014-10-02 01:48:53 +04:00
services21.yml added missing quotes in YAML files 2015-10-27 19:40:10 -07:00
services22.yml [DependencyInjection] Add autowiring capabilities 2015-10-03 08:39:08 +02:00
services23.yml [DependencyInjection] Add autowiring capabilities 2015-10-03 08:39:08 +02:00
services24.yml [DependencyInjection] make the service container builder register the definition of its related service container service (and aliases) in order to make compiler passes be able to reference the special service_container service. 2017-02-27 17:26:21 +01:00
services26.yml [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% syntax 2016-09-10 12:12:34 +02:00
services28.yml [DI][DX] Do not map id to class for global classes 2017-01-29 23:41:20 +01:00
services31_invalid_tags.yml [DependencyInjection] Yaml: check if is an array before using it 2017-01-24 13:43:55 +01:00
services31.yml [DI] Add getter injection 2017-01-28 08:49:57 +01:00
services_configurator_short_syntax.yml [DependencyInjection] Yaml: check if is an array before using it 2017-01-24 13:43:55 +01:00
services_instanceof.yml [DependencyInjection] Tests + refacto for "instanceof" definitions 2017-02-17 19:36:36 +01:00
services_locator_argument.yml [DI] Replace container injection by explicit service locators 2017-02-13 11:05:06 +01:00
services_named_args.yml [DependencyInjection] Add a new pass to check arguments validity 2017-02-13 10:03:44 +01:00
services_prototype.yml [DI] Add "psr4" service attribute for PSR4-based discovery and registration 2017-02-12 23:09:17 +01:00
services_underscore.yml [DI] Deprecate underscore-services in YamlFileLoader 2017-02-02 16:30:45 +01:00
tag_name_empty_string.yml [DependencyInjection] enforce tags to have a name 2016-01-27 23:29:55 +01:00
tag_name_no_string.yml [DependencyInjection] enforce tags to have a name 2016-01-27 23:29:55 +01:00
tag_name_only.yml Added Yaml syntax shortcut for name-only tags 2016-11-27 12:17:06 +01:00
yaml_with_wrong_ext.ini [DI] FileLoaders: Allow to explicit type to load 2017-01-10 12:54:54 +01:00