Skip to content

Commit 0ae7958

Browse files
feature #22200 [DI] Reference tagged services in config (ro0NL)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Reference tagged services in config | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12269 | License | MIT | Doc PR | symfony/symfony-docs#8404 This is a proof of concept to reference a sequence of tagged services. The problem bugs me for some time, and at first i thought the solution was to have some super generic compiler pass. If it could replace a lot of compilers in core.. perhaps worth it, but eventually each tag comes with it's own logic, including how to deal with tag attributes. However, writing the passes over and over again becomes tedious for the most basic usecase. So given the recent developments, this idea came to mind. ```yml services: a: class: stdClass properties: { a: true } tags: [foo] b: class: stdClass properties: { b: true } tags: [foo] c: class: stdClass properties: #stds: !tagged_services foo (see #22198) stds: !tagged_services foo ``` ``` dump(iterator_to_array($this->get('c')->stds)); ``` ``` array:2 [▼ 0 => {#5052 ▼ +"a": true } 1 => {#4667 ▼ +"b": true } ] ``` Given the _basic_ example at https://symfony.com/doc/current/service_container/tags.html, this could replace that. Any thoughts? Commits ------- 979e58f [DI] Reference tagged services in config
2 parents 8903998 + 83acd5a commit 0ae7958

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Inline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ private static function parseTag($value, &$i, $flags)
774774
$nextOffset += strspn($value, ' ', $nextOffset);
775775

776776
// Is followed by a scalar
777-
if (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) {
778-
// Manage scalars in {@link self::evaluateScalar()}
777+
if ((!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && 'tagged' !== $tag) {
778+
// Manage non-whitelisted scalars in {@link self::evaluateScalar()}
779779
return;
780780
}
781781

Parser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ private function parseValue($value, $flags, $context)
698698
if ('' !== $matches['tag']) {
699699
if ('!!binary' === $matches['tag']) {
700700
return Inline::evaluateBinaryScalar($data);
701+
} elseif ('tagged' === $matches['tag']) {
702+
return new TaggedValue(substr($matches['tag'], 1), $data);
701703
} elseif ('!' !== $matches['tag']) {
702704
@trigger_error($this->getDeprecationMessage(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since version 3.3. It will be replaced by an instance of %s in 4.0.', $matches['tag'], $data, TaggedValue::class)), E_USER_DEPRECATED);
703705
}

0 commit comments

Comments
 (0)