Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d53905

Browse files
committed
Merge branch '2.2' into 2.3
* 2.2: return 0 if there is no valid data [Tests] Tests on php 5.5 should pass [Twig] fixed TwigEngine::exists() method when a template contains a syntax error (closes #88546)
2 parents de1915f + 63ef3c1 commit 9d53905

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Bridge\Twig\Tests;
13+
14+
use Symfony\Bridge\Twig\TwigEngine;
15+
16+
class TwigEngineTest extends TestCase
17+
{
18+
public function testExistsWithTemplateInstances()
19+
{
20+
$engine = $this->getTwig();
21+
22+
$this->assertTrue($engine->exists($this->getMockForAbstractClass('Twig_Template', array(), '', false)));
23+
}
24+
25+
public function testExistsWithNonExistentTemplates()
26+
{
27+
$engine = $this->getTwig();
28+
29+
$this->assertFalse($engine->exists('foobar'));
30+
}
31+
32+
public function testExistsWithTemplateWithSyntaxErrors()
33+
{
34+
$engine = $this->getTwig();
35+
36+
$this->assertTrue($engine->exists('error'));
37+
}
38+
39+
public function testExists()
40+
{
41+
$engine = $this->getTwig();
42+
43+
$this->assertTrue($engine->exists('index'));
44+
}
45+
46+
protected function getTwig()
47+
{
48+
$twig = new \Twig_Environment(new \Twig_Loader_Array(array(
49+
'index' => 'foo',
50+
'error' => '{{ foo }',
51+
)));
52+
$parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
53+
54+
return new TwigEngine($twig, $parser);
55+
}
56+
}

src/Symfony/Bridge/Twig/TwigEngine.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,19 @@ public function stream($name, array $parameters = array())
7575
*/
7676
public function exists($name)
7777
{
78+
if ($name instanceof \Twig_Template) {
79+
return true;
80+
}
81+
82+
$loader = $this->environment->getLoader();
83+
84+
if ($loader instanceof \Twig_ExistsLoaderInterface) {
85+
return $loader->exists($name);
86+
}
87+
7888
try {
79-
$this->load($name);
80-
} catch (\InvalidArgumentException $e) {
89+
$loader->getSource($name);
90+
} catch (\Twig_Error_Loader $e) {
8191
return false;
8292
}
8393

src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function getEvents()
7878
*/
7979
public function getDuration()
8080
{
81+
if (!isset($this->data['events']['__section__'])) {
82+
return 0;
83+
}
84+
8185
$lastEvent = $this->data['events']['__section__'];
8286

8387
return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
@@ -92,6 +96,10 @@ public function getDuration()
9296
*/
9397
public function getInitTime()
9498
{
99+
if (!isset($this->data['events']['__section__'])) {
100+
return 0;
101+
}
102+
95103
return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
96104
}
97105

0 commit comments

Comments
 (0)