[FrameworkBundle] Create a dedicated template filename parser
This commit is contained in:
parent
11e8a33c7c
commit
aef7663676
@ -36,3 +36,5 @@ CHANGELOG
|
|||||||
start on demand.
|
start on demand.
|
||||||
* Commands cache:warmup and cache:clear (unless --no-warmup is specified) now
|
* Commands cache:warmup and cache:clear (unless --no-warmup is specified) now
|
||||||
create the class cache.
|
create the class cache.
|
||||||
|
* [BC BREAK] TemplateNameParser::parseFromFilename() has been moved to a dedicated
|
||||||
|
parser: TemplateFilenameParser::parse().
|
||||||
|
@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
|||||||
|
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
use Symfony\Component\Finder\Finder;
|
use Symfony\Component\Finder\Finder;
|
||||||
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,11 +31,11 @@ class TemplateFinder implements TemplateFinderInterface
|
|||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param KernelInterface $kernel A KernelInterface instance
|
* @param KernelInterface $kernel A KernelInterface instance
|
||||||
* @param TemplateNameParser $parser A TemplateNameParser instance
|
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
||||||
* @param string $rootDir The directory where global templates can be stored
|
* @param string $rootDir The directory where global templates can be stored
|
||||||
*/
|
*/
|
||||||
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, $rootDir)
|
public function __construct(KernelInterface $kernel, TemplateNameParserInterface $parser, $rootDir)
|
||||||
{
|
{
|
||||||
$this->kernel = $kernel;
|
$this->kernel = $kernel;
|
||||||
$this->parser = $parser;
|
$this->parser = $parser;
|
||||||
@ -78,7 +78,7 @@ class TemplateFinder implements TemplateFinderInterface
|
|||||||
if (is_dir($dir)) {
|
if (is_dir($dir)) {
|
||||||
$finder = new Finder();
|
$finder = new Finder();
|
||||||
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
||||||
$template = $this->parser->parseFromFilename($file->getRelativePathname());
|
$template = $this->parser->parse($file->getRelativePathname());
|
||||||
if (false !== $template) {
|
if (false !== $template) {
|
||||||
$templates[] = $template;
|
$templates[] = $template;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="templating.engine.delegating.class">Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine</parameter>
|
<parameter key="templating.engine.delegating.class">Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine</parameter>
|
||||||
<parameter key="templating.name_parser.class">Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser</parameter>
|
<parameter key="templating.name_parser.class">Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser</parameter>
|
||||||
|
<parameter key="templating.filename_parser.class">Symfony\Bundle\FrameworkBundle\Templating\TemplateFilenameParser</parameter>
|
||||||
<parameter key="templating.cache_warmer.template_paths.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer</parameter>
|
<parameter key="templating.cache_warmer.template_paths.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer</parameter>
|
||||||
<parameter key="templating.locator.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator</parameter>
|
<parameter key="templating.locator.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator</parameter>
|
||||||
<parameter key="templating.loader.filesystem.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader</parameter>
|
<parameter key="templating.loader.filesystem.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader</parameter>
|
||||||
@ -25,6 +26,8 @@
|
|||||||
<argument type="service" id="kernel" />
|
<argument type="service" id="kernel" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<service id="templating.filename_parser" class="%templating.filename_parser.class%" />
|
||||||
|
|
||||||
<service id="templating.locator" class="%templating.locator.class%" public="false">
|
<service id="templating.locator" class="%templating.locator.class%" public="false">
|
||||||
<argument type="service" id="file_locator" />
|
<argument type="service" id="file_locator" />
|
||||||
<argument>%kernel.cache_dir%</argument>
|
<argument>%kernel.cache_dir%</argument>
|
||||||
@ -32,7 +35,7 @@
|
|||||||
|
|
||||||
<service id="templating.finder" class="%templating.finder.class%" public="false">
|
<service id="templating.finder" class="%templating.finder.class%" public="false">
|
||||||
<argument type="service" id="kernel" />
|
<argument type="service" id="kernel" />
|
||||||
<argument type="service" id="templating.name_parser" />
|
<argument type="service" id="templating.filename_parser" />
|
||||||
<argument>%kernel.root_dir%/Resources</argument>
|
<argument>%kernel.root_dir%/Resources</argument>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
||||||
|
|
||||||
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TemplateFilenameParser converts template filenames to
|
||||||
|
* TemplateReferenceInterface instances.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*/
|
||||||
|
class TemplateFilenameParser implements TemplateNameParserInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function parse($file)
|
||||||
|
{
|
||||||
|
$parts = explode('/', strtr($file, '\\', '/'));
|
||||||
|
|
||||||
|
$elements = explode('.', array_pop($parts));
|
||||||
|
if (3 > count($elements)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$engine = array_pop($elements);
|
||||||
|
$format = array_pop($elements);
|
||||||
|
|
||||||
|
return new TemplateReference('', implode('/', $parts), implode('.', $elements), $format, $engine);
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
||||||
|
|
||||||
use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||||
use Symfony\Component\Templating\TemplateReferenceInterface;
|
use Symfony\Component\Templating\TemplateReferenceInterface;
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
|
|||||||
*
|
*
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
*/
|
*/
|
||||||
class TemplateNameParser extends BaseTemplateNameParser
|
class TemplateNameParser implements TemplateNameParserInterface
|
||||||
{
|
{
|
||||||
protected $kernel;
|
protected $kernel;
|
||||||
protected $cache;
|
protected $cache;
|
||||||
@ -80,26 +80,4 @@ class TemplateNameParser extends BaseTemplateNameParser
|
|||||||
|
|
||||||
return $this->cache[$name] = $template;
|
return $this->cache[$name] = $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a filename to a template.
|
|
||||||
*
|
|
||||||
* @param string $file The filename
|
|
||||||
*
|
|
||||||
* @return TemplateReferenceInterface A template
|
|
||||||
*/
|
|
||||||
public function parseFromFilename($file)
|
|
||||||
{
|
|
||||||
$parts = explode('/', strtr($file, '\\', '/'));
|
|
||||||
|
|
||||||
$elements = explode('.', array_pop($parts));
|
|
||||||
if (3 > count($elements)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$engine = array_pop($elements);
|
|
||||||
$format = array_pop($elements);
|
|
||||||
|
|
||||||
return new TemplateReference('', implode('/', $parts), implode('.', $elements), $format, $engine);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
|
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||||
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateFilenameParser;
|
||||||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder;
|
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder;
|
||||||
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BaseBundle\BaseBundle;
|
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BaseBundle\BaseBundle;
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ class TemplateFinderTest extends TestCase
|
|||||||
->will($this->returnValue(array('BaseBundle' => new BaseBundle())))
|
->will($this->returnValue(array('BaseBundle' => new BaseBundle())))
|
||||||
;
|
;
|
||||||
|
|
||||||
$parser = new TemplateNameParser($kernel);
|
$parser = new TemplateFilenameParser($kernel);
|
||||||
|
|
||||||
$finder = new TemplateFinder($kernel, $parser, __DIR__.'/../Fixtures/Resources');
|
$finder = new TemplateFinder($kernel, $parser, __DIR__.'/../Fixtures/Resources');
|
||||||
|
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateFilenameParser;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
|
||||||
|
|
||||||
|
class TemplateFilenameParserTest extends TestCase
|
||||||
|
{
|
||||||
|
protected $parser;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->parser = new TemplateFilenameParser();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
$this->parser = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getFilenameToTemplateProvider
|
||||||
|
*/
|
||||||
|
public function testParseFromFilename($file, $ref)
|
||||||
|
{
|
||||||
|
$template = $this->parser->parse($file);
|
||||||
|
|
||||||
|
if ($ref === false) {
|
||||||
|
$this->assertFalse($template);
|
||||||
|
} else {
|
||||||
|
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilenameToTemplateProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('/path/to/section/name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
|
||||||
|
array('\\path\\to\\section\\name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
|
||||||
|
array('name.format.engine', new TemplateReference('', '', 'name', 'format', 'engine')),
|
||||||
|
array('name.format', false),
|
||||||
|
array('name', false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -84,30 +84,4 @@ class TemplateNameParserTest extends TestCase
|
|||||||
array('FooBundle:Post:foo:bar'),
|
array('FooBundle:Post:foo:bar'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider getFilenameToTemplateProvider
|
|
||||||
*/
|
|
||||||
public function testParseFromFilename($file, $ref)
|
|
||||||
{
|
|
||||||
$template = $this->parser->parseFromFilename($file);
|
|
||||||
|
|
||||||
if ($ref === false) {
|
|
||||||
$this->assertFalse($template);
|
|
||||||
} else {
|
|
||||||
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getFilenameToTemplateProvider()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
array('/path/to/section/name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
|
|
||||||
array('\\path\\to\\section\\name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
|
|
||||||
array('name.format.engine', new TemplateReference('', '', 'name', 'format', 'engine')),
|
|
||||||
array('name.format', false),
|
|
||||||
array('name', false),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user