From ae7fa11c9179517007419ffc0b44741fbdfc4d18 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 23 Jul 2013 14:30:22 +0200 Subject: [PATCH 1/3] [Twig] fixed TwigEngine::exists() method when a template contains a syntax error (closes #88546) --- .../Bridge/Twig/Tests/TwigEngineTest.php | 56 +++++++++++++++++++ src/Symfony/Bridge/Twig/TwigEngine.php | 14 ++++- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php diff --git a/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php new file mode 100644 index 0000000000..1e6a3c4933 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Tests; + +use Symfony\Bridge\Twig\TwigEngine; + +class TwigEngineTest extends TestCase +{ + public function testExistsWithTemplateInstances() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists($this->getMockForAbstractClass('Twig_Template', array(), '', false))); + } + + public function testExistsWithNonExistentTemplates() + { + $engine = $this->getTwig(); + + $this->assertFalse($engine->exists('foobar')); + } + + public function testExistsWithTemplateWithSyntaxErrors() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists('error')); + } + + public function testExists() + { + $engine = $this->getTwig(); + + $this->assertTrue($engine->exists('index')); + } + + protected function getTwig() + { + $twig = new \Twig_Environment(new \Twig_Loader_Array(array( + 'index' => 'foo', + 'error' => '{{ foo }', + ))); + $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface'); + + return new TwigEngine($twig, $parser); + } +} diff --git a/src/Symfony/Bridge/Twig/TwigEngine.php b/src/Symfony/Bridge/Twig/TwigEngine.php index 955d4e0bae..41e15bacaa 100644 --- a/src/Symfony/Bridge/Twig/TwigEngine.php +++ b/src/Symfony/Bridge/Twig/TwigEngine.php @@ -75,9 +75,19 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface */ public function exists($name) { + if ($name instanceof \Twig_Template) { + return true; + } + + $loader = $this->environment->getLoader(); + + if ($loader instanceof \Twig_ExistsLoaderInterface) { + return $loader->exists($name); + } + try { - $this->load($name); - } catch (\InvalidArgumentException $e) { + $loader->getSource($name); + } catch (\Twig_Error_Loader $e) { return false; } From 0086ee392ba0088517dcef1f6590aca2f9ed689a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 24 Jul 2013 14:55:10 +0200 Subject: [PATCH 2/3] [Tests] Tests on php 5.5 should pass Because php 5.5 is stable now --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index dbbcc91e35..cb5f0ac5f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,10 +6,6 @@ php: - 5.4 - 5.5 -matrix: - allow_failures: - - php: 5.5 - before_script: - echo '' > ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini - sh -c 'if [ $(php -r "echo PHP_MINOR_VERSION;") -le 4 ]; then echo "extension = apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;' From 3553c71d1abd1fcfc7b9377aa0597a15f197a5fc Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Thu, 25 Jul 2013 16:45:03 +0200 Subject: [PATCH 3/3] return 0 if there is no valid data --- .../HttpKernel/DataCollector/TimeDataCollector.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php index 5fd2378499..3b1bda5084 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php @@ -72,6 +72,10 @@ class TimeDataCollector extends DataCollector */ public function getDuration() { + if (!isset($this->data['events']['__section__'])) { + return 0; + } + $lastEvent = $this->data['events']['__section__']; return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime(); @@ -86,6 +90,10 @@ class TimeDataCollector extends DataCollector */ public function getInitTime() { + if (!isset($this->data['events']['__section__'])) { + return 0; + } + return $this->data['events']['__section__']->getOrigin() - $this->getStartTime(); }