Skip to content
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

Using tagged_iterator for RuleCollections #7405

Open
wants to merge 9 commits into
base: 2.6
Choose a base branch
from
231 changes: 119 additions & 112 deletions UPGRADE.md

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2395,11 +2395,6 @@ parameters:
count: 1
path: src/Sulu/Bundle/AudienceTargetingBundle/Rule/ReferrerRule.php

-
message: "#^Method Sulu\\\\Bundle\\\\AudienceTargetingBundle\\\\Rule\\\\RuleCollection\\:\\:__construct\\(\\) has parameter \\$rules with no value type specified in iterable type array\\.$#"
count: 1
path: src/Sulu/Bundle/AudienceTargetingBundle/Rule/RuleCollection.php

-
message: "#^Method Sulu\\\\Bundle\\\\AudienceTargetingBundle\\\\Rule\\\\RuleInterface\\:\\:evaluate\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @internal
*
* @deprecated
*/
class AddRulesPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$ruleCollection = $container->getDefinition('sulu_audience_targeting.rules_collection');
$taggedServices = $container->findTaggedServiceIds('sulu.audience_target_rule');

$ruleReferences = [];
foreach ($taggedServices as $id => $attributes) {
if (!isset($attributes[0]['alias'])) {
throw new \InvalidArgumentException(
\sprintf('No "alias" specified for audience targeting rule with service ID: "%s"', $id)
);
}

$ruleReferences[$attributes[0]['alias']] = new Reference($id);
}

$ruleCollection->replaceArgument(0, $ruleReferences);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</service>
<service id="sulu_audience_targeting.rules_collection"
class="Sulu\Bundle\AudienceTargetingBundle\Rule\RuleCollection">
<argument type="collection"></argument>
<argument type="tagged_iterator" tag="sulu.audience_target_rule" index-by="alias" />
</service>
<service id="sulu_audience_targeting.rules.locale"
class="Sulu\Bundle\AudienceTargetingBundle\Rule\LocaleRule">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
class RuleCollection implements RuleCollectionInterface
{
/**
* @var RuleInterface[]
* @var array<string, RuleInterface>
*/
private $rules;

public function __construct(array $rules)
/**
* @param iterable<RuleInterface> $rules
*/
public function __construct(iterable $rules)
{
$this->rules = $rules;
$this->rules = \iterator_to_array($rules);
}

public function getRule($name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Sulu\Bundle\AudienceTargetingBundle;

use Sulu\Bundle\AudienceTargetingBundle\DependencyInjection\Compiler\AddRulesPass;
use Sulu\Bundle\AudienceTargetingBundle\DependencyInjection\Compiler\DeviceDetectorCachePass;
use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupConditionInterface;
use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupInterface;
Expand Down Expand Up @@ -46,7 +45,6 @@ public function build(ContainerBuilder $container): void
$container
);

$container->addCompilerPass(new AddRulesPass());
$container->addCompilerPass(new DeviceDetectorCachePass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function process(ContainerBuilder $container)
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $id => $tags) {
foreach ($tags as $attributes) {
$type = $attributes[self::TYPE_ATTRIBUTE];
$namespace = \array_key_exists(self::NAMESPACE_ATTRIBUTE, $attributes)
? $attributes[self::NAMESPACE_ATTRIBUTE] : 'sulu';
$namespace = $attributes[self::NAMESPACE_ATTRIBUTE] ?? 'sulu';
$tag = $attributes[self::TAG_ATTRIBUTE];

if (!\array_key_exists($type, $references)) {
Expand Down
Loading