Skip to content

Commit

Permalink
Using tagged iterators for the markup listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed May 6, 2024
1 parent 771a24c commit 738dfae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,28 @@

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

/**
* Collects all parsers for markup.
*/
class ParserCompilerPass implements CompilerPassInterface
{
/**
* @deprecated Don't use this constant anymore use tagged_iterator
*/
public const SERVICE_ID = 'sulu_markup.response_listener';

/**
* @deprecated Use the value instead
*/
public const TAG_NAME = 'sulu_markup.parser';

/**
* @deprecated don't use this constant anymore
*/
public const TYPE_ATTRIBUTE = 'type';

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(self::SERVICE_ID)) {
return;
}

$references = [];
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $id => $tags) {
foreach ($tags as $attributes) {
$references[$attributes[self::TYPE_ATTRIBUTE]] = new Reference($id);
}
}

if (0 === \count($references)) {
return;
}

$container->getDefinition(self::SERVICE_ID)->replaceArgument(0, $references);
}
}
10 changes: 5 additions & 5 deletions src/Sulu/Bundle/MarkupBundle/Listener/MarkupListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
class MarkupListener implements EventSubscriberInterface
{
/**
* @var MarkupParserInterface
* @var array<string, MarkupParserInterface>
*/
private $markupParser;
private array $markupParser;

/**
* @param MarkupParserInterface[] $markupParser
* @param iterable<MarkupParserInterface> $markupParser
*/
public function __construct(array $markupParser)
public function __construct(iterable $markupParser)
{
$this->markupParser = $markupParser;
$this->markupParser = \iterator_to_array($markupParser);
}

public static function getSubscribedEvents(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Sulu/Bundle/MarkupBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</service>

<service id="sulu_markup.response_listener" class="Sulu\Bundle\MarkupBundle\Listener\MarkupListener">
<argument type="collection"/>
<argument type="tagged_iterator" tag="sulu_markup.parser" index-by="type" />

<tag name="kernel.event_subscriber"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function setUp(): void
$this->response->reveal()
);

$this->listener = new MarkupListener(['html' => $this->markupParser->reveal()], ['text/html' => 'html']);
$this->listener = new MarkupListener(['html' => $this->markupParser->reveal()]);
}

public function testReplaceMarkup(): void
Expand Down

0 comments on commit 738dfae

Please sign in to comment.