Skip to content

Commit 8974c48

Browse files
committed
Going through more chapters to use types and autowiring
1 parent cbedd79 commit 8974c48

20 files changed

+201
-260
lines changed

controller/argument_value_resolver.rst

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Functionality Shipped with the HttpKernel
1616
-----------------------------------------
1717

1818
.. versionadded:: 3.3
19-
The ``SessionValueResolver`` was introduced in Symfony 3.3.
19+
The ``SessionValueResolver`` and ``ServiceValueResolver`` were both added in Symfony 3.3.
2020

2121
Symfony ships with five value resolvers in the HttpKernel component:
2222

@@ -27,6 +27,10 @@ Symfony ships with five value resolvers in the HttpKernel component:
2727
Injects the current ``Request`` if type-hinted with ``Request`` or a class
2828
extending ``Request``.
2929

30+
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\ServiceValueResolver`
31+
Injects a service if type-hinted with a valid service class or interface. This
32+
works like :doc:`autowiring </service_container/autowiring>`.
33+
3034
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
3135
Injects the configured session class extending ``SessionInterface`` if
3236
type-hinted with ``SessionInterface`` or a class extending
@@ -146,10 +150,10 @@ and adding a priority.
146150
147151
# app/config/services.yml
148152
services:
149-
app.value_resolver.user:
150-
class: AppBundle\ArgumentResolver\UserValueResolver
151-
arguments:
152-
- '@security.token_storage'
153+
# ...
154+
155+
AppBundle\ArgumentResolver\UserValueResolver:
156+
# arguments is not needed if you have autowire above under _defaults
153157
tags:
154158
- { name: controller.argument_value_resolver, priority: 50 }
155159
@@ -160,12 +164,10 @@ and adding a priority.
160164
<container xmlns="http://symfony.com/schema/dic/services"
161165
xmlns:xsi="'http://www.w3.org/2001/XMLSchema-Instance"
162166
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
167+
<!-- ... -->
163168
164169
<services>
165-
<service id="app.value_resolver.user"
166-
class="AppBundle\ArgumentResolver\UserValueResolver"
167-
>
168-
<argument type="service" id="security.token_storage">
170+
<service id="AppBundle\ArgumentResolver\UserValueResolver">
169171
<tag name="controller.argument_value_resolver" priority="50" />
170172
</service>
171173
</services>
@@ -175,14 +177,10 @@ and adding a priority.
175177
.. code-block:: php
176178
177179
// app/config/services.php
178-
use Symfony\Component\DependencyInjection\Definition;
179-
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);
180+
use AppBundle\ArgumentResolver\UserValueResolver;
181+
182+
$container->autowire(UserValueResolver::class)
183+
->addTag('controller.argument_value_resolver', array('priority' => 50));
186184
187185
While adding a priority is optional, it's recommended to add one to make sure
188186
the expected value is injected. The ``RequestAttributeValueResolver`` has a

controller/error_pages.rst

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ 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
274-
arguments: ['@twig', '%kernel.debug%']
272+
# ...
273+
274+
AppBundle\Controller\CustomExceptionController:
275+
arguments:
276+
$debug: '%kernel.debug%'
275277
276278
.. code-block:: xml
277279
@@ -282,11 +284,10 @@ In that case, you might want to override one or both of the ``showAction()`` and
282284
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
283285
>
284286
<services>
285-
<service id="app.exception_controller"
286-
class="AppBundle\Controller\CustomExceptionController"
287-
>
288-
<argument type="service" id="twig"/>
289-
<argument>%kernel.debug%</argument>
287+
<!-- ... -->
288+
289+
<service id="AppBundle\Controller\CustomExceptionController">
290+
<argument key="$debug">%kernel.debug%</argument>
290291
</service>
291292
</services>
292293
</container>
@@ -295,17 +296,9 @@ In that case, you might want to override one or both of the ``showAction()`` and
295296
296297
// app/config/services.php
297298
use AppBundle\Controller\CustomExceptionController;
298-
use Symfony\Component\DependencyInjection\Reference;
299-
use Symfony\Component\DependencyInjection\Definition;
300-
301-
$definition = new Definition(CustomExceptionController::class, array(
302-
new Reference('twig'),
303-
'%kernel.debug%'
304-
));
305-
$container->setDefinition('app.exception_controller', $definition);
306299
307-
And then configure ``twig.exception_controller`` using the controller as
308-
services syntax (e.g. ``app.exception_controller:showAction``).
300+
$$container->autowire(CustomExceptionController::class)
301+
setArgument('$debug', '%kernel.debug%');
309302
310303
.. tip::
311304

controller/soap_web_service.rst

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ which represents the functionality that you'll expose in your SOAP service.
2424
In this case, the SOAP service will allow the client to call a method called
2525
``hello``, which happens to send an email::
2626

27-
// src/Acme/SoapBundle/Services/HelloService.php
28-
namespace Acme\SoapBundle\Services;
27+
// src/AppBundle/Service/HelloService.php
28+
namespace AppBundle\Service;
2929

3030
class HelloService
3131
{
@@ -50,56 +50,68 @@ In this case, the SOAP service will allow the client to call a method called
5050
}
5151
}
5252

53-
Next, you can train Symfony to be able to create an instance of this class.
54-
Since the class sends an email, it's been designed to accept a ``Swift_Mailer``
55-
instance. Using the Service Container, you can configure Symfony to construct
56-
a ``HelloService`` object properly:
53+
Next, make sure that your new class is registered as a service. If you use
54+
:doc:`autowiring </service_container/autowiring>` (enabled by default), this is easy:
5755

5856
.. configuration-block::
5957

6058
.. code-block:: yaml
6159
6260
# app/config/services.yml
6361
services:
64-
hello_service:
65-
class: Acme\SoapBundle\Services\HelloService
66-
arguments: ['@mailer']
62+
_defaults:
63+
# be sure autowire is enabled
64+
autowire: true
65+
66+
# load services from the Service directory
67+
AppBundle\:
68+
resource: '../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}'
6769
6870
.. code-block:: xml
6971
7072
<!-- app/config/services.xml -->
71-
<services>
72-
<service id="hello_service" class="Acme\SoapBundle\Services\HelloService">
73-
<argument type="service" id="mailer"/>
74-
</service>
75-
</services>
73+
<?xml version="1.0" encoding="UTF-8" ?>
74+
<container xmlns="http://symfony.com/schema/dic/services"
75+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
76+
xsi:schemaLocation="http://symfony.com/schema/dic/services
77+
http://symfony.com/schema/dic/services/services-1.0.xsd">
78+
79+
<services>
80+
<defaults autowire="true" autoconfigure="true" />
81+
82+
<!-- load services from the Service directory -->
83+
<prototype namespace="AppBundle\" resource="../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}" />
84+
</services>
85+
</container>
7686
7787
.. code-block:: php
7888
7989
// app/config/services.php
80-
use Acme\SoapBundle\Services\HelloService;
90+
use AppBundle\Service\HelloService;
8191
82-
$container
83-
->register('hello_service', HelloService::class)
84-
->addArgument(new Reference('mailer'));
92+
$container->autowire(HelloService::class)
93+
->setPublic(false);
8594
8695
Below is an example of a controller that is capable of handling a SOAP
87-
request. If ``indexAction()`` is accessible via the route ``/soap``, then the
88-
WSDL document can be retrieved via ``/soap?wsdl``.
89-
90-
.. code-block:: php
96+
request. BEcause ``indexAction()`` is accessible via ``/soap``, the WSDL document
97+
can be retrieved via ``/soap?wsdl``::
9198

92-
namespace Acme\SoapBundle\Controller;
99+
namespace AppBundle\Controller;
93100

94101
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
95102
use Symfony\Component\HttpFoundation\Response;
103+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
104+
use AppBundle\Service\HelloService;
96105

97106
class HelloServiceController extends Controller
98107
{
99-
public function indexAction()
108+
/**
109+
* @Route("/soap")
110+
*/
111+
public function indexAction(HelloService $helloService)
100112
{
101113
$server = new \SoapServer('/path/to/hello.wsdl');
102-
$server->setObject($this->get('hello_service'));
114+
$server->setObject($helloService);
103115

104116
$response = new Response();
105117
$response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');

controller/upload_file.rst

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ Creating an Uploader Service
221221
To avoid logic in controllers, making them big, you can extract the upload
222222
logic to a separate service::
223223

224-
// src/AppBundle/FileUploader.php
225-
namespace AppBundle;
224+
// src/AppBundle/Service\FileUploader.php
225+
namespace AppBundle\Service;
226226

227227
use Symfony\Component\HttpFoundation\File\UploadedFile;
228228

@@ -259,50 +259,50 @@ Then, define a service for this class:
259259
# app/config/services.yml
260260
services:
261261
# ...
262-
app.brochure_uploader:
263-
class: AppBundle\FileUploader
264-
arguments: ['%brochures_directory%']
262+
263+
AppBundle\Service\FileUploader:
264+
arguments:
265+
$targetDir: '%brochures_directory%'
265266
266267
.. code-block:: xml
267268
268-
<!-- app/config/config.xml -->
269+
<!-- app/config/services.xml -->
269270
<?xml version="1.0" encoding="UTF-8" ?>
270271
<container xmlns="http://symfony.com/schema/dic/services"
271272
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
272273
xsi:schemaLocation="http://symfony.com/schema/dic/services
273-
http://symfony.com/schema/dic/services/services-1.0.xsd"
274-
>
275-
<!-- ... -->
274+
http://symfony.com/schema/dic/services/services-1.0.xsd">
276275
277-
<service id="app.brochure_uploader" class="AppBundle\FileUploader">
278-
<argument>%brochures_directory%</argument>
279-
</service>
276+
<services>
277+
<!-- ... -->
278+
279+
<service id="AppBundle\Service\FileUploader">
280+
<argument key="$targetDir">%brochures_directory%</argument>
281+
</service>
282+
</services>
280283
</container>
281284
282285
.. code-block:: php
283286
284287
// app/config/services.php
285-
use AppBundle\FileUploader;
286-
use Symfony\Component\DependencyInjection\Definition;
288+
use AppBundle\Service\FileUploader;
287289
288-
// ...
289-
$container->setDefinition('app.brochure_uploader', new Definition(
290-
FileUploader::class,
291-
array('%brochures_directory%')
292-
));
290+
$container->autowire(FileUploader::class)
291+
->setArgument('$targetDir', '%brochures_directory%');
293292
294293
Now you're ready to use this service in the controller::
295294

296295
// src/AppBundle/Controller/ProductController.php
296+
use AppBundle\Service\FileUploader;
297297

298298
// ...
299-
public function newAction(Request $request)
299+
public function newAction(Request $request, FileUploader $fileUploader)
300300
{
301301
// ...
302302

303303
if ($form->isSubmitted() && $form->isValid()) {
304304
$file = $product->getBrochure();
305-
$fileName = $this->get('app.brochure_uploader')->upload($file);
305+
$fileName = $fileUploader->upload($file);
306306

307307
$product->setBrochure($fileName);
308308

@@ -378,10 +378,12 @@ Now, register this class as a Doctrine listener:
378378
379379
# app/config/services.yml
380380
services:
381+
_defaults:
382+
# make sure you have autowire enabled
383+
autowire: true
381384
# ...
382-
app.doctrine_brochure_listener:
383-
class: AppBundle\EventListener\BrochureUploadListener
384-
arguments: ['@app.brochure_uploader']
385+
386+
AppBundle\EventListener\BrochureUploadListener:
385387
tags:
386388
- { name: doctrine.event_listener, event: prePersist }
387389
- { name: doctrine.event_listener, event: preUpdate }
@@ -397,11 +399,7 @@ Now, register this class as a Doctrine listener:
397399
>
398400
<!-- ... -->
399401
400-
<service id="app.doctrine_brochure_listener"
401-
class="AppBundle\EventListener\BrochureUploaderListener"
402-
>
403-
<argument type="service" id="app.brochure_uploader"/>
404-
402+
<service id="AppBundle\EventListener\BrochureUploaderListener">
405403
<tag name="doctrine.event_listener" event="prePersist"/>
406404
<tag name="doctrine.event_listener" event="preUpdate"/>
407405
</service>
@@ -411,21 +409,15 @@ Now, register this class as a Doctrine listener:
411409
412410
// app/config/services.php
413411
use AppBundle\EventListener\BrochureUploaderListener;
414-
use Symfony\Component\DependencyInjection\Definition;
415-
use Symfony\Component\DependencyInjection\Reference;
416412
417-
// ...
418-
$definition = new Definition(
419-
BrochureUploaderListener::class,
420-
array(new Reference('brochures_directory'))
421-
);
422-
$definition->addTag('doctrine.event_listener', array(
423-
'event' => 'prePersist',
424-
));
425-
$definition->addTag('doctrine.event_listener', array(
426-
'event' => 'preUpdate',
427-
));
428-
$container->setDefinition('app.doctrine_brochure_listener', $definition);
413+
$container->autowire(BrochureUploaderListener::class)
414+
->addTag('doctrine.event_listener', array(
415+
'event' => 'prePersist',
416+
))
417+
->addTag('doctrine.event_listener', array(
418+
'event' => 'preUpdate',
419+
))
420+
;
429421
430422
This listener is now automatically executed when persisting a new Product
431423
entity. This way, you can remove everything related to uploading from the

0 commit comments

Comments
 (0)