diff --git a/src/bundle/DependencyInjection/EzPlatformAdminUiExtension.php b/src/bundle/DependencyInjection/EzPlatformAdminUiExtension.php index cd3c2e4d2a..aedb200669 100644 --- a/src/bundle/DependencyInjection/EzPlatformAdminUiExtension.php +++ b/src/bundle/DependencyInjection/EzPlatformAdminUiExtension.php @@ -12,6 +12,7 @@ use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Yaml\Yaml; class EzPlatformAdminUiExtension extends Extension implements PrependExtensionInterface @@ -39,6 +40,8 @@ public function load(array $configs, ContainerBuilder $container) if ($environment === 'behat') { $loader->load('services/feature_contexts.yaml'); } + + $this->registerDebugConfiguration($container, $loader); } /** @@ -143,4 +146,13 @@ private function prependJMSTranslation(ContainerBuilder $container): void ], ]); } + + private function registerDebugConfiguration(ContainerBuilder $container, YamlFileLoader $loader): void + { + $debug = $container->getParameter('kernel.debug'); + + if ($debug && class_exists(Stopwatch::class)) { + $loader->load('services.debug.yaml'); + } + } } diff --git a/src/bundle/Resources/config/services.debug.yaml b/src/bundle/Resources/config/services.debug.yaml new file mode 100644 index 0000000000..bb4a1d64c7 --- /dev/null +++ b/src/bundle/Resources/config/services.debug.yaml @@ -0,0 +1,7 @@ +services: + EzSystems\EzPlatformAdminUi\Component\Renderer\TraceableRenderer: + decorates: EzSystems\EzPlatformAdminUi\Component\Renderer\RendererInterface + public: false + arguments: + - '@.inner' + - '@debug.stopwatch' diff --git a/src/bundle/Resources/config/services/components.yaml b/src/bundle/Resources/config/services/components.yaml index 66672f21ea..97819f5eb9 100644 --- a/src/bundle/Resources/config/services/components.yaml +++ b/src/bundle/Resources/config/services/components.yaml @@ -36,5 +36,7 @@ services: EzSystems\EzPlatformAdminUi\Component\Renderer\DefaultRenderer: public: false + calls: + - setRenderer: ['@EzSystems\EzPlatformAdminUi\Component\Renderer\RendererInterface'] EzSystems\EzPlatformAdminUi\Component\Renderer\RendererInterface: '@EzSystems\EzPlatformAdminUi\Component\Renderer\DefaultRenderer' diff --git a/src/bundle/Templating/Twig/ComponentExtension.php b/src/bundle/Templating/Twig/ComponentExtension.php index 0020543404..14b1669568 100644 --- a/src/bundle/Templating/Twig/ComponentExtension.php +++ b/src/bundle/Templating/Twig/ComponentExtension.php @@ -8,22 +8,17 @@ namespace EzSystems\EzPlatformAdminUiBundle\Templating\Twig; -use EzSystems\EzPlatformAdminUi\Component\Registry as ComponentRegistry; use EzSystems\EzPlatformAdminUi\Component\Renderer\RendererInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class ComponentExtension extends AbstractExtension { - protected $registry; - protected $renderer; public function __construct( - ComponentRegistry $registry, RendererInterface $renderer ) { - $this->registry = $registry; $this->renderer = $renderer; } diff --git a/src/lib/Component/Renderer/DefaultRenderer.php b/src/lib/Component/Renderer/DefaultRenderer.php index 7177d0bd8a..d8a3c595ed 100644 --- a/src/lib/Component/Renderer/DefaultRenderer.php +++ b/src/lib/Component/Renderer/DefaultRenderer.php @@ -16,16 +16,26 @@ class DefaultRenderer implements RendererInterface { + /** @var \EzSystems\EzPlatformAdminUi\Component\Registry */ protected $registry; + /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */ protected $eventDispatcher; + /** @var \EzSystems\EzPlatformAdminUi\Component\Renderer\RendererInterface */ + protected $renderer; + public function __construct(Registry $registry, EventDispatcherInterface $eventDispatcher) { $this->registry = $registry; $this->eventDispatcher = $eventDispatcher; } + public function setRenderer(RendererInterface $renderer): void + { + $this->renderer = $renderer; + } + public function renderGroup(string $groupName, array $parameters = []): array { $this->eventDispatcher->dispatch(new RenderGroupEvent( @@ -38,7 +48,7 @@ public function renderGroup(string $groupName, array $parameters = []): array $rendered = []; foreach ($components as $id => $component) { - $rendered[] = $this->renderSingle($id, $groupName, $parameters); + $rendered[] = $this->renderer->renderSingle($id, $groupName, $parameters); } return $rendered; diff --git a/src/lib/Component/Renderer/TraceableRenderer.php b/src/lib/Component/Renderer/TraceableRenderer.php new file mode 100644 index 0000000000..32ca04d6ee --- /dev/null +++ b/src/lib/Component/Renderer/TraceableRenderer.php @@ -0,0 +1,56 @@ +decorated = $decorated; + $this->stopwatch = $stopwatch; + } + + public function renderGroup(string $groupName, array $parameters = []): array + { + $event = $this->stopwatch->start(sprintf('%s', $groupName), 'admin-ui'); + + try { + $rendered = $this->decorated->renderGroup($groupName, $parameters); + } finally { + if ($event->isStarted()) { + $event->stop(); + } + } + + return $rendered; + } + + public function renderSingle(string $name, $groupName, array $parameters = []): string + { + $event = $this->stopwatch->start(sprintf('%s - %s', $groupName, $name), 'admin-ui'); + + try { + $rendered = $this->decorated->renderSingle($name, $groupName, $parameters); + } finally { + if ($event->isStarted()) { + $event->stop(); + } + } + + return $rendered; + } +}