Skip to content

Commit 0839806

Browse files
committed
Update some examples using 3.3 features
1 parent 3a9e1ba commit 0839806

16 files changed

+155
-265
lines changed

best_practices/templates.rst

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,8 @@ a new dependency of the project:
7373
7474
$ composer require erusev/parsedown
7575
76-
Then, create a new ``Markdown`` service that will be used later by the Twig
77-
extension. The service definition only requires the path to the class:
78-
79-
.. code-block:: yaml
80-
81-
# app/config/services.yml
82-
services:
83-
# ...
84-
app.markdown:
85-
class: AppBundle\Utils\Markdown
86-
87-
And the ``Markdown`` class just needs to define one single method to transform
76+
Then, create a new ``Markdown`` class that will be used later by the Twig
77+
extension. It just needs to define one single method to transform
8878
Markdown content into HTML::
8979

9080
namespace AppBundle\Utils;
@@ -147,17 +137,36 @@ service in the constructor of the Twig extension:
147137
}
148138
}
149139
150-
Lastly define a new service to enable this Twig extension in the app (the service
151-
name is irrelevant because you never use it in your own code):
140+
Lastly make sure these new classes are registered as services:
152141

153142
.. code-block:: yaml
154143
155144
# app/config/services.yml
156145
services:
157-
app.twig.app_extension:
158-
class: AppBundle\Twig\AppExtension
146+
# default configuration for services in *this* file
147+
_defaults:
148+
autowire: true
149+
autoconfigure: true
150+
public: false
151+
152+
# loads services from whatever directories you want (you can update this!)
153+
AppBundle\:
154+
resource: '../../src/AppBundle/{Twig,Utils}'
155+
156+
Or if you want to register them explicitly:
157+
158+
.. code-block:: yaml
159+
160+
# app/config/services.yml
161+
services:
162+
_defaults:
163+
public: false
164+
165+
app.markdown:
166+
class: AppBundle\Utils\Markdown
167+
168+
AppBundle\Twig\AppExtension:
159169
arguments: ['@app.markdown']
160-
public: false
161170
tags: [twig.extension]
162171
163172
.. _`Twig`: http://twig.sensiolabs.org/

console/commands_as_services.rst

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ with ``console.command``:
2727
2828
# app/config/config.yml
2929
services:
30-
app.command.my_command:
31-
class: AppBundle\Command\MyCommand
32-
tags: [console.command]
30+
AppBundle\Command\MyCommand: [console.command]
3331
3432
.. code-block:: xml
3533
@@ -41,8 +39,7 @@ with ``console.command``:
4139
http://symfony.com/schema/dic/services/services-1.0.xsd">
4240
4341
<services>
44-
<service id="app.command.my_command"
45-
class="AppBundle\Command\MyCommand">
42+
<service id="AppBundle\Command\MyCommand">
4643
<tag name="console.command" />
4744
</service>
4845
</services>
@@ -54,7 +51,7 @@ with ``console.command``:
5451
use AppBundle\Command\MyCommand;
5552
5653
$container
57-
->register('app.command.my_command', MyCommand::class)
54+
->register(MyCommand::class)
5855
->addTag('console.command')
5956
;
6057
@@ -131,8 +128,7 @@ inject the ``command.default_name`` parameter:
131128
command.default_name: Javier
132129
133130
services:
134-
app.command.my_command:
135-
class: AppBundle\Command\MyCommand
131+
AppBundle\Command\MyCommand:
136132
arguments: ["%command.default_name%"]
137133
tags: [console.command]
138134
@@ -150,8 +146,7 @@ inject the ``command.default_name`` parameter:
150146
</parameters>
151147
152148
<services>
153-
<service id="app.command.my_command"
154-
class="AppBundle\Command\MyCommand">
149+
<service class="AppBundle\Command\MyCommand">
155150
<argument>%command.default_name%</argument>
156151
<tag name="console.command" />
157152
</service>
@@ -166,7 +161,7 @@ inject the ``command.default_name`` parameter:
166161
$container->setParameter('command.default_name', 'Javier');
167162
168163
$container
169-
->register('app.command.my_command', MyCommand::class)
164+
->register(MyCommand::class)
170165
->setArguments(array('%command.default_name%'))
171166
->addTag('console.command')
172167
;

