Skip to content

Commit dba6d86

Browse files
[DependencyInjection] Parse attributes found on abstract classes for resource definitions
1 parent 96f81af commit dba6d86

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

DependencyInjection/StreamablePass.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617

1718
/**
1819
* Sets the streamable metadata to the services that need them.
@@ -31,16 +32,20 @@ public function process(ContainerBuilder $container): void
3132

3233
// retrieve concrete services tagged with "json_streamer.streamable" tag
3334
foreach ($container->getDefinitions() as $id => $definition) {
34-
if (!$tag = ($definition->getTag('json_streamer.streamable')[0] ?? null)) {
35+
if (!$tag = $definition->getTag('json_streamer.streamable')[0] ?? null) {
3536
continue;
3637
}
37-
38-
if (($className = $container->getDefinition($id)->getClass()) && !$container->getDefinition($id)->isAbstract()) {
39-
$streamable[$className] = [
40-
'object' => $tag['object'],
41-
'list' => $tag['list'],
42-
];
38+
if (!$definition->hasTag('container.excluded')) {
39+
throw new InvalidArgumentException(\sprintf('The resource "%s" tagged "json_streamer.streamable" is missing the "container.excluded" tag.', $id));
40+
}
41+
$class = $container->getParameterBag()->resolveValue($definition->getClass());
42+
if (!$class || $definition->isAbstract()) {
43+
throw new InvalidArgumentException(\sprintf('The resource "%s" tagged "json_streamer.streamable" must have a class and not be abstract.', $id));
4344
}
45+
$streamable[$class] = [
46+
'object' => $tag['object'],
47+
'list' => $tag['list'],
48+
];
4449

4550
$container->removeDefinition($id);
4651
}

Tests/DependencyInjection/StreamablePassTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\JsonStreamer\DependencyInjection\StreamablePass;
1718

1819
class StreamablePassTest extends TestCase
@@ -25,8 +26,7 @@ public function testSetStreamable()
2526
$container->register('.json_streamer.cache_warmer.streamer')->setArguments([null]);
2627
$container->register('.json_streamer.cache_warmer.lazy_ghost')->setArguments([null]);
2728

28-
$container->register('streamable')->setClass('Foo')->addTag('json_streamer.streamable', ['object' => true, 'list' => true]);
29-
$container->register('abstractStreamable')->setClass('Bar')->addTag('json_streamer.streamable', ['object' => true, 'list' => true])->setAbstract(true);
29+
$container->register('streamable')->setClass('Foo')->addTag('json_streamer.streamable', ['object' => true, 'list' => true])->addTag('container.excluded');
3030
$container->register('notStreamable')->setClass('Baz');
3131

3232
$pass = new StreamablePass();
@@ -37,5 +37,9 @@ public function testSetStreamable()
3737

3838
$this->assertSame(['Foo' => ['object' => true, 'list' => true]], $streamerCacheWarmer->getArgument(0));
3939
$this->assertSame(['Foo'], $lazyGhostCacheWarmer->getArgument(0));
40+
41+
$container->register('abstractStreamable')->setClass('Bar')->addTag('json_streamer.streamable', ['object' => true, 'list' => true])->addTag('container.excluded')->setAbstract(true);
42+
$this->expectException(InvalidArgumentException::class);
43+
$pass->process($container);
4044
}
4145
}

0 commit comments

Comments
 (0)