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
Fabien Potencier 91904af902 feature #21289 [DI] Add prototype services for PSR4-based discovery and registration (nicolas-grekas)
This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Add prototype services for PSR4-based discovery and registration

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | to be done

Talking with @dunglas, we wondered if this could be a good idea, as a more general approach to folder-based service registration as done in [DunglasActionBundle](https://github.com/dunglas/DunglasActionBundle/blob/master/DependencyInjection/DunglasActionExtension.php).

So here is the implementation.

This allows one to define a set of services as such:
```yaml
services:
    App\:
        resources: ../src/{Controller,Command} # relative to the current file as usual
        autowire: true # or any other attributes really
```

This looks for php files in the "src" folder, derivates PSR-4 class names from them, and uses `class_exists` for final discovery. The resulting services are named after the classes found this way.

The "resource" attribute can be a glob to select only a subset of the classes/files.

This approach has several advantages over [DunglasActionBundle](https://github.com/dunglas/DunglasActionBundle/blob/master/DependencyInjection/DunglasActionExtension.php):
- it is resilient to missing parent classes (see test case)
- it loads classes using the normal code path (the autoloader), thus play well with them (e.g. if one registered a special autoloader).
- it is more predictable, because it uses discovered paths as the only source of ids/classes to register - vs relying on `get_declared_classes`, which would make things context sensitive.

Fits well with current initiatives to me.

Commits
-------

03470b788e [DI] Add "psr4" service attribute for PSR4-based discovery and registration
2017-02-13 06:22:10 +01:00
..
Argument [DI] Allow to count on lazy collection arguments 2017-01-31 10:40:15 +01:00
Compiler feature #21505 [Config][DI] Add ComposerResource to track the runtime engine + deps (nicolas-grekas) 2017-02-12 12:59:14 +01:00
Config minor #21090 Secure unserialize by restricting allowed classes when using PHP 7 (dbrumann) 2017-02-12 20:14:59 +01:00
Dumper feature #21194 [Yaml] Add tags support (GuilhemN) 2017-02-10 15:54:52 +01:00
Exception Implement PSR-11 2017-02-02 08:42:59 +01:00
Extension [DI][Config] Add & use ReflectionClassResource 2017-02-02 14:15:15 +01:00
LazyProxy [DI] Refacto / cleanup / minor fixes 2017-02-08 13:32:07 +01:00
Loader feature #21289 [DI] Add prototype services for PSR4-based discovery and registration (nicolas-grekas) 2017-02-13 06:22:10 +01:00
ParameterBag [DI] Allow null as default env value 2016-11-23 14:12:24 +01:00
Tests feature #21289 [DI] Add prototype services for PSR4-based discovery and registration (nicolas-grekas) 2017-02-13 06:22:10 +01:00
.gitignore
Alias.php [DI] Deprecate case insentivity of service identifiers 2017-01-11 16:25:44 +01:00
CHANGELOG.md [DI] Add "psr4" service attribute for PSR4-based discovery and registration 2017-02-12 23:09:17 +01:00
ChildDefinition.php [DI] Refacto / cleanup / minor fixes 2017-02-08 13:32:07 +01:00
composer.json [DI] Add "psr4" service attribute for PSR4-based discovery and registration 2017-02-12 23:09:17 +01:00
Container.php [DI] Deprecate (un)setting pre-defined services 2017-02-05 11:30:27 +01:00
ContainerAwareInterface.php [DI] Clean a phpdoc 2015-11-04 19:12:53 +01:00
ContainerAwareTrait.php Merge branch '2.3' into 2.7 2015-11-04 19:50:51 +01:00
ContainerBuilder.php feature #21505 [Config][DI] Add ComposerResource to track the runtime engine + deps (nicolas-grekas) 2017-02-12 12:59:14 +01:00
ContainerInterface.php Implement PSR-11 2017-02-02 08:42:59 +01:00
Definition.php [DI] Refacto / cleanup / minor fixes 2017-02-08 13:32:07 +01:00
DefinitionDecorator.php Merge branch '2.8' into 3.1 2016-12-27 11:43:25 +01:00
ExpressionLanguage.php Fixed minor typo - override 2015-01-03 21:58:03 +01:00
ExpressionLanguageProvider.php [DependencyInjection] added support for custom expression language functions 2014-09-25 19:08:38 +02:00
LICENSE updated LICENSE year 2017-01-02 12:30:00 -08:00
Parameter.php [DependencyInjection] fix phpDoc 2016-04-11 16:45:49 +02:00
phpunit.xml.dist Add missing exclusions from phpunit.xml.dist 2015-11-18 09:19:46 +01:00
README.md Updated all the README files 2016-03-04 08:12:06 +01:00
Reference.php [DI] Deprecate case insentivity of service identifiers 2017-01-11 16:25:44 +01:00
ResettableContainerInterface.php Implement resettable containers 2015-07-22 10:09:28 +02:00
TaggedContainerInterface.php remove api tags from code 2015-09-28 19:11:22 +02:00
Variable.php [DependencyInjection] fix phpDoc 2016-04-11 16:45:49 +02:00

DependencyInjection Component

The DependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.

Resources