controller/argument_value_resolver.rst

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ and adding a priority.
146146
147147
# app/config/services.yml
148148
services:
149-
app.value_resolver.user:
150-
class: AppBundle\ArgumentResolver\UserValueResolver
149+
AppBundle\ArgumentResolver\UserValueResolver:
151150
arguments:
152151
- '@security.token_storage'
153152
tags:
@@ -162,10 +161,8 @@ and adding a priority.
162161
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
163162
164163
<services>
165-
<service id="app.value_resolver.user"
166-
class="AppBundle\ArgumentResolver\UserValueResolver"
167-
>
168-
<argument type="service" id="security.token_storage">
164+
<service id="AppBundle\ArgumentResolver\UserValueResolver">
165+
<argument type="service" id="security.token_storage" />
169166
<tag name="controller.argument_value_resolver" priority="50" />
170167
</service>
171168
</services>
@@ -175,14 +172,12 @@ and adding a priority.
175172
.. code-block:: php
176173
177174
// app/config/services.php
175+
use AppBundle\ArgumentResolver\UserValueResolver;
178176
use Symfony\Component\DependencyInjection\Definition;
179177
180-
$definition = new Definition(
181-
'AppBundle\ArgumentResolver\UserValueResolver',
182-
array(new Reference('security.token_storage'))
183-
);
184-
$definition->addTag('controller.argument_value_resolver', array('priority' => 50));
185-
$container->setDefinition('app.value_resolver.user', $definition);
178+
$container->register(UserValueResolver::class)
179+
->addArgument(new Reference('security.token_storage'))
180+
->addTag('controller.argument_value_resolver', array('priority' => 50));
186181
187182
While adding a priority is optional, it's recommended to add one to make sure
188183
the expected value is injected. The ``RequestAttributeValueResolver`` has a

controller/error_pages.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ In that case, you might want to override one or both of the ``showAction()`` and
269269
270270
# app/config/services.yml
271271
services:
272-
app.exception_controller:
273-
class: AppBundle\Controller\CustomExceptionController
272+
AppBundle\Controller\CustomExceptionController:
274273
arguments: ['@twig', '%kernel.debug%']
275274
276275
.. code-block:: xml
@@ -282,9 +281,7 @@ In that case, you might want to override one or both of the ``showAction()`` and
282281
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
283282
>
284283
<services>
285-
<service id="app.exception_controller"
286-
class="AppBundle\Controller\CustomExceptionController"
287-
>
284+
<service id="AppBundle\Controller\CustomExceptionController">
288285
<argument type="service" id="twig"/>
289286
<argument>%kernel.debug%</argument>
290287
</service>
@@ -297,14 +294,14 @@ In that case, you might want to override one or both of the ``showAction()`` and
297294
use AppBundle\Controller\CustomExceptionController;
298295
use Symfony\Component\DependencyInjection\Reference;
299296
300-
$container->register('app.exception_controller', CustomExceptionController::class)
297+
$container->register(CustomExceptionController::class)
301298
->setArguments(array(
302299
new Reference('twig'),
303300
'%kernel.debug%',
304301
));
305302
306-
And then configure ``twig.exception_controller`` using the controller as
307-
services syntax (e.g. ``app.exception_controller:showAction``).
303+
And then configure ``twig.exception_controller`` to point to this service
304+
(using ``AppBundle:CustomException:showAction`` for example).
308305

309306
.. tip::
310307

