Skip to content

Commit de0a81c

Browse files
author
Hugo Hamon
committed
[app] make the app/config/services.yml file use the new Symfony autoregistration and autowiring features.
1 parent 291a62e commit de0a81c

File tree

7 files changed

+284
-91
lines changed

7 files changed

+284
-91
lines changed

app/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function registerBundles()
3232
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
3333
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
3434
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
35+
$bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
3536
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
3637
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
3738

app/config/services.yml

Lines changed: 105 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,121 @@
11
services:
2-
# First we define some basic services to make these utilities available in
3-
# the entire application
4-
slugger:
5-
class: AppBundle\Utils\Slugger
62

7-
markdown:
8-
class: AppBundle\Utils\Markdown
3+
# The special "_defaults" section configure the default behaviors for registering
4+
# new services into the Symfony service container. This section has a local scope
5+
# only, which means it only affects the services definitions being registered by
6+
# this file.
7+
#
8+
# The "autowire" section indicates Symfony will make its best to autowire new
9+
# services by introspecting their constructor, getter and setter methods prototypes.
10+
# The syntax for autowiring methods uses a glob pattern that can be adapted according
11+
# to your needs. Constructor methods are always autowired by default, so there is no
12+
# need to add "__construct" to the list of methods patterns.
13+
#
14+
# The "public" section with the FALSE boolean value forces all new autowired classes
15+
# to be declared as private services. Marking services private limits their scope to
16+
# the Symfony service container itself only. They're not accessible from the outside
17+
# world and thus let the service container optimizes itself when it's being compiled
18+
# and dumped to raw PHP code.
19+
_defaults:
20+
autowire: ['get*', 'set*']
21+
public: false
922

10-
# These are the Twig extensions that create new filters and functions for
11-
# using them in the templates
12-
app.twig.app_extension:
13-
public: false
14-
class: AppBundle\Twig\AppExtension
15-
arguments: ['@markdown', '%app_locales%']
16-
tags:
17-
- { name: twig.extension }
23+
# The special "_instanceof" section registers common batch behaviors to apply
24+
# to any new registered services definitions that match the conditional rules.
25+
#
26+
# For instance, the first rule forces the service container to add a special
27+
# "kernel.event_subscriber" tag to any new registered services of this file
28+
# whose class is an instance of the "Symfony\Component\EventDispatcher\EventSubscriberInterface"
29+
# class or interface.
30+
_instanceof:
31+
Symfony\Component\EventDispatcher\EventSubscriberInterface:
32+
tags: ['kernel.event_subscriber']
1833

34+
Symfony\Component\Form\FormTypeInterface:
35+
tags: ['form.type']
36+
# temporary workaround as form types services will be soon made private in Symfony 3.3
37+
public: true
38+
39+
Symfony\Component\Security\Core\Authorization\VoterInterface:
40+
tags: ['security.voter']
41+
42+
Twig_ExtensionInterface:
43+
tags: ['twig.extension']
44+
45+
# This section enables to automatically register all classes found in the matching
46+
# file paths and directories as services in the container. File and directory
47+
# matching uses any valid glob pattern to create a white list of paths.
48+
#
49+
# In this example, the classes found in the following directories will be
50+
# automatically registered as services:
51+
#
52+
# * src/AppBundle/Form/Type/
53+
# * src/AppBundle/Security/
54+
# * src/AppBundle/Twig/
55+
# * src/AppBundle/Utils/
56+
#
57+
# Thus, only the classes whose corresponding filename ends with the "Subscriber.php"
58+
# suffix in the src/AppBundle/EventListener/ directory will be registered as
59+
# services in the service container. Other classes of this directory will be simply
60+
# ignored.
61+
AppBundle\:
62+
# Register all classes in the src/AppBundle directory as services
63+
resource: '../../src/AppBundle/{EventListener/*Subscriber.php,Form/Type,Security,Twig,Utils}'
64+
65+
# The other section defines a rule to automatically register and autowire the
66+
# controller classes found in the src/AppBundle/Controller/ directory.
67+
#
68+
# By default all services are made private according to the global "_defaults"
69+
# section at the top of this file.
70+
#
71+
# However, in Symfony, controllers must always be declared public in order to
72+
# be lazy instanciated when they're really needed. This is why the inherited
73+
# default "public" attribute is overriden to force the registered controller
74+
# services to be marked public.
75+
AppBundle\Controller\:
76+
resource: '../../src/AppBundle/Controller'
77+
public: true
78+
79+
# This third party Twig extension must be manually registered as a service
80+
# because its class doesn't live in any of the previous defined directories.
81+
#
82+
# Thus, its class is not namespaced and cannot be defined in a global rule.
83+
# Indeed, registering new classes whose filename matches a glob pattern as
84+
# services like in the two previous sections only works for namespaced classes.
1985
app.twig.intl_extension:
20-
public: false
2186
class: Twig_Extensions_Extension_Intl
22-
tags:
23-
- { name: twig.extension }
2487

