8000 [DI] Allow to choose an index for tagged collection by deguif · Pull Request #29598 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Allow to choose an index for tagged collection #29598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,33 @@
class TaggedIteratorArgument extends IteratorArgument
{
private $tag;
private $indexAttribute;
private $defaultIndexMethod;

public function __construct(string $tag)
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null)
{
parent::__construct([]);

$this->tag = $tag;
$this->indexAttribute = $indexAttribute ?: null;

if ($indexAttribute) {
$this->defaultIndexMethod = $defaultIndexMethod ?: ('getDefault'.str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $indexAttribute))).'Name');
}
}

public function getTag()
{
return $this->tag;
}

public function getIndexAttribute(): ?string
{
return $this->indexAttribute;
}

public function getDefaultIndexMethod(): ?string
{
return $this->defaultIndexMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\DependencyInjection\Compiler;

use Sy 8000 mfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;

/**
Expand All @@ -31,18 +33,59 @@ trait PriorityTaggedServiceTrait
* @see https://bugs.php.net/bug.php?id=53710
* @see https://bugs.php.net/bug.php?id=60926
*
* @param string $tagName
* @param ContainerBuilder $container
* @param string|TaggedIteratorArgument $tagName
* @param ContainerBuilder $container
*
* @return Reference[]
*/
private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
{
$indexAttribute = $defaultIndexMethod = null;
if ($tagName instanceof TaggedIteratorArgument) {
$indexAttribute = $tagName->getIndexAttribute();
$defaultIndexMethod = $tagName->getDefaultIndexMethod();
$tagName = $tagName->getTag();
}
$services = [];

foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$services[$priority][] = new Reference($serviceId);

if (null === $indexAttribute) {
$services[$priority][] = new Reference($serviceId);

continue;
}

if (isset($attributes[0][$indexAttribute])) {
$services[$priority][$attributes[0][$indexAttribute]] = new Reference($serviceId);

continue;
}

if (!$r = $container->getReflectionClass($class = $container->getDefinition($serviceId)->getClass())) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId));
}

if (!$r->hasMethod($defaultIndexMethod)) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" not found: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}

if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}

if (!$rm->isPublic()) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}

$key = $rm->invoke(null);

if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return a string, got %s: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, \gettype($key), $tagName, $serviceId, $indexAttribute));
}

$services[$priority][$key] = new Reference($serviceId);
}

if ($services) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function processValue($value, $isRoot = false)
return parent::processValue($value, $isRoot);
}

$value->setValues($this->findAndSortTaggedServices($value->getTag(), $this->container));
$value->setValues($this->findAndSortTaggedServices($value, $this->container));

return $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ function iterator(array $values): IteratorArgument
/**
* Creates a lazy iterator by tag name.
*/
function tagged(string $tag): TaggedIteratorArgument
function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument
{
return new TaggedIteratorArgument($tag);
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,17 @@ private function resolveServices($value, $file, $isParameter = false)
}
}
if ('tagged' === $value->getTag()) {
if (!\is_string($argument) || !$argument) {
throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts non empty string in "%s".', $file));
if (\is_string($argument) && $argument) {
return new TaggedIteratorArgument($argument);
}
if (\is_array($argument) && isset($argument['name']) && $argument['name']) {
if (array_diff(array_keys($argument), ['name', 'index_by', 'default_index_method'])) {
throw new InvalidArgumentException('"!tagged" tag contains unsupported keys. Supported are: "name, index_by, default_index_method".');
}

return new TaggedIteratorArgument($argument);
return new TaggedIteratorArgument($argument['name'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to ease spotting typos, we should validate that there are no extra keys

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts a non empty string or an array with a key "name" in "%s".', $file));
}
if ('service' === $value->getTag()) {
if ($isParameter) {
Expand Down
0