|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\Console\ConsoleEvents; |
| 15 | +use Symfony\Component\Console\Debug\CliRequest; |
| 16 | +use Symfony\Component\Console\Event\ConsoleCommandEvent; |
| 17 | +use Symfony\Component\Console\Event\ConsoleErrorEvent; |
| 18 | +use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
| 19 | +use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 20 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 23 | +use Symfony\Component\HttpKernel\Profiler\Profile; |
| 24 | +use Symfony\Component\HttpKernel\Profiler\Profiler; |
| 25 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 26 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 27 | + |
| 28 | +/** |
| 29 | + * @internal |
| 30 | + * |
| 31 | + * @author Jules Pietri <[email protected]> |
| 32 | + */ |
| 33 | +final class ConsoleProfilerListener implements EventSubscriberInterface |
| 34 | +{ |
| 35 | + private ?\Throwable $error = null; |
| 36 | + /** @var \SplObjectStorage<Request, Profile> */ |
| 37 | + private \SplObjectStorage $profiles; |
| 38 | + /** @var \SplObjectStorage<Request, ?Request> */ |
| 39 | + private \SplObjectStorage $parents; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + private readonly Profiler $profiler, |
| 43 | + private readonly RequestStack $requestStack, |
| 44 | + private readonly Stopwatch $stopwatch, |
| 45 | + private readonly UrlGeneratorInterface $urlGenerator, |
| 46 | + ) { |
| 47 | + $this->profiles = new \SplObjectStorage(); |
| 48 | + $this->parents = new \SplObjectStorage(); |
| 49 | + } |
| 50 | + |
| 51 | + public static function getSubscribedEvents(): array |
| 52 | + { |
| 53 | + return [ |
| 54 | + ConsoleEvents::COMMAND => ['initialize', 4096], |
| 55 | + ConsoleEvents::ERROR => ['catch', -2048], |
| 56 | + ConsoleEvents::TERMINATE => ['profile', -4096], |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + public function initialize(ConsoleCommandEvent $event): void |
| 61 | + { |
| 62 | + if (!$event->getInput()->getOption('profile')) { |
| 63 | + $this->profiler->disable(); |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $request = $this->requestStack->getCurrentRequest(); |
| 69 | + |
| 70 | + if (!$request instanceof CliRequest || $request->command !== $event->getCommand()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + $request->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6)); |
| 75 | + $this->stopwatch->openSection(); |
| 76 | + } |
| 77 | + |
| 78 | + public function catch(ConsoleErrorEvent $event): void |
| 79 | + { |
| 80 | + $this->error = $event->getError(); |
| 81 | + } |
| 82 | + |
| 83 | + public function profile(ConsoleTerminateEvent $event): void |
| 84 | + { |
| 85 | + if (!$this->profiler->isEnabled()) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $request = $this->requestStack->getCurrentRequest(); |
| 90 | + |
| 91 | + if (!$request instanceof CliRequest || $request->command !== $event->getCommand()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (null !== $sectionId = $request->attributes->get('_stopwatch_token')) { |
| 96 | + // we must close the section before saving the profile to allow late collect |
| 97 | + try { |
| 98 | + $this->stopwatch->stopSection($sectionId); |
| 99 | + } catch (\LogicException) { |
| 100 | + // noop |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + $request->command->exitCode = $event->getExitCode(); |
| 105 | + $request->command->interruptedBySignal = $event->getInterruptingSignal(); |
| 106 | + |
| 107 | + $profile = $this->profiler->collect($request, $request->getResponse(), $this->error); |
| 108 | + $this->error = null; |
| 109 | + $this->profiles[$request] = $profile; |
| 110 | + |
| 111 | + if ($this->parents[$request] = $this->requestStack->getParentRequest()) { |
| 112 | + // do not save on sub commands |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // attach children to parents |
| 117 | + foreach ($this->profiles as $request) { |
| 118 | + if (null !== $parentRequest = $this->parents[$request]) { |
| 119 | + if (isset($this->profiles[$parentRequest])) { |
| 120 | + $this->profiles[$parentRequest]->addChild($this->profiles[$request]); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + $output = $event->getOutput(); |
| 126 | + $output = $output instanceof ConsoleOutputInterface && $output->isVerbose() ? $output->getErrorOutput() : null; |
| 127 | + |
| 128 | + // save profiles |
| 129 | + foreach ($this->profiles as $r) { |
| 130 | + $p = $this->profiles[$r]; |
| 131 | + $this->profiler->saveProfile($p); |
| 132 | + |
| 133 | + $token = $p->getToken(); |
| 134 | + $output?->writeln(sprintf( |
| 135 | + 'See profile <href=%s>%s</>', |
| 136 | + $this->urlGenerator->generate('_profiler', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL), |
| 137 | + $token |
| 138 | + )); |
| 139 | + } |
| 140 | + |
| 141 | + $this->profiles = new \SplObjectStorage(); |
| 142 | + $this->parents = new \SplObjectStorage(); |
| 143 | + } |
| 144 | +} |
0 commit comments