25-
# Defining a form type as a service is only required when the form type
26-
# needs to use some other services, such as the entity manager.
27-
# See http://symfony.com/doc/current/best_practices/forms.html
28-
app.form.type.tagsinput:
29-
class: AppBundle\Form\Type\TagsInputType
30-
arguments: ['@doctrine.orm.entity_manager']
31-
tags:
32-
- { name: form.type }
88+
# Some classes cannot be fully autowired because their methods accept either
89+
# some scalar arguments that Symfony cannot guess or a typehinted dependency
90+
# for which the container has at least two registered services matching the
91+
# type.
92+
#
93+
# Both the "AppBundle\Twig\AppExtension" and "AppBundle\EventListener\RedirectToPreferredLocaleSubscriber"
94+
# classes have a "__construct()" method that receives a "$locales" scalar argument
95+
# that Symfony cannot guess. This is why we must manually and explicitly provide
96+
# the wiring of this argument to complete their service definitions.
97+
#
98+
# To do so, the remaining unwired named arguments must be defined with their
99+
# corresponding values. Here, the "$locales" named argument is configured to
100+
# receive the value of the global "%app_locales%" parameter defined under the
101+
# "parameters" section of the "app/config/config.yml" file.
102+
#
103+
# Note that the order in which the named arguments are defined below doesn't
104+
# matter as they're referenced here by their real names in the PHP code.
105+
# Symfony is then smart enough to make the corresponding matching when compiling,
106+
# optimizing and dumping the container as a raw PHP class in the cache directory.
107+
AppBundle\Twig\AppExtension:
108+
$locales: '%app_locales%'
109+
110+
AppBundle\EventListener\RedirectToPreferredLocaleSubscriber:
111+
$locales: '%app_locales%'
33112

34113
# Event Listeners are classes that listen to one or more specific events.
35114
# Those events are defined in the tags added to the service definition.
36115
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-listener
37-
app.redirect_to_preferred_locale_listener:
38-
class: AppBundle\EventListener\RedirectToPreferredLocaleListener
39-
arguments: ['@router', '%app_locales%', '%locale%']
40-
tags:
41-
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
42-
43-
app.comment_notification:
44-
class: AppBundle\EventListener\CommentNotificationListener
116+
AppBundle\EventListener\CommentNotificationListener:
45117
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
46118
# The "method" attribute of this tag is optional and defaults to "on + camelCasedEventName"
47119
# If the event is "comment.created" the method executed by default is "onCommentCreated()".
48120
tags:
49121
- { name: kernel.event_listener, event: comment.created, method: onCommentCreated }
50-
51-
# Event subscribers are similar to event listeners but they don't need service tags.
52-
# Instead, the PHP class of the event subscriber includes a method that returns
53-
# the list of events listened by that class.
54-
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
55-
app.requirements_subscriber:
56-
class: AppBundle\EventListener\CheckRequirementsSubscriber
57-
arguments: ['@doctrine.orm.entity_manager']
58-
tags:
59-
- { name: kernel.event_subscriber }
60-
61-
# To inject the voter into the security layer, you must declare it as a service and tag it with security.voter.
62-
# See http://symfony.com/doc/current/security/voters.html#configuring-the-voter
63-
app.post_voter:
64-
class: AppBundle\Security\PostVoter
65-
public: false
66-
tags:
67-
- { name: security.voter }
68-
69-
# Uncomment the following lines to define a service for the Post Doctrine repository.
70-
# It's not mandatory to create these services, but if you use repositories a lot,
71-
# these services simplify your code:
72-
#
73-
# app.post_repository:
74-
# class: Doctrine\ORM\EntityRepository
75-
# factory: ['@doctrine.orm.entity_manager', getRepository]
76-
# arguments: [AppBundle\Entity\Post]
77-
#
78-
# // traditional code inside a controller
79-
# $entityManager = $this->getDoctrine()->getManager();
80-
# $posts = $entityManager->getRepository('AppBundle:Post')->findAll();
81-
#
82-
# // same code using repository services
83-
# $posts = $this->get('app.post_repository')->findAll();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"symfony/monolog-bundle" : "^3.0",
2626
"symfony/polyfill-apcu" : "^1.0",
2727
"symfony/swiftmailer-bundle" : "^2.3",
28-
"symfony/symfony" : "^3.2",
28+
"symfony/symfony" : "^3.3@dev",
2929
"twig/extensions" : "^1.3",
3030
"twig/twig" : "^1.28 || ^2.0",
3131
"white-october/pagerfanta-bundle" : "^1.0"

0 commit comments

Comments
 (0)