merged branch vicb/templates (PR #4723)
Commits
-------
aef7663
[FrameworkBundle] Create a dedicated template filename parser
Discussion
----------
[FrameworkBundle] Create a dedicated template filename parser
Bug fix: no
Feature addition: no
Backwards compatibility break: yes
Symfony2 tests pass: yes
related to #3116
This commit is contained in:
commit
23f41a21ca
@ -36,3 +36,5 @@ CHANGELOG
|
||||
start on demand.
|
||||
* Commands cache:warmup and cache:clear (unless --no-warmup is specified) now
|
||||
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\Finder\Finder;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
||||
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||
|
||||
/**
|
||||
@ -31,11 +31,11 @@ class TemplateFinder implements TemplateFinderInterface
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param KernelInterface $kernel A KernelInterface instance
|
||||
* @param TemplateNameParser $parser A TemplateNameParser instance
|
||||
* @param string $rootDir The directory where global templates can be stored
|
||||
* @param KernelInterface $kernel A KernelInterface instance
|
||||
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
||||
* @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->parser = $parser;
|
||||
@ -78,7 +78,7 @@ class TemplateFinder implements TemplateFinderInterface
|
||||
if (is_dir($dir)) {
|
||||
$finder = new Finder();
|
||||
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
||||
$template = $this->parser->parseFromFilename($file->getRelativePathname());
|
||||
$template = $this->parser->parse($file->getRelativePathname());
|
||||
if (false !== $template) {
|
||||
$templates[] = $template;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
<parameters>
|
||||
<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.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.locator.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator</parameter>
|
||||
<parameter key="templating.loader.filesystem.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader</parameter>
|
||||
@ -25,6 +26,8 @@
|
||||
<argument type="service" id="kernel" />
|
||||
</service>
|
||||
|
||||
<service id="templating.filename_parser" class="%templating.filename_parser.class%" />
|
||||
|
||||
<service id="templating.locator" class="%templating.locator.class%" public="false">
|
||||
<argument type="service" id="file_locator" />
|
||||
<argument>%kernel.cache_dir%</argument>
|
||||
@ -32,7 +35,7 @@
|
||||
|
||||
<service id="templating.finder" class="%templating.finder.class%" public="false">
|
||||
<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>
|
||||
</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;
|
||||
|
||||
use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
|
||||
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||
use Symfony\Component\Templating\TemplateReferenceInterface;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
@ -22,7 +22,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class TemplateNameParser extends BaseTemplateNameParser
|
||||
class TemplateNameParser implements TemplateNameParserInterface
|
||||
{
|
||||
protected $kernel;
|
||||
protected $cache;
|
||||
@ -80,26 +80,4 @@ class TemplateNameParser extends BaseTemplateNameParser
|
||||
|
||||
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;
|
||||
|
||||
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\Tests\Fixtures\BaseBundle\BaseBundle;
|
||||
|
||||
@ -38,7 +38,7 @@ class TemplateFinderTest extends TestCase
|
||||
->will($this->returnValue(array('BaseBundle' => new BaseBundle())))
|
||||
;
|
||||
|
||||
$parser = new TemplateNameParser($kernel);
|
||||
$parser = new TemplateFilenameParser($kernel);
|
||||
|
||||
$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'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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