Skip to content

Commit

Permalink
Don't try to load real templates using TwigLoaderRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
marekkalnik committed Nov 25, 2012
1 parent a370d59 commit 176560b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 64 deletions.
46 changes: 10 additions & 36 deletions Extensions/Twig/TwigLoaderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

namespace Theodo\RogerCmsBundle\Extensions\Twig;

use Twig_LoaderInterface;
use Twig_Error_Loader;
use Twig_LoaderInterface;
use Twig_Loader_Filesystem;
use Theodo\RogerCmsBundle\Repository\ContentRepositoryInterface;

/**
* Loads a template from a repository.
*
* @author Fabrice Bernhard <[email protected]>
* @author Marek Kalnik <[email protected]>
*/
class TwigLoaderRepository implements Twig_LoaderInterface
{
Expand All @@ -29,44 +31,20 @@ class TwigLoaderRepository implements Twig_LoaderInterface
*/
protected $contentRepository = null;

/**
* @var Twig_LoaderInterface
*/
protected $fallbackLoader = null;

/**
* Registers content repository and fallback loader
*
* @param ContentRepositoryInterface $contentRepository Cms objects repository
* @param Twig_LoaderInterface $fallbackLoader Twig loader for objects not handled by CMS
* @param String $fallbackPath Path to use by Twig loader
*
* @author Fabrice Bernhard <[email protected]>
* @since 2011-06-22
*/
public function __construct(ContentRepositoryInterface $contentRepository, Twig_LoaderInterface $fallbackLoader = null, $fallbackPath = null)
public function __construct(ContentRepositoryInterface $contentRepository)
{
$this->contentRepository = $contentRepository;
$this->fallbackLoader = $fallbackLoader;
if ($this->fallbackLoader instanceof Twig_Loader_Filesystem
&& $fallbackPath != null) {
$pathPrefix = __DIR__.'/../../../../../';

if (!file_exists($pathPrefix.$fallbackPath)) {
throw new \InvalidArgumentException('The specified fallback path does not exist. Tried to access: '.$pathPrefix.$fallbackPath);
}

$this->fallbackLoader->addPath($pathPrefix.$fallbackPath);
}
}

/**
* Getter for content repository
*
* @return ContentRepositoryInterface The current content repository
*
* @author Fabrice Bernhard <[email protected]>
* @since 2001-06-22
*/
public function getContentRepository()
{
Expand All @@ -77,9 +55,6 @@ public function getContentRepository()
* Setter for content repository
*
* @param ContentRepositoryInterface $contentRepository
*
* @author Fabrice Bernhard <[email protected]>
* @since 2001-06-22
*/
public function setContentRepository(ContentRepositoryInterface $contentRepository)
{
Expand All @@ -89,14 +64,16 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
/**
* Parse an identifier of an RogerCms object and return it's name and type
*
* @todo Extract to another object injected as a service
*
* @param string $name A CMS object identifier
*
* @return array|false Array of type and name, false if identifier contains no type
*/
public static function parseName($name)
{
$nameParts = explode(':', $name);
if (count($nameParts) < 2) {
if (count($nameParts) != 2) {
return false;
}

Expand All @@ -118,16 +95,13 @@ public static function parseName($name)
* @param string $name The name of the template to load
*
* @return string The template source code
*
* @author Fabrice Bernhard <[email protected]>
* @since 2011-06-22
*/
public function getSource($name)
{
$parsedInfo = self::parseName($name);

if ($parsedInfo === false) {
return $this->fallbackLoader->getSource($name);
throw new Twig_Error_Loader('Unable to parse ' . $name);
} else {
list($type, $name) = $parsedInfo;
}
Expand All @@ -153,7 +127,7 @@ public function getSource($name)
public function getCacheKey($name)
{
if (self::parseName($name) === false) {
return $this->fallbackLoader->getCacheKey($name);
throw new Twig_Error_Loader('Unable to parse ' . $name);
}

return $name;
Expand All @@ -171,7 +145,7 @@ public function getCacheKey($name)
public function isFresh($name, $time)
{
if (self::parseName($name) === false) {
return $this->fallbackLoader->isFresh($name, $time);
throw new Twig_Error_Loader('Unable to parse ' . $name);
}

return true;
Expand Down
10 changes: 7 additions & 3 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
</service>
<service id="roger.twig.loader" class="%roger.twig.loader.class%">
<argument type="service" id="roger.content_repository" />
<argument type="service" id="twig.loader" />
<argument>%roger.twig.loader.fallback_path%</argument>
</service>
<service id="roger.twig.loader_chain" class="Twig_Loader_Chain">
<argument type="collection">
<argument type="service" id="roger.twig.loader" />
<argument type="service" id="twig.loader" />
</argument>
</service>
<service id="roger.twig" class="%twig.class%">
<argument type="service" id="roger.twig.loader" />
<argument type="service" id="roger.twig.loader_chain" />
<argument>%twig.options%</argument>
</service>
<service id="roger.templating" class="%templating.engine.twig.class%">
Expand Down
71 changes: 46 additions & 25 deletions Tests/Extensions/TwigLoaderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,39 @@
* with this source code in the file LICENSE.
*/

namespace Theodo\RogerCmsBundle\Tests\Extensions;

use Theodo\RogerCmsBundle\Tests\Test as TestCase;
use Theodo\RogerCmsBundle\Extensions\Twig\TwigLoaderRepository;

/**
* TwigLoaderRepository extension test class.
*
* @author Vincent Guillon <[email protected]>
* @author Benjamin Grandfond <[email protected]>
*/
namespace Theodo\RogerCmsBundle\Tests\Extensions;

require_once __DIR__.'/../Test.php';

use Theodo\RogerCmsBundle\Tests\Test as TestCase;
use Theodo\RogerCmsBundle\Extensions\Twig\TwigLoaderRepository;

class TwigLoaderRepositoryTest extends TestCase
{
/**
* @var Theodo\RogerCmsBundle\Extensions\Twig\TwigLoaderRepository
*/
protected static $twigLoader;

public function setUp()
{
static::createRogerKernel();

// Load "test" entity manager
static::$twigLoader = static::$kernel->getContainer()->get('roger.twig.loader');
}

/**
* TwigLoader getter
*
* @return Theodo\RogerCmsBundle\Extensions\Twig\TwigLoaderRepository
*/
protected function getTwigLoader()
{
return static::$twigLoader;
}

/**
* Test page status
*
* @group functional
*
* @author Vincent Guillon <[email protected]>
* @since 2011-06-20
*/
public function testGetSource()
{
static::createRogerKernel();

// Load "test" entity manager
static::$twigLoader = static::$kernel->getContainer()->get('roger.twig.loader');

$source = $this->getTwigLoader()->getSource('page:Homepage');
$this->assertRegExp('/id="homepage"/', $source);

Expand Down Expand Up @@ -84,4 +71,38 @@ public function testGetSource()
$this->assertTrue(true);
}
}

/**
* @expectedException \Twig_Error_Loader
* @dataProvider getUnsupportedTemplateNames
*/
public function testThrowsExceptionForStandardTemplates($name)
{
$repository = $this->getMock('Theodo\RogerCmsBundle\Repository\ContentRepositoryInterface');
$repository->expects($this->never())
->method('getSourceByNameAndType');

$loader = new TwigLoaderRepository($repository);

$loader->getSource($name);
}

public function getUnsupportedTemplateNames()
{
return array(
array('AcmeDemoBundle:Default:index.html.twig'),
array('AcmeDemoBundle::layout.html.twig'),
array('::base.html.twig'),
);
}

/**
* TwigLoader getter
*
* @return Theodo\RogerCmsBundle\Extensions\Twig\TwigLoaderRepository
*/
protected function getTwigLoader()
{
return static::$twigLoader;
}
}

0 comments on commit 176560b

Please sign in to comment.