[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;
}
$helper->setEngine($this);
$helper->setCharset($this->charset);
}
/**

View File

@ -2,8 +2,6 @@
namespace Symfony\Components\Templating\Helper;
use Symfony\Components\Templating\Engine;
/*
* This file is part of the symfony package.
*
@ -22,25 +20,25 @@ use Symfony\Components\Templating\Engine;
*/
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;
use Symfony\Components\Templating\Engine;
/*
* This file is part of the symfony package.
*
@ -30,16 +28,16 @@ interface HelperInterface
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
{
protected $javascripts = array();
protected $assetHelper;
/**
* Constructor.
*
* @param AssetsHelper $assetHelper A AssetsHelper instance
*/
public function __construct(AssetsHelper $assetHelper)
{
$this->assetHelper = $assetHelper;
}
/**
* Adds a JavaScript file.
@ -37,7 +48,7 @@ class JavascriptsHelper extends Helper
*/
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 = '';
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";

View File

@ -28,6 +28,17 @@ namespace Symfony\Components\Templating\Helper;
class StylesheetsHelper extends Helper
{
protected $stylesheets = array();
protected $assetHelper;
/**
* Constructor.
*
* @param AssetsHelper $assetHelper A AssetsHelper instance
*/
public function __construct(AssetsHelper $assetHelper)
{
$this->assetHelper = $assetHelper;
}
/**
* Adds a stylesheets file.
@ -37,7 +48,7 @@ class StylesheetsHelper extends Helper
*/
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 = '';
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";

View File

@ -87,7 +87,7 @@ class Engine extends BaseEngine
if (is_string($this->helpers[$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];