Skip to content

Commit 3372071

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 3372071

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: 107 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,123 @@
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 service definitions being registered by
6+
# this file.
7+
#
8+
# The "autowire" section tells Symfony to make its best to autowire new services
9+
# by introspecting their constructor.
10+
#
11+
# The "public" section with the FALSE boolean value forces all new autowired classes
12+
# to be declared as private services. Marking services private limits their scope to
13+
# the Symfony service container itself only. They're not accessible from the outside
14+
# world and thus let the service container optimizes itself when it's being compiled
15+
# and dumped to raw PHP code.
16+
_defaults:
17+
autowire: true
18+
public: false
919

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 }
20+
# The special "_instanceof" section appends additional wiring configuration
21+
# to any service definitions that match the listed types.
22+
#
23+
# For instance, the first rule adds a special "kernel.event_subscriber" tag
24+
# to any definitions declared in this file whose class is an instance of the
25+
# "Symfony\Component\EventDispatcher\EventSubscriberInterface" class or interface.
26+
_instanceof:
27+
Symfony\Component\EventDispatcher\EventSubscriberInterface:
28+
tags: ['kernel.event_subscriber']
29+
30+
Symfony\Component\Form\FormTypeInterface:
31+
tags: ['form.type']
32+
33+
Symfony\Component\Security\Core\Authorization\VoterInterface:
34+
tags: ['security.voter']
35+
36+
Twig_ExtensionInterface:
37+
tags: ['twig.extension']
38+
39+
# The following settings will be appended to any local service definitions
40+
# whose class matches this type. In this example, it will only apply to
41+
# the "AppBundle\Controller\Admin\BlogController" service discovered by
42+
# the "AppBundle\Controller\" rule below.
43+
AppBundle\Controller\Admin\BlogController:
44+
getters:
45+
getSlugger: '@AppBundle\Utils\Slugger'
1846

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

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 }
90+
# Some classes cannot be fully autowired because their methods accept either
91+
# some scalar arguments that Symfony cannot guess or a typehinted dependency
92+
# for which the container has at least two registered services matching the
93+
# type.
94+
#
95+
# Both the "AppBundle\Twig\AppExtension" and "AppBundle\EventListener\RedirectToPreferredLocaleSubscriber"
96+
# classes have a "__construct()" method that receives a "$locales" scalar argument
97+
# that Symfony cannot guess. This is why we must manually and explicitly provide
98+
# the wiring of this argument to complete their service definitions.
99+
#
100+
# To do so, the remaining unwired named arguments must be defined with their
101+
# corresponding values. Here, the "$locales" named argument is configured to
102+
# receive the value of the global "%app_locales%" parameter defined under the
103+
# "parameters" section of the "app/config/config.yml" file.
104+
#
105+
# Note that the order in which the named arguments are defined below doesn't
106+
# matter as they're referenced here by their real names in the PHP code.
107+
# Symfony is then smart enough to make the corresponding matching when compiling,
108+
# optimizing and dumping the container as a raw PHP class in the cache directory.
109+
AppBundle\Twig\AppExtension:
110+
$locales: '%app_locales%'
111+
112+
AppBundle\EventListener\RedirectToPreferredLocaleSubscriber:
113+
$locales: '%app_locales%'
33114

34115
# Event Listeners are classes that listen to one or more specific events.
35116
# Those events are defined in the tags added to the service definition.
36117
# 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
118+
AppBundle\EventListener\CommentNotificationListener:
45119
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
46120
# The "method" attribute of this tag is optional and defaults to "on + camelCasedEventName"
47121
# If the event is "comment.created" the method executed by default is "onCommentCreated()".
48122
tags:
49123
- { 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)