[Templating] remove Engine as a dependency for the Helper objects

This commit is contained in:
Fabien Potencier 2010-03-13 14:42:33 +01:00
parent bce240badd
commit bf08289e18
6 changed files with 43 additions and 25 deletions

View File

@ -182,7 +182,7 @@ class Engine
$this->helpers[$alias] = $helper; $this->helpers[$alias] = $helper;
} }
$helper->setEngine($this); $helper->setCharset($this->charset);
} }
/** /**

View File

@ -2,8 +2,6 @@
namespace Symfony\Components\Templating\Helper; namespace Symfony\Components\Templating\Helper;
use Symfony\Components\Templating\Engine;
/* /*
* This file is part of the symfony package. * This file is part of the symfony package.
* *
@ -22,25 +20,25 @@ use Symfony\Components\Templating\Engine;
*/ */
abstract class Helper implements HelperInterface abstract class Helper implements HelperInterface
{ {
protected $engine; protected $charset = 'UTF-8';
/** /**
* Sets the engine associated with this helper. * Sets the default charset.
* *
* @param Engine $engine A Engine instance * @param string $charset The charset
*/ */
public function setEngine(Engine $engine = null) public function setCharset($charset)
{ {
$this->engine = $engine; $this->charset = $charset;
} }
/** /**
* Gets the engine associated with this helper. * Gets the default charset.
* *
* @return Engine A Engine instance * @return string The default charset
*/ */
public function getEngine() public function getCharset()
{ {
return $this->engine; return $this->charset;
} }
} }

View File

@ -2,8 +2,6 @@
namespace Symfony\Components\Templating\Helper; namespace Symfony\Components\Templating\Helper;
use Symfony\Components\Templating\Engine;
/* /*
* This file is part of the symfony package. * This file is part of the symfony package.
* *
@ -30,16 +28,16 @@ interface HelperInterface
function getName(); function getName();
/** /**
* Sets the engine associated with this helper. * Sets the default charset.
* *
* @param Engine $engine A Engine instance * @param string $charset The charset
*/ */
function setEngine(Engine $engine = null); function setCharset($charset);
/** /**
* Gets the engine associated with this helper. * Gets the default charset.
* *
* @return Engine A Engine instance * @return string The default charset
*/ */
function getEngine(); function getCharset();
} }

View File

@ -28,6 +28,17 @@ namespace Symfony\Components\Templating\Helper;
class JavascriptsHelper extends Helper class JavascriptsHelper extends Helper
{ {
protected $javascripts = array(); protected $javascripts = array();
protected $assetHelper;
/**
* Constructor.
*
* @param AssetsHelper $assetHelper A AssetsHelper instance
*/
public function __construct(AssetsHelper $assetHelper)
{
$this->assetHelper = $assetHelper;
}
/** /**
* Adds a JavaScript file. * Adds a JavaScript file.
@ -37,7 +48,7 @@ class JavascriptsHelper extends Helper
*/ */
public function add($javascript, $attributes = array()) public function add($javascript, $attributes = array())
{ {
$this->javascripts[$this->engine->get('assets')->getUrl($javascript)] = $attributes; $this->javascripts[$this->assetHelper->getUrl($javascript)] = $attributes;
} }
/** /**
@ -63,7 +74,7 @@ class JavascriptsHelper extends Helper
$atts = ''; $atts = '';
foreach ($attributes as $key => $value) foreach ($attributes as $key => $value)
{ {
$atts .= ' '.sprintf('%s="%s"', $key, $this->engine->escape($value)); $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
} }
$html .= sprintf('<script type="text/javascript" src="%s"%s></script>', $path, $atts)."\n"; $html .= sprintf('<script type="text/javascript" src="%s"%s></script>', $path, $atts)."\n";

View File

@ -28,6 +28,17 @@ namespace Symfony\Components\Templating\Helper;
class StylesheetsHelper extends Helper class StylesheetsHelper extends Helper
{ {
protected $stylesheets = array(); protected $stylesheets = array();
protected $assetHelper;
/**
* Constructor.
*
* @param AssetsHelper $assetHelper A AssetsHelper instance
*/
public function __construct(AssetsHelper $assetHelper)
{
$this->assetHelper = $assetHelper;
}
/** /**
* Adds a stylesheets file. * Adds a stylesheets file.
@ -37,7 +48,7 @@ class StylesheetsHelper extends Helper
*/ */
public function add($stylesheet, $attributes = array()) public function add($stylesheet, $attributes = array())
{ {
$this->stylesheets[$this->engine->get('assets')->getUrl($stylesheet)] = $attributes; $this->stylesheets[$this->assetHelper->getUrl($stylesheet)] = $attributes;
} }
/** /**
@ -63,7 +74,7 @@ class StylesheetsHelper extends Helper
$atts = ''; $atts = '';
foreach ($attributes as $key => $value) foreach ($attributes as $key => $value)
{ {
$atts .= ' '.sprintf('%s="%s"', $key, $this->engine->escape($value)); $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
} }
$html .= sprintf('<link href="%s" rel="stylesheet" type="text/css"%s />', $path, $atts)."\n"; $html .= sprintf('<link href="%s" rel="stylesheet" type="text/css"%s />', $path, $atts)."\n";

View File

@ -87,7 +87,7 @@ class Engine extends BaseEngine
if (is_string($this->helpers[$name])) if (is_string($this->helpers[$name]))
{ {
$this->helpers[$name] = $this->container->getService('templating.helper.'.$name); $this->helpers[$name] = $this->container->getService('templating.helper.'.$name);
$this->helpers[$name]->setEngine($this); $this->helpers[$name]->setCharset($this->charset);
} }
return $this->helpers[$name]; return $this->helpers[$name];