Skip to content

Commit f347595

Browse files
committed
Reword
1 parent 16b2bca commit f347595

File tree

1 file changed

+77
-76
lines changed

1 file changed

+77
-76
lines changed

service_container/tags.rst

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -919,109 +919,110 @@ array element. For example, to retrieve the ``handler_two`` handler::
919919
}
920920
}
921921

922-
.. tip::
922+
You can omit the index attribute (``key`` in the previous example) by setting
923+
the ``index_by`` attribute on the ``tagged_iterator`` tag. In this case, you
924+
must define a static method whose name follows the pattern:
925+
``getDefault<CamelCase index_by value>Name``.
923926

924-
Just like the priority, if you set the attribute (``index_by``) on the :tagged_iterator, you can also implement a static
925-
``getDefault(``index_by``)Name()`` method in the handlers and omit the
926-
index attribute (``key``)::
927-
927+
For example, if ``index_by`` is ``handler``, the method name must be
928+
``getDefaultHandlerName()``:
928929

929-
.. code-block:: yaml
930-
931-
# config/services.yaml
932-
services:
933-
# ...
930+
.. code-block:: yaml
934931
935-
App\HandlerCollection:
936-
arguments: [!tagged_iterator { tag: 'app.handler', index_by: 'handler' }]
937-
938-
.. code-block:: php
932+
# config/services.yaml
933+
services:
934+
# ...
935+
936+
App\HandlerCollection:
937+
arguments: [!tagged_iterator { tag: 'app.handler', index_by: 'handler' }]
939938
940-
// src/Handler/One.php
941-
namespace App\Handler;
939+
.. code-block:: php
942940
943-
class One
941+
// src/Handler/One.php
942+
namespace App\Handler;
943+
944+
class One
945+
{
946+
// ...
947+
public static function getDefaultHandlerName(): string
944948
{
945-
// ...
946-
public static function getDefaultHandlerName(): string
947-
{
948-
return 'handler_one';
949-
}
949+
return 'handler_one';
950950
}
951+
}
951952
952-
You also can define the name of the static method to implement on each service
953-
with the ``default_index_method`` attribute on the tagged argument:
953+
You also can define the name of the static method to implement on each service
954+
with the ``default_index_method`` attribute on the tagged argument:
954955

955-
.. configuration-block::
956+
.. configuration-block::
956957

957-
.. code-block:: php-attributes
958+
.. code-block:: php-attributes
958959
959-
// src/HandlerCollection.php
960-
namespace App;
960+
// src/HandlerCollection.php
961+
namespace App;
961962
962-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
963+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
963964
964-
class HandlerCollection
965-
{
966-
public function __construct(
967-
#[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
968-
iterable $handlers
969-
) {
970-
}
965+
class HandlerCollection
966+
{
967+
public function __construct(
968+
#[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
969+
iterable $handlers
970+
) {
971971
}
972+
}
972973
973-
.. code-block:: yaml
974+
.. code-block:: yaml
974975
975-
# config/services.yaml
976-
services:
977-
# ...
976+
# config/services.yaml
977+
services:
978+
# ...
978979
979-
App\HandlerCollection:
980-
# use getIndex() instead of getDefaultIndexName()
981-
arguments: [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
980+
App\HandlerCollection:
981+
# use getIndex() instead of getDefaultIndexName()
982+
arguments: [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
982983
983-
.. code-block:: xml
984+
.. code-block:: xml
984985
985-
<!-- config/services.xml -->
986-
<?xml version="1.0" encoding="UTF-8" ?>
987-
<container xmlns="http://symfony.com/schema/dic/services"
988-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
989-
xsi:schemaLocation="http://symfony.com/schema/dic/services
990-
https://symfony.com/schema/dic/services/services-1.0.xsd">
986+
<!-- config/services.xml -->
987+
<?xml version="1.0" encoding="UTF-8" ?>
988+
<container xmlns="http://symfony.com/schema/dic/services"
989+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
990+
xsi:schemaLocation="http://symfony.com/schema/dic/services
991+
https://symfony.com/schema/dic/services/services-1.0.xsd">
991992
992-
<services>
993-
<!-- ... -->
993+
<services>
994+
<!-- ... -->
994995
995-
<service id="App\HandlerCollection">
996-
<!-- use getIndex() instead of getDefaultIndexName() -->
997-
<argument type="tagged_iterator"
998-
tag="app.handler"
999-
default-index-method="someFunctionName"
1000-
/>
1001-
</service>
1002-
</services>
1003-
</container>
996+
<service id="App\HandlerCollection">
997+
<!-- use getIndex() instead of getDefaultIndexName() -->
998+
<argument type="tagged_iterator"
999+
tag="app.handler"
1000+
default-index-method="someFunctionName"
1001+
/>
1002+
</service>
1003+
</services>
1004+
</container>
10041005
1005-
.. code-block:: php
1006+
.. code-block:: php
10061007
1007-
// config/services.php
1008-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1008+
// config/services.php
1009+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
10091010
1010-
use App\HandlerCollection;
1011-
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1011+
use App\HandlerCollection;
1012+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
10121013
1013-
return function (ContainerConfigurator $container) {
1014-
$services = $container->services();
1014+
return function (ContainerConfigurator $container) {
1015+
$services = $container->services();
10151016
1016-
// ...
1017+
// ...
10171018
1018-
// use getIndex() instead of getDefaultIndexName()
1019-
$services->set(HandlerCollection::class)
1020-
->args([
1021-
tagged_iterator('app.handler', null, 'getIndex'),
1022-
])
1023-
;
1024-
};
1019+
// use getIndex() instead of getDefaultIndexName()
1020+
$services->set(HandlerCollection::class)
1021+
->args([
1022+
tagged_iterator('app.handler', null, 'getIndex'),
1023+
])
1024+
;
1025+
};
10251026
10261027
.. _tags_as-tagged-item:
10271028

0 commit comments

Comments
 (0)