Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Create a dedicated template filename parser
- Loading branch information
Showing
8 changed files
with
112 additions
and
59 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
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
40 changes: 40 additions & 0 deletions
40
src/Symfony/Bundle/FrameworkBundle/Templating/TemplateFilenameParser.php
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateFilenameParserTest.php
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 |
---|---|---|
@@ -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), | ||
); | ||
} | ||
} |
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