From ef0c9679cbf31c9c660627539f8a377b99e014d3 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 14 Jan 2015 22:09:22 +0100 Subject: [PATCH] integrated the Twig profiler --- composer.json | 8 ++- .../Twig/Extension/ProfilerExtension.php | 58 +++++++++++++++++++ .../TwigBundle/Debug/TimedTwigEngine.php | 4 ++ .../Compiler/ExtensionPass.php | 9 ++- .../TwigBundle/Resources/config/debug.xml | 7 --- .../TwigBundle/Resources/config/twig.xml | 7 +++ 6 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 src/Symfony/Bridge/Twig/Extension/ProfilerExtension.php diff --git a/composer.json b/composer.json index 4642916eb4..106e7f112e 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,10 @@ { + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/fabpot/Twig" + } + ], "name": "symfony/symfony", "type": "library", "description": "The Symfony PHP framework", @@ -18,7 +24,7 @@ "require": { "php": ">=5.3.9", "doctrine/common": "~2.3", - "twig/twig": "~1.17", + "twig/twig": "dev-profiler as 1.17.x-dev", "psr/log": "~1.0" }, "replace": { diff --git a/src/Symfony/Bridge/Twig/Extension/ProfilerExtension.php b/src/Symfony/Bridge/Twig/Extension/ProfilerExtension.php new file mode 100644 index 0000000000..2ec5fc189c --- /dev/null +++ b/src/Symfony/Bridge/Twig/Extension/ProfilerExtension.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\Stopwatch\Stopwatch; + +/** + * @author Fabien Potencier + */ +class ProfilerExtension extends \Twig_Extension_Profiler +{ + private $stopwatch; + private $events; + + public function __construct(\Twig_Profiler_Profile $profile, Stopwatch $stopwatch) + { + parent::__construct($profile); + + $this->stopwatch = $stopwatch; + $this->events = new \SplObjectStorage(); + } + + public function enter(\Twig_Profiler_Profile $profile) + { + if ($profile->isTemplate()) { + $this->events[$profile] = $this->stopwatch->start($profile->getName(), 'template'); + } + + parent::enter($profile); + } + + public function leave(\Twig_Profiler_Profile $profile) + { + parent::leave($profile); + + if ($profile->isTemplate()) { + $this->events[$profile]->stop(); + unset($this->events[$profile]); + } + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'native_profiler'; + } +} diff --git a/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php b/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php index 1a73eee9bb..826af7925f 100644 --- a/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php @@ -11,6 +11,8 @@ namespace Symfony\Bundle\TwigBundle\Debug; +trigger_error('The '.__NAMESPACE__.'\TimedTwigEngine class is deprecated since version 2.7 and will be removed in 3.0. Use the Twig native profiler instead.', E_USER_DEPRECATED); + use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Component\Templating\TemplateNameParserInterface; use Symfony\Component\Stopwatch\Stopwatch; @@ -20,6 +22,8 @@ use Symfony\Component\Config\FileLocatorInterface; * Times the time spent to render a template. * * @author Fabien Potencier + * + * @deprecated since version 2.7, to be removed in 3.0. Use the Twig native profiler instead. */ class TimedTwigEngine extends TwigEngine { diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php index 228e485cc7..fa92bbd9f1 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php @@ -63,13 +63,12 @@ class ExtensionPass implements CompilerPassInterface $container->getDefinition('twig.extension.code')->replaceArgument(0, $container->getParameter('templating.helper.code.file_link_format')); } + if ($container->getParameter('kernel.debug') && $container->has('debug.stopwatch')) { + $container->getDefinition('twig.extension.profiler')->addTag('twig.extension'); + } + if ($container->has('templating')) { $container->getDefinition('twig.cache_warmer')->addTag('kernel.cache_warmer'); - - if ($container->getParameter('kernel.debug')) { - $container->setDefinition('templating.engine.twig', $container->findDefinition('debug.templating.engine.twig')); - $container->setAlias('debug.templating.engine.twig', 'templating.engine.twig'); - } } else { $loader = $container->getDefinition('twig.loader.native_filesystem'); $loader->addTag('twig.loader'); diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml index c44ae63121..3f3ff3260d 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml @@ -9,13 +9,6 @@ - - - - - - - diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 3f82a3a095..28b7483180 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -70,6 +70,13 @@ + + + + + + +