Skip to content

Commit

Permalink
[FrameworkBundle] Create a dedicated template filename parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jul 3, 2012
1 parent 11e8a33 commit aef7663
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 59 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -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().
Expand Up @@ -13,7 +13,7 @@

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;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -78,7 +78,7 @@ private function findTemplatesInFolder($dir)
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;
}
Expand Down
Expand Up @@ -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>
Expand All @@ -25,14 +26,16 @@
<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>
</service>

<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>

Expand Down
@@ -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);
}
}
Expand Up @@ -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;

Expand All @@ -22,7 +22,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateNameParser extends BaseTemplateNameParser
class TemplateNameParser implements TemplateNameParserInterface
{
protected $kernel;
protected $cache;
Expand Down Expand Up @@ -80,26 +80,4 @@ public function parse($name)

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);
}

}
Expand Up @@ -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;

Expand All @@ -38,7 +38,7 @@ public function testFindAllTemplates()
->will($this->returnValue(array('BaseBundle' => new BaseBundle())))
;

$parser = new TemplateNameParser($kernel);
$parser = new TemplateFilenameParser($kernel);

$finder = new TemplateFinder($kernel, $parser, __DIR__.'/../Fixtures/Resources');

Expand Down
@@ -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),
);
}
}
Expand Up @@ -84,30 +84,4 @@ public function getInvalidLogicalNameProvider()
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),
);
}

}

0 comments on commit aef7663

Please sign in to comment.