[HttpKernel] Make TraceableValueResolver $stopwatch mandatory

This commit is contained in:
Maxime Steinhausser 2018-05-13 19:11:39 +02:00
parent ebbe1adebc
commit 585ae7c646
3 changed files with 28 additions and 7 deletions

View File

@ -26,10 +26,10 @@ final class TraceableValueResolver implements ArgumentValueResolverInterface
private $inner;
private $stopwatch;
public function __construct(ArgumentValueResolverInterface $inner, ?Stopwatch $stopwatch = null)
public function __construct(ArgumentValueResolverInterface $inner, Stopwatch $stopwatch)
{
$this->inner = $inner;
$this->stopwatch = $stopwatch ?? new Stopwatch();
$this->stopwatch = $stopwatch;
}
/**

View File

@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
use Symfony\Component\Stopwatch\Stopwatch;
@ -31,11 +30,13 @@ class ControllerArgumentValueResolverPass implements CompilerPassInterface
private $argumentResolverService;
private $argumentValueResolverTag;
private $traceableResolverStopwatch;
public function __construct(string $argumentResolverService = 'argument_resolver', string $argumentValueResolverTag = 'controller.argument_value_resolver')
public function __construct(string $argumentResolverService = 'argument_resolver', string $argumentValueResolverTag = 'controller.argument_value_resolver', string $traceableResolverStopwatch = 'debug.stopwatch')
{
$this->argumentResolverService = $argumentResolverService;
$this->argumentValueResolverTag = $argumentValueResolverTag;
$this->traceableResolverStopwatch = $traceableResolverStopwatch;
}
public function process(ContainerBuilder $container)
@ -46,12 +47,12 @@ class ControllerArgumentValueResolverPass implements CompilerPassInterface
$resolvers = $this->findAndSortTaggedServices($this->argumentValueResolverTag, $container);
if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class)) {
if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class) && $container->has($this->traceableResolverStopwatch)) {
foreach ($resolvers as $resolverReference) {
$id = (string) $resolverReference;
$container->register("debug.$id", TraceableValueResolver::class)
->setDecoratedService($id)
->setArguments(array(new Reference("debug.$id.inner"), new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE)));
->setArguments(array(new Reference("debug.$id.inner"), new Reference($this->traceableResolverStopwatch)));
}
}

View File

@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass;
use Symfony\Component\Stopwatch\Stopwatch;
class ControllerArgumentValueResolverPassTest extends TestCase
{
@ -52,7 +53,7 @@ class ControllerArgumentValueResolverPassTest extends TestCase
$this->assertFalse($container->hasDefinition('n3.traceable'));
}
public function testInDebug()
public function testInDebugWithStopWatchDefinition()
{
$services = array(
'n3' => array(array()),
@ -68,6 +69,7 @@ class ControllerArgumentValueResolverPassTest extends TestCase
$definition = new Definition(ArgumentResolver::class, array(null, array()));
$container = new ContainerBuilder();
$container->register('debug.stopwatch', Stopwatch::class);
$container->setDefinition('argument_resolver', $definition);
foreach ($services as $id => list($tag)) {
@ -88,6 +90,24 @@ class ControllerArgumentValueResolverPassTest extends TestCase
$this->assertTrue($container->hasDefinition('n3'));
}
public function testInDebugWithouStopWatchDefinition()
{
$expected = array(new Reference('n1'));
$definition = new Definition(ArgumentResolver::class, array(null, array()));
$container = new ContainerBuilder();
$container->register('n1')->addTag('controller.argument_value_resolver');
$container->setDefinition('argument_resolver', $definition);
$container->setParameter('kernel.debug', true);
(new ControllerArgumentValueResolverPass())->process($container);
$this->assertEquals($expected, $definition->getArgument(1)->getValues());
$this->assertFalse($container->hasDefinition('debug.n1'));
$this->assertTrue($container->hasDefinition('n1'));
}
public function testReturningEmptyArrayWhenNoService()
{
$definition = new Definition(ArgumentResolver::class, array(null, array()));