Skip to content

Commit 94e428b

Browse files
committed
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
2 parents 4c08251 + db461e7 commit 94e428b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class FileLoader extends Loader
3232
*/
3333
protected $locator;
3434

35-
private $currentDir;
35+
protected $currentDir;
3636

3737
/**
3838
* Constructor.

0 commit comments

Comments
 (0)