Skip to content

Commit

Permalink
Fixes for Preprocessor Loader.
Browse files Browse the repository at this point in the history
Fixed PHP 5.2 compatibility, code style and proper handling of Twig_ExistsLoaderInterface loaders.
  • Loading branch information
TiGR committed Oct 11, 2014
1 parent 446169b commit e5f3157
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/Twig/Loader/Preprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*/
class Twig_Loader_Preprocessor implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
{
private $realLoader, $callback;
private $realLoader;
private $callback;

/**
* Constructor
Expand All @@ -37,7 +38,7 @@ class Twig_Loader_Preprocessor implements Twig_LoaderInterface, Twig_ExistsLoade
* @param Twig_LoaderInterface $loader A loader that does real loading of templates
* @param callable $callback The processing callback
*/
public function __construct(Twig_LoaderInterface $loader, callable $callback)
public function __construct(Twig_LoaderInterface $loader, $callback)
{
$this->realLoader = $loader;
$this->callback = $callback;
Expand All @@ -56,11 +57,20 @@ public function getSource($name)
*/
public function exists($name)
{
$name = (string)$name;

if ($this->realLoader instanceof Twig_ExistsLoaderInterface) {
return $this->realLoader->exists($name);
} else {
try {
$this->realLoader->getSource($name);

return true;
} catch (Twig_Error_Loader $e) {
}
}

return true;
return false;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/Twig/Tests/Loader/PreprocessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ public function testProcessing()
$loader = new Twig_Loader_Preprocessor($realLoader, 'strtoupper');
$this->assertEquals('TEST', $loader->getSource('test'));
}

public function testExists()
{
$realLoader = $this->getMock('Twig_Loader_Array', array('exists', 'getSource'), array(), '', false);
$realLoader->expects($this->once())->method('exists')->will($this->returnValue(false));
$realLoader->expects($this->never())->method('getSource');

$loader = new Twig_Loader_Preprocessor($realLoader, 'trim');
$this->assertFalse($loader->exists('foo'));

$realLoader = $this->getMock('Twig_LoaderInterface');
$realLoader->expects($this->once())->method('getSource')->will($this->returnValue('content'));

$loader = new Twig_Loader_Preprocessor($realLoader, 'trim');
$this->assertTrue($loader->exists('foo'));
}
}

0 comments on commit e5f3157

Please sign in to comment.