New in Symfony 3.3: PSR-4 based Service Discovery
May 15, 2017 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Symfony 3.3 will introduce lots of features to simplify the way you work with services in your applications, such as simpler configuration and autoconfig. This article explains the last big feature related to Dependency Injection: PSR-4 based service discovery and registration.
The idea is to look for PHP classes in some given directories and register them
as services if their namespaces meet the PSR-4 naming syntax. This feature is
configured using the resource
option, which accepts a directory path or a
Glob expression to match multiple directories. Example:
1 2 3 4
services:
App\:
resource: ../src/{Controller,Command}
# ...
This config looks for PHP files in the src/Controller/
and src/Command/
directories of the application, infers the PSR-4 class names from those files
and uses class_exist()
to check that they exist before registering those
classes as Symfony services (using the class FQCN as the service id).
This feature is so convenient that Symfony Flex uses it by default in the
app.yaml
config file that will be used in Symfony 4 applications:
1 2 3 4 5 6 7 8 9 10 11
services:
# ...
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../../src/{Command,Form,EventSubscriber,Twig,Security}'
App\Controller\:
resource: '../../src/Controller'
public: true
tags: ['controller.service_arguments']
If your application contains lots of directories, you can include all of them
using a *
value in the resource
option and then use the exclude
option to exclude some directories if needed:
1 2 3 4 5 6 7 8
services:
# ...
AppBundle\:
# discover services in all AppBundle/ sub-directories...
resource: '../../src/AppBundle/*'
# ... except in those matching this Glob expression
exclude: '../../src/AppBundle/{AppBundle.php,Entity}'
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Can anyone explain to me the benefit of this change? Or is there a Github issue I could read? Genuine request.
https://bugs.php.net/bug.php?id=72095
http://ftp.grokbase.com/t/php/php-doc-bugs/164rneeqx3/php-bug-bug-72095-new-undefined-constant-glob-brace-while-doc-base-configure-php
https://github.com/zendframework/zend-stdlib/issues/58