forked from theodo/TheodoRogerCmsBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't try to load real templates using TwigLoaderRepository
- Loading branch information
1 parent
a370d59
commit 176560b
Showing
3 changed files
with
63 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
|
@@ -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; | ||
} | ||
} |