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 ff726c6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 39 deletions.
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12660,16 +12660,6 @@ parameters:
count: 1
path: src/Sulu/Bundle/MarkupBundle/Listener/MarkupListener.php

-
message: "#^Parameter \\#2 \\$array of function array_key_exists expects array, Sulu\\\\Bundle\\\\MarkupBundle\\\\Markup\\\\MarkupParserInterface given\\.$#"
count: 1
path: src/Sulu/Bundle/MarkupBundle/Listener/MarkupListener.php

-
message: "#^Property Sulu\\\\Bundle\\\\MarkupBundle\\\\Listener\\\\MarkupListener\\:\\:\\$markupParser \\(Sulu\\\\Bundle\\\\MarkupBundle\\\\Markup\\\\MarkupParserInterface\\) does not accept array\\<Sulu\\\\Bundle\\\\MarkupBundle\\\\Markup\\\\MarkupParserInterface\\>\\.$#"
count: 1
path: src/Sulu/Bundle/MarkupBundle/Listener/MarkupListener.php

-
message: "#^Method Sulu\\\\Bundle\\\\MarkupBundle\\\\Markup\\\\HtmlTagExtractor\\:\\:count\\(\\) should return int but returns int\\<0, max\\>\\|false\\.$#"
count: 1
Expand Down Expand Up @@ -12835,11 +12825,6 @@ parameters:
count: 1
path: src/Sulu/Bundle/MarkupBundle/Tag/TagRegistry.php

-
message: "#^Class Sulu\\\\Bundle\\\\MarkupBundle\\\\Listener\\\\MarkupListener constructor invoked with 2 parameters, 1 required\\.$#"
count: 1
path: src/Sulu/Bundle/MarkupBundle/Tests/Unit/Listener/MarkupListenerTest.php

-
message: "#^Method Sulu\\\\Bundle\\\\MarkupBundle\\\\Tests\\\\Unit\\\\Markup\\\\HtmlTagExtractorTest\\:\\:provideMultipleTags\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
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 ff726c6

Please sign in to comment.