8000 Merge branch '4.2' · symfony/symfony-docs@ebf693b · GitHub
[go: up one dir, main page]

Skip to content

Commit ebf693b

Browse files
committed
Merge branch '4.2'
* 4.2: minor #11386 Update cache.rst (poliveras) [Cache] Syntax fix Update cache.rst Document how to use named aliases
2 parents 13c92ff + cf524db commit ebf693b

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

cache.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ of:
5151
provider then a service is automatically created.
5252

5353
There are two pools that are always enabled by default. They are ``cache.app`` and
54-
``cache.system``. The system cache is use for things like annotations, serializer,
54+
``cache.system``. The system cache is used for things like annotations, serializer,
5555
and validation. The ``cache.app`` can be used in your code. You can configure which
5656
adapter (template) they use by using the ``app`` and ``system`` key like:
5757

@@ -329,7 +329,7 @@ For advanced configurations it could sometimes be useful to use a pool as an ada
329329
Custom Provider Options
330330
-----------------------
331331

332-
Some providers have specific options that could be configured. The
332+
Some providers have specific options that can be configured. The
333333
:doc:`RedisAdapter </components/cache/adapters/redis_adapter>` allows you to
334334
create providers with option ``timeout``, ``retry_interval``. etc. To use these
335335
options with non-default values you need to create your own ``\Redis`` provider
@@ -355,7 +355,7 @@ and use that when configuring the pool.
355355
- 'redis://localhost'
356356
- [ retry_interval: 2, timeout: 10 ]
357357
358-
.. code-block:: xml
358+
.. code-block:: xml
359359
360360
<!-- config/packages/cache.xml -->
361361
<?xml version="1.0" encoding="UTF-8" ?>
@@ -406,7 +406,7 @@ and use that when configuring the pool.
406406
Creating a Cache Chain
407407
----------------------
408408

409-
Different cache adapters has different strengths and weaknesses. Some might be really
409+
Different cache adapters have different strengths and weaknesses. Some might be really
410410
quick but small and some may be able to contain a lot of data but are quite slow.
411411
To get the best of both worlds you may use a chain of adapters. The idea is to
412412
first look at the quick adapter and then move on to slower adapters. In the worst

service_container/autowiring.rst

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,40 @@ If you register this as a service, you now have *two* services that implement th
363363
which one to use. Remember, autowiring isn't magic; it looks for a service
364364
whose id matches the type-hint. So you need to choose one by creating an alias
365365
from the type to the correct service id (see :ref:`autowiring-interface-alias`).
366+
Additionally, you can define several named aliases if you want to use
367+
one implementation in some cases, and another implementation in some
368+
other cases.
366369

367-
If you want ``Rot13Transformer`` to be the service that's used for autowiring, create
368-
that alias:
370+
371+
For instance, you may want to use by default the ``Rot13Transformer``
372+
implementation by default when the ``TransformerInterface`` interface is
373+
type hinted, but use the ``UppercaseTransformer`` implementation in some
374+
specific cases. To do so, you can create a normal alias from the
375+
``TransformerInterface`` interface to ``Rot13Transformer``, and then
376+
create a *named alias* from a special string containing the interface
377+
followed by a variable name matching the one you use when doing the
378+
injection::
379+
380+
namespace App\Service;
381+
382+
use App\Util\TransformerInterface;
383+
384+
class MastodonClient
385+
{
386+
private $transformer;
387+
388+
public function __construct(TransformerInterface $shoutyTransformer)
389+
{
390+
$this->transformer = $shoutyTransformer;
391+
}
392+
393+
public function toot($user, $key, $status)
394+
{
395+
$transformedStatus = $this->transformer->transform($status);
396+
397+
// ... connect to Mastodon and send the transformed status
398+
}
399+
}
369400

370401
.. configuration-block::
371402

@@ -378,15 +409,22 @@ that alias:
378409
App\Util\Rot13Transformer: ~
379410
App\Util\UppercaseTransformer: ~
380411
381-
# the ``App\Util\Rot13Transformer`` service will be injected when
382-
# a ``App\Util\TransformerInterface`` type-hint is detected
412+
# the ``App\Util\UppercaseTransformer`` service will be
413+
# injected when an ``App\Util\TransformerInterface``
414+
# type-hint for a ``$shoutyTransformer`` argument is detected.
383415
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
384416
417+
# If the argument used for injection does not match, but the
418+
# type-hint still matches, the ``App\Util\Rot13Transformer``
419+
# service will be injected.
420+
App\Util\TransformerInterface $shoutyTransformer: '@App\Util\UppercaseTransformer'
421+
385422
App\Service\TwitterClient:
386423
# the Rot13Transformer will be passed as the $transformer argument
387424
autowire: true
388425
389-
# If you wanted to choose the non-default service, wire it manually
426+
# If you wanted to choose the non-default service and
427+
# do not want to use a named alias, wire it manually
390428
# arguments:
391429
# $transformer: '@App\Util\UppercaseTransformer'
392430
# ...
@@ -405,6 +443,9 @@ that alias:
405443
<service id="App\Util\UppercaseTransformer"/>
406444
407445
<service id="App\Util\TransformerInterface" alias="App\Util\Rot13Transformer"/>
446+
<service
447+
id="App\Util\TransformerInterface $shoutyTransformer"
448+
alias="App\Util\UppercaseTransformer"/>
408449
409450
<service id="App\Service\TwitterClient" autowire="true">
410451
<!-- <argument key="$transformer" type="service" id="App\Util\UppercaseTransformer"/> -->
@@ -418,21 +459,33 @@ that alias:
418459
use App\Util\Rot13Transformer;
419460
use App\Util\UppercaseTransformer;
420461
use App\Util\TransformerInterface;
462+
use App\Service\MastodonClient;
421463
use App\Service\TwitterClient;
422464
423465
// ...
424466
$container->autowire(Rot13Transformer::class);
425467
$container->autowire(UppercaseTransformer::class);
426468
$container->setAlias(TransformerInterface::class, Rot13Transformer::class);
469+
$container->setAlias(
470+
TransformerInterface::class.' $shoutyTransformer',
471+
UppercaseTransformer::class
472+
);
427473
$container->autowire(TwitterClient::class)
428474
//->setArgument('$transformer', new Reference(UppercaseTransformer::class))
429475
;
476+
$container->autowire(MastodonClient::class);
430477
431478
Thanks to the ``App\Util\TransformerInterface`` alias, any argument type-hinted
432479
with this interface will be passed the ``App\Util\Rot13Transformer`` service.
433-
But, you can also manually wire the *other* service by specifying the argument
480+
If the argument is named ``$shoutyTransformer``,
481< 7D6E /td>+
``App\Util\UppercaseTransformer`` will be used instead.
482+
But, you can also manually wire any *other* service by specifying the argument
434483
under the arguments key.
435484

485+
.. versionadded:: 4.2
486+
487+
Named aliases have been introduced in Symfony 4.2.
488+
436489
Fixing Non-Autowireable Arguments
437490
---------------------------------
438491

0 commit comments

Comments
 (0)
0