doctrine/event_listeners_subscribers.rst

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ managers that use this connection.
4141
memory: true
4242
4343
services:
44-
my.listener:
45-
class: AppBundle\EventListener\SearchIndexer
44+
AppBundle\EventListener\SearchIndexer:
4645
tags:
4746
- { name: doctrine.event_listener, event: postPersist }
48-
my.listener2:
49-
class: AppBundle\EventListener\SearchIndexer2
47+
AppBundle\EventListener\SearchIndexer2:
5048
tags:
5149
- { name: doctrine.event_listener, event: postPersist, connection: default }
52-
my.subscriber:
53-
class: AppBundle\EventListener\SearchIndexerSubscriber
50+
AppBundle\EventListener\SearchIndexerSubscriber:
5451
tags:
5552
- { name: doctrine.event_subscriber, connection: default }
5653
@@ -67,13 +64,13 @@ managers that use this connection.
6764
</doctrine:config>
6865
6966
<services>
70-
<service id="my.listener" class="AppBundle\EventListener\SearchIndexer">
67+
<service id="AppBundle\EventListener\SearchIndexer">
7168
<tag name="doctrine.event_listener" event="postPersist" />
7269
</service>
73-
<service id="my.listener2" class="AppBundle\EventListener\SearchIndexer2">
70+
<service id="AppBundle\EventListener\SearchIndexer2">
7471
<tag name="doctrine.event_listener" event="postPersist" connection="default" />
7572
</service>
76-
<service id="my.subscriber" class="AppBundle\EventListener\SearchIndexerSubscriber">
73+
<service id="AppBundle\EventListener\SearchIndexerSubscriber">
7774
<tag name="doctrine.event_subscriber" connection="default" />
7875
</service>
7976
</services>
@@ -97,28 +94,25 @@ managers that use this connection.
9794
),
9895
));
9996
100-
$container
101-
->register('my.listener', SearchIndexer::class)
97+
$container->register(SearchIndexer::class)
10298
->addTag('doctrine.event_listener', array('event' => 'postPersist'))
10399
;
104-
$container
105-
->register('my.listener2', SearchIndexer2::class)
100+
$container->register(SearchIndexer2::class)
106101
->addTag('doctrine.event_listener', array(
107102
'event' => 'postPersist',
108103
'connection' => 'default'
109104
))
110105
;
111-
$container
112-
->register('my.subscriber', SearchIndexerSubscriber::class)
106+
$container->register(SearchIndexerSubscriber::class)
113107
->addTag('doctrine.event_subscriber', array('connection' => 'default'))
114108
;
115109
116110
Creating the Listener Class
117111
---------------------------
118112

119-
In the previous example, a service ``my.listener`` was configured as a Doctrine
120-
listener on the event ``postPersist``. The class behind that service must have
121-
a ``postPersist()`` method, which will be called when the event is dispatched::
113+
In the previous example, the ``SearchIndexer`` class was configured as a
114+
Doctrine listener on the event ``postPersist``. So it must have a
115+
``postPersist()`` method, which will be called when the event is dispatched::
122116

123117
// src/AppBundle/EventListener/SearchIndexer.php
124118
namespace AppBundle\EventListener;

event_dispatcher.rst

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ using a special "tag":
7777
7878
# app/config/services.yml
7979
services:
80-
app.exception_listener:
81-
class: AppBundle\EventListener\ExceptionListener
80+
AppBundle\EventListener\ExceptionListener:
8281
tags:
8382
- { name: kernel.event_listener, event: kernel.exception }
8483
@@ -91,9 +90,7 @@ using a special "tag":
9190
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
9291
9392
<services>
94-
<service id="app.exception_listener"
95-
class="AppBundle\EventListener\ExceptionListener">
96-
93+
<service id="AppBundle\EventListener\ExceptionListener">
9794
<tag name="kernel.event_listener" event="kernel.exception" />
9895
</service>
9996
</services>
@@ -105,7 +102,7 @@ using a special "tag":
105102
use AppBundle\EventListener\ExceptionListener;
106103
107104
$container
108-
->register('app.exception_listener', ExceptionListener::class)
105+
->register(ExceptionListener::class)
109106
->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
110107
;
111108
@@ -185,10 +182,7 @@ Now, you just need to register the class as a service and add the
185182
186183
# app/config/services.yml
187184
services:
188-
app.exception_subscriber:
189-
class: AppBundle\EventSubscriber\ExceptionSubscriber
190-
tags:
191-
- { name: kernel.event_subscriber }
185+
AppBundle\EventSubscriber\ExceptionSubscriber: [kernel.event_subscriber]
192186
193187
.. code-block:: xml
194188
@@ -199,9 +193,7 @@ Now, you just need to register the class as a service and add the
199193
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
200194
201195
<services>
202-
<service id="app.exception_subscriber"
203-
class="AppBundle\EventSubscriber\ExceptionSubscriber">
204-
196+
<service id="AppBundle\EventSubscriber\ExceptionSubscriber">
205197
<tag name="kernel.event_subscriber"/>
206198
</service>
207199
</services>
@@ -213,7 +205,7 @@ Now, you just need to register the class as a service and add the
213205
use AppBundle\EventSubscriber\ExceptionSubscriber;
214206
215207
$container
216-
->register('app.exception_subscriber', ExceptionSubscriber::class)
208+
->register(ExceptionSubscriber::class)
217209
->addTag('kernel.event_subscriber')
218210
;
219211

0 commit comments

Comments
 (0)