Skip to content

Commit 5fc9ffe

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

12 files changed

+113
-218
lines changed

best_practices/templates.rst

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,11 @@ 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

90-
namespace AppBundle\Utils;
80+
namespace AppBundle\Service;
9181

9282
class Markdown
9383
{
@@ -114,7 +104,7 @@ service in the constructor of the Twig extension:
114104
115105
namespace AppBundle\Twig;
116106
117-
use AppBundle\Utils\Markdown;
107+
use AppBundle\Service\Markdown;
118108
119109
class AppExtension extends \Twig_Extension
120110
{
@@ -147,18 +137,21 @@ 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
159-
arguments: ['@app.markdown']
160-
public: false
161-
tags: [twig.extension]
146+
_defaults:
147+
# ... be sure autowiring and autoconfiguring are enabled
148+
autowire: true
149+
autoconfigure: true
150+
# ...
151+
152+
# add Service/ to the list of directories to load services from
153+
AppBundle\:
154+
resource: '../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}'
162155
163156
.. _`Twig`: http://twig.sensiolabs.org/
164157
.. _`Parsedown`: http://parsedown.org/

console/commands_as_services.rst

Lines changed: 6 additions & 12 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>
@@ -53,8 +50,7 @@ with ``console.command``:
5350
// app/config/config.php
5451
use AppBundle\Command\MyCommand;
5552
56-
$container
57-
->register('app.command.my_command', MyCommand::class)
53+
$container->register(MyCommand::class)
5854
->addTag('console.command')
5955
;
6056
@@ -131,8 +127,7 @@ inject the ``command.default_name`` parameter:
131127
command.default_name: Javier
132128
133129
services:
134-
app.command.my_command:
135-
class: AppBundle\Command\MyCommand
130+
AppBundle\Command\MyCommand:
136131
arguments: ["%command.default_name%"]
137132
tags: [console.command]
138133
@@ -150,8 +145,7 @@ inject the ``command.default_name`` parameter:
150145
</parameters>
151146
152147
<services>
153-
<service id="app.command.my_command"
154-
class="AppBundle\Command\MyCommand">
148+
<service class="AppBundle\Command\MyCommand">
155149
<argument>%command.default_name%</argument>
156150
<tag name="console.command" />
157151
</service>
@@ -166,7 +160,7 @@ inject the ``command.default_name`` parameter:
166160
$container->setParameter('command.default_name', 'Javier');
167161
168162
$container
169-
->register('app.command.my_command', MyCommand::class)
163+
->register(MyCommand::class)
170164
->setArguments(array('%command.default_name%'))
171165
->addTag('console.command')
172166
;

event_dispatcher/before_after_filters.rst

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,15 @@ your listener to be called just before any controller is executed.
155155
156156
# app/config/services.yml
157157
services:
158-
app.tokens.action_listener:
159-
class: AppBundle\EventListener\TokenListener
158+
AppBundle\EventListener\TokenListener:
160159
arguments: ['%tokens%']
161160
tags:
162161
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
163162
164163
.. code-block:: xml
165164
166165
<!-- app/config/services.xml -->
167-
<service id="app.tokens.action_listener" class="AppBundle\EventListener\TokenListener">
166+
<service id="AppBundle\EventListener\TokenListener">
168167
<argument>%tokens%</argument>
169168
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" />
170169
</service>
@@ -174,14 +173,14 @@ your listener to be called just before any controller is executed.
174173
// app/config/services.php
175174
use AppBundle\EventListener\TokenListener;
176175
177-
$container->register('app.tokens.action_listener', TokenListener::class)
176+
$container->register(TokenListener::class)
178177
->addArgument('%tokens%')
179178
->addTag('kernel.event_listener', array(
180179
'event' => 'kernel.controller',
181180
'method' => 'onKernelController',
182181
));
183182
184-
With this configuration, your ``TokenListener`` ``onKernelController()`` method
183+
With this configuration, the ``TokenListener::onKernelController()`` method
185184
will be executed on each request. If the controller that is about to be executed
186185
implements ``TokenAuthenticatedController``, token authentication is
187186
applied. This lets you have a "before" filter on any controller that you
@@ -250,8 +249,7 @@ event:
250249
251250
# app/config/services.yml
252251
services:
253-
app.tokens.action_listener:
254-
class: AppBundle\EventListener\TokenListener
252+
AppBundle\EventListener\TokenListener:
255253
arguments: ['%tokens%']
256254
tags:
257255
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
@@ -260,7 +258,7 @@ event:
260258
.. code-block:: xml
261259
262260
<!-- app/config/services.xml -->
263-
<service id="app.tokens.action_listener" class="AppBundle\EventListener\TokenListener">
261+
<service id="AppBundle\EventListener\TokenListener">
264262
<argument>%tokens%</argument>
265263
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" />
266264
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
@@ -271,7 +269,7 @@ event:
271269
// app/config/services.php
272270
use AppBundle\EventListener\TokenListener;
273271
274-
$container->register('app.tokens.action_listener', TokenListener::class)
272+
$container->register(TokenListener::class)
275273
->addArgument('%tokens%')
276274
->addTag('kernel.event_listener', array(
277275
'event' => 'kernel.controller',

form/create_custom_field_type.rst

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,37 +303,31 @@ the ``genders`` parameter value as the first argument to its to-be-created
303303

304304
.. code-block:: yaml
305305
306-
# src/AppBundle/Resources/config/services.yml
306+
# app/config/services.yml
307307
services:
308-
app.form.type.gender:
309-
class: AppBundle\Form\Type\GenderType
308+
AppBundle\Form\Type\GenderType:
310309
arguments:
311310
- '%genders%'
312311
tags: [form.type]
313312
314313
.. code-block:: xml
315314
316-
<!-- src/AppBundle/Resources/config/services.xml -->
317-
<service id="app.form.type.gender" class="AppBundle\Form\Type\GenderType">
315+
<!-- app/config/services.xml -->
316+
<service id="AppBundle\Form\Type\GenderType">
318317
<argument>%genders%</argument>
319318
<tag name="form.type" />
320319
</service>
321320
322321
.. code-block:: php
323322
324-
// src/AppBundle/Resources/config/services.php
323+
// app/config/services.php
325324
use AppBundle\Form\Type\GenderType;
326325
327-
$container->register('app.form.type.gender', GenderType::class)
326+
$container->register(GenderType::class)
328327
->addArgument('%genders%')
329328
->addTag('form.type')
330329
;
331330
332-
.. tip::
333-
334-
Make sure the services file is being imported. See :ref:`service-container-imports-directive`
335-
for details.
336-
337331
.. versionadded:: 3.3
338332
Prior to Symfony 3.3, you needed to define form type services as ``public``.
339333
Starting from Symfony 3.3, you can also define them as ``private``.

form/create_form_type_extension.rst

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,13 @@ tag:
102102
.. code-block:: yaml
103103
104104
services:
105-
app.image_type_extension:
106-
class: AppBundle\Form\Extension\ImageTypeExtension
105+
AppBundle\Form\Extension\ImageTypeExtension:
107106
tags:
108107
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FileType }
109108
110109
.. code-block:: xml
111110
112-
<service id="app.image_type_extension"
113-
class="AppBundle\Form\Extension\ImageTypeExtension"
114-
>
111+
<service id="AppBundle\Form\Extension\ImageTypeExtension">
115112
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FileType" />
116113
</service>
117114
@@ -121,7 +118,7 @@ tag:
121118
use Symfony\Component\Form\Extension\Core\Type\FileType;
122119
123120
$container
124-
->register('app.image_type_extension', ImageTypeExtension::class)
121+
->register(ImageTypeExtension::class)
125122
->addTag('form.type_extension', array(
126123
'extended_type' => FileType::class
127124
))
@@ -137,10 +134,10 @@ field type, you will use that as the ``extended_type``.
137134

138135
.. tip::
139136

140-
There is an optional tag attribute called ``priority``, which
141-
defaults to ``0`` and controls the order in which the form
142-
type extensions are loaded (the higher the priority, the earlier
143-
an extension is loaded). This is useful when you need to guarantee
137+
There is an optional tag attribute called ``priority``, which
138+
defaults to ``0`` and controls the order in which the form
139+
type extensions are loaded (the higher the priority, the earlier
140+
an extension is loaded). This is useful when you need to guarantee
144141
that one extension is loaded before or after another extension.
145142

146143
.. versionadded:: 3.2

form/data_transformers.rst

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ Define the form type as a service in your configuration files.
297297
298298
# src/AppBundle/Resources/config/services.yml
299299
services:
300-
app.form.type.task:
301-
class: AppBundle\Form\Type\TaskType
300+
AppBundle\Form\Type\TaskType:
302301
arguments: ["@doctrine.orm.entity_manager"]
303302
tags: [form.type]
304303
@@ -311,7 +310,7 @@ Define the form type as a service in your configuration files.
311310
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
312311
313312
<services>
314-
<service id="app.form.type.task" class="AppBundle\Form\Type\TaskType">
313+
<service id="AppBundle\Form\Type\TaskType">
315314
<tag name="form.type" />
316315
<argument type="service" id="doctrine.orm.entity_manager"></argument>
317316
</service>
@@ -322,15 +321,10 @@ Define the form type as a service in your configuration files.
322321
323322
// src/AppBundle/Resources/config/services.php
324323
use AppBundle\Form\Type\TaskType;
324+
use Symfony\Component\DependencyInjection\Reference;
325325
326-
$definition = new Definition(TaskType::class, array(
327-
new Reference('doctrine.orm.entity_manager'),
328-
));
329-
$container
330-
->setDefinition(
331-
'app.form.type.task',
332-
$definition
333-
)
326+
$container->register(TaskType::class)
327+
->addArgument(new Reference('doctrine.orm.entity_manager'))
334328
->addTag('form.type')
335329
;
336330
@@ -430,8 +424,7 @@ it's recognized as a custom field type:
430424
431425
# app/config/services.yml
432426
services:
433-
app.type.issue_selector:
434-
class: AppBundle\Form\IssueSelectorType
427+
AppBundle\Form\IssueSelectorType:
435428
arguments: ['@doctrine.orm.entity_manager']
436429
tags: [form.type]
437430
@@ -446,8 +439,7 @@ it's recognized as a custom field type:
446439
http://symfony.com/schema/dic/services/services-1.0.xsd">
447440
448441
<services>
449-
<service id="app.type.issue_selector"
450-
class="AppBundle\Form\IssueSelectorType">
442+
<service id="AppBundle\Form\IssueSelectorType">
451443
<argument type="service" id="doctrine.orm.entity_manager"/>
452444
<tag name="form.type" />
453445
</service>
@@ -461,7 +453,7 @@ it's recognized as a custom field type:
461453
use Symfony\Component\DependencyInjection\Reference;
462454
// ...
463455
464-
$container->register('app.type.issue_selector', IssueSelectorType::class)
456+
$container->register(IssueSelectorType::class)
465457
->addArgument(new Reference('doctrine.orm.entity_manager'))
466458
->addTag('form.type');
467459

form/dynamic_form_modification.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,15 @@ you need to register it as a service and tag it with :ref:`form.type <dic-tags-f
341341
342342
# app/config/config.yml
343343
services:
344-
app.form.friend_message:
345-
class: AppBundle\Form\Type\FriendMessageFormType
344+
AppBundle\Form\Type\FriendMessageFormType:
346345
arguments: ['@security.token_storage']
347346
tags: [form.type]
348347
349348
.. code-block:: xml
350349
351350
<!-- app/config/config.xml -->
352351
<services>
353-
<service id="app.form.friend_message" class="AppBundle\Form\Type\FriendMessageFormType">
352+
<service id="AppBundle\Form\Type\FriendMessageFormType">
354353
<argument type="service" id="security.token_storage" />
355354
<tag name="form.type" />
356355
</service>
@@ -362,7 +361,7 @@ you need to register it as a service and tag it with :ref:`form.type <dic-tags-f
362361
use AppBundle\Form\Type\FriendMessageFormType;
363362
use Symfony\Component\DependencyInjection\Reference;
364363
365-
$container->register('app.form.friend_message', FriendMessageFormType::class)
364+
$container->register(FriendMessageFormType::class)
366365
->addArgument(new Reference('security.token_storage'))
367366
->addTag('form.type');
368367

form/form_dependencies.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ Next, register this as a service and tag it with ``form.type``:
9999
100100
# src/AppBundle/Resources/config/services.yml
101101
services:
102-
app.form.type.task:
103-
class: AppBundle\Form\TaskType
102+
AppBundle\Form\TaskType:
104103
arguments: ['@doctrine.orm.entity_manager']
105104
tags: [form.type]
106105
@@ -113,7 +112,7 @@ Next, register this as a service and tag it with ``form.type``:
113112
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
114113
115114
<services>
116-
<service id="app.form.type.task" class="AppBundle\Form\TaskType">
115+
<service id="AppBundle\Form\TaskType">
117116
<argument type="service" id="doctrine.orm.entity_manager"/>
118117
<tag name="form.type" />
119118
</service>
@@ -126,7 +125,7 @@ Next, register this as a service and tag it with ``form.type``:
126125
use AppBundle\Form\TaskType;
127126
use Symfony\Component\DependencyInjection\Reference;
128127
129-
$container->register('app.form.type.task', TaskType::class)
128+
$container->register(TaskType::class)
130129
->addArgument(new Reference('doctrine.orm.entity_manager'))
131130
->addTag('form.type')
132131
;

0 commit comments

Comments
 (0)