Skip to content

Commit

Permalink
Merge remote branch 'hason/logicalname'
Browse files Browse the repository at this point in the history
* hason/logicalname:
  [Templating] changed __toString method (synonym for getLogicalName method)
  [Templating] fixed phpdoc a test
  [FrameworkBundle] added getLogicalName() method to TemplateReference
  [Templating] added method to return the template logical name, added test
  • Loading branch information
fabpot committed Apr 8, 2011
2 parents 7018335 + 185144b commit 5558ee2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct($bundle = null, $controller = null, $name = null, $f
* Returns the path to the template
* - as a path when the template is not part of a bundle
* - as a resource when the template is part of a bundle
*
*
* @return string A path to the template or a resource
*/
public function getPath()
Expand All @@ -46,4 +46,15 @@ public function getPath()
return empty($this->parameters['bundle']) ? 'views/'.$path : '@'.$this->get('bundle').'/Resources/views/'.$path;
}

/**
* {@inheritdoc}
*/
public function getLogicalName()
{
$parts = sprintf('%s:%s:', $this->get('bundle'), $this->get('controller'));
$elements = sprintf('%s.%s.%s', $this->get('name'), $this->get('format'), $this->get('engine'));

return $parts . $elements;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function testParse($name, $ref)
$template = $this->parser->parse($name);

$this->assertEquals($template->getSignature(), $ref->getSignature());
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
$this->assertEquals($template->getLogicalName(), $name);
}

public function getLogicalNameToTemplateProvider()
Expand Down Expand Up @@ -82,7 +84,7 @@ public function getInvalidLogicalNameProvider()
public function testParseFromFilename($file, $ref)
{
$template = $this->parser->parseFromFilename($file);

if ($ref === false) {
$this->assertFalse($template);
} else {
Expand Down
43 changes: 37 additions & 6 deletions src/Symfony/Component/Templating/TemplateReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ public function __construct($name = null, $engine = null)

public function __toString()
{
return json_encode($this->parameters);
return $this->getLogicalName();
}

/**
* {@inheritDoc}
* Returns the template signature
*
* @return string A UID for the template
*/
public function getSignature()
{
return md5(serialize($this->parameters));
}

/**
* {@inheritDoc}
* Sets a template parameter.
*
* @param string $name The parameter name
* @param string $value The parameter value
*
* @return TemplateReferenceInterface The TemplateReferenceInterface instance
*
* @throws \InvalidArgumentException if the parameter is not defined
*/
public function set($name, $value)
{
Expand All @@ -56,7 +65,13 @@ public function set($name, $value)
}

/**
* {@inheritDoc}
* Gets a template parameter.
*
* @param string $name The parameter name
*
* @return string The parameter value
*
* @throws \InvalidArgumentException if the parameter is not defined
*/
public function get($name)
{
Expand All @@ -68,18 +83,34 @@ public function get($name)
}

/**
* {@inheritDoc}
* Gets the template parameters.
*
* @return array An array of parameters
*/
public function all()
{
return $this->parameters;
}

/**
* {@inheritDoc}
* Returns the path to the template.
*
* By default, it just returns the template name.
*
* @return string A path to the template or a resource
*/
public function getPath()
{
return $this->parameters['name'];
}

/**
* Returns the template name
*
* @return string The template name
*/
public function getLogicalName()
{
return $this->parameters['name'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ function getSignature();
* @return string A path to the template or a resource
*/
function getPath();

/**
* Returns the template name
*
* @return string The template name
*/
function getLogicalName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Tests\Component\Templating;

use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\TemplateReference;

class TemplateNameParserTest extends \PHPUnit_Framework_TestCase
{
protected $parser;

protected function setUp()
{
$this->parser = new TemplateNameParser();
}

protected function tearDown()
{
unset($this->parser);
}

/**
* @dataProvider getLogicalNameToTemplateProvider
*/
public function testParse($name, $ref)
{
$template = $this->parser->parse($name);

$this->assertEquals($template->getSignature(), $ref->getSignature());
$this->assertEquals($template->getLogicalName(), $name);
}

public function getLogicalNameToTemplateProvider()
{
return array(
array('/path/to/section/name.engine', new TemplateReference('/path/to/section/name.engine', 'engine')),
array('name.engine', new TemplateReference('name.engine', 'engine')),
array('name', new TemplateReference('name')),
);
}
}

0 comments on commit 5558ee2

Please sign in to comment.