-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Added support for timings in ArgumentValueResolvers #26833
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* Provides timing information via the stopwatch. | ||
* | ||
* @author Iltar van der Berg <kjarli@gmail.com> | ||
*/ | ||
final class TraceableValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
private $inner; | ||
private $stopwatch; | ||
|
||
public function __construct(ArgumentValueResolverInterface $inner, ?Stopwatch $stopwatch = null) | ||
{ | ||
$this->inner = $inner; | ||
$this->stopwatch = $stopwatch ?? new Stopwatch(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
$method = \get_class($this->inner).'::'.__FUNCTION__; | ||
$this->stopwatch->start($method); | ||
|
||
$return = $this->inner->supports($request, $argument); | ||
|
||
$this->stopwatch->stop($method); | ||
|
||
return $return; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
$method = \get_class($this->inner).'::'.__FUNCTION__; | ||
$this->stopwatch->start($method); | ||
|
||
yield from $this->inner->resolve($request, $argument); | ||
|
||
$this->stopwatch->stop($method); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
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; | ||
|
||
/** | ||
* Gathers and configures the argument value resolvers. | ||
|
@@ -40,9 +44,20 @@ public function process(ContainerBuilder $container) | |
return; | ||
} | ||
|
||
$resolvers = $this->findAndSortTaggedServices($this->argumentValueResolverTag, $container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assignment does not seem necessary, leftover? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually being used for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, missed the loop! |
||
|
||
if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class)) { | ||
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))); | ||
} | ||
} | ||
|
||
$container | ||
->getDefinition($this->argumentResolverService) | ||
->replaceArgument(1, new IteratorArgument($this->findAndSortTaggedServices($this->argumentValueResolverTag, $container))) | ||
->replaceArgument(1, new IteratorArgument($resolvers)) | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
class TraceableValueResolverTest extends TestCase | ||
{ | ||
public function testTimingsInSupports() | ||
{ | ||
$stopwatch = new Stopwatch(); | ||
$resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch); | ||
$argument = new ArgumentMetadata('dummy', 'string', false, false, null); | ||
$request = new Request(); | ||
|
||
$this->assertTrue($resolver->supports($request, $argument)); | ||
|
||
$event = $stopwatch->getEvent(ResolverStub::class.'::supports'); | ||
$this->assertCount(1, $event->getPeriods()); | ||
} | ||
|
||
public function testTimingsInResolve() | ||
{ | ||
$stopwatch = new Stopwatch(); | ||
$resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch); | ||
$argument = new ArgumentMetadata('dummy', 'string', false, false, null); | ||
$request = new Request(); | ||
|
||
$iterable = $resolver->resolve($request, $argument); | ||
|
||
foreach ($iterable as $index => $resolved) { | ||
$event = $stopwatch->getEvent(ResolverStub::class.'::resolve'); | ||
$this->assertTrue($event->isStarted()); | ||
$this->assertEmpty($event->getPeriods()); | ||
switch ($index) { | ||
case 0: | ||
$this->assertEquals('first', $resolved); | ||
break; | ||
case 1: | ||
$this->assertEquals('second', $resolved); | ||
break; | ||
} | ||
} | ||
|
||
$event = $stopwatch->getEvent(ResolverStub::class.'::resolve'); | ||
$this->assertCount(1, $event->getPeriods()); | ||
} | ||
} | ||
|
||
class ResolverStub implements ArgumentValueResolverInterface | ||
{ | ||
public function supports(Request $request, ArgumentMetadata $argument) | ||
{ | ||
return true; | ||
} | ||
|
||
public function resolve(Request $request, ArgumentMetadata $argument) | ||
{ | ||
yield 'first'; | ||
yield 'second'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also just do
$method = \get_class($this->inner).'::supports';
and resolve for the other. Not sure what is preferred. Opinions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍for
__FUNCTION__