2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Templating;
|
|
|
|
|
|
|
|
use Symfony\Component\Templating\Storage\Storage;
|
|
|
|
use Symfony\Component\Templating\Storage\FileStorage;
|
|
|
|
use Symfony\Component\Templating\Storage\StringStorage;
|
|
|
|
use Symfony\Component\Templating\Helper\HelperInterface;
|
|
|
|
use Symfony\Component\Templating\Loader\LoaderInterface;
|
|
|
|
|
2011-08-30 06:43:00 +01:00
|
|
|
if (!defined('ENT_SUBSTITUTE')) {
|
|
|
|
define('ENT_SUBSTITUTE', 8);
|
|
|
|
}
|
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
2011-01-13 13:58:17 +00:00
|
|
|
* PhpEngine is an engine able to render PHP templates.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
2011-12-31 13:50:33 +00:00
|
|
|
class PhpEngine implements EngineInterface, \ArrayAccess
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $loader;
|
|
|
|
protected $current;
|
2014-04-09 17:01:45 +01:00
|
|
|
/**
|
|
|
|
* @var HelperInterface[]
|
|
|
|
*/
|
2013-11-10 17:06:47 +00:00
|
|
|
protected $helpers = array();
|
|
|
|
protected $parents = array();
|
|
|
|
protected $stack = array();
|
|
|
|
protected $charset = 'UTF-8';
|
|
|
|
protected $cache = array();
|
|
|
|
protected $escapers = array();
|
|
|
|
protected static $escaperCache = array();
|
|
|
|
protected $globals = array();
|
2011-01-26 13:53:12 +00:00
|
|
|
protected $parser;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-05-02 18:04:46 +01:00
|
|
|
private $evalTemplate;
|
|
|
|
private $evalParameters;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-26 13:53:12 +00:00
|
|
|
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
|
|
|
* @param LoaderInterface $loader A loader instance
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param HelperInterface[] $helpers An array of helper instances
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-26 13:53:12 +00:00
|
|
|
public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = array())
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-26 13:53:12 +00:00
|
|
|
$this->parser = $parser;
|
2011-01-13 13:58:17 +00:00
|
|
|
$this->loader = $loader;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-11-10 17:06:47 +00:00
|
|
|
$this->addHelpers($helpers);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-01-06 19:35:08 +00:00
|
|
|
$this->initializeEscapers();
|
|
|
|
foreach ($this->escapers as $context => $escaper) {
|
|
|
|
$this->setEscaper($context, $escaper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-01-13 13:58:17 +00:00
|
|
|
* @throws \InvalidArgumentException if the template does not exist
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function render($name, array $parameters = array())
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-02-10 17:20:44 +00:00
|
|
|
$storage = $this->load($name);
|
2013-07-30 13:08:17 +01:00
|
|
|
$key = hash('sha256', serialize($storage));
|
2011-01-26 14:34:10 +00:00
|
|
|
$this->current = $key;
|
|
|
|
$this->parents[$key] = null;
|
2010-10-21 07:57:23 +01:00
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
// attach the global variables
|
2010-12-20 12:05:28 +00:00
|
|
|
$parameters = array_replace($this->getGlobals(), $parameters);
|
2010-05-06 12:25:53 +01:00
|
|
|
// render
|
2011-02-10 17:20:44 +00:00
|
|
|
if (false === $content = $this->evaluate($storage, $parameters)) {
|
2011-04-08 18:50:57 +01:00
|
|
|
throw new \RuntimeException(sprintf('The template "%s" cannot be rendered.', $this->parser->parse($name)));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decorator
|
2011-01-26 14:34:10 +00:00
|
|
|
if ($this->parents[$key]) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$slots = $this->get('slots');
|
|
|
|
$this->stack[] = $slots->get('_content');
|
|
|
|
$slots->set('_content', $content);
|
|
|
|
|
2011-01-26 14:34:10 +00:00
|
|
|
$content = $this->render($this->parents[$key], $parameters);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$slots->set('_content', array_pop($this->stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-08-29 15:09:02 +01:00
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-29 15:09:02 +01:00
|
|
|
*/
|
|
|
|
public function exists($name)
|
|
|
|
{
|
2011-01-13 12:18:48 +00:00
|
|
|
try {
|
|
|
|
$this->load($name);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-11-21 08:33:03 +00:00
|
|
|
}
|
2010-08-29 15:09:02 +01:00
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
|
|
|
public function supports($name)
|
|
|
|
{
|
2011-01-26 13:53:12 +00:00
|
|
|
$template = $this->parser->parse($name);
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
return 'php' === $template->get('engine');
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates a template.
|
|
|
|
*
|
|
|
|
* @param Storage $template The template to render
|
|
|
|
* @param array $parameters An array of parameters to pass to the template
|
|
|
|
*
|
|
|
|
* @return string|false The evaluated template, or false if the engine is unable to render the template
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
|
|
|
protected function evaluate(Storage $template, array $parameters = array())
|
|
|
|
{
|
2013-05-02 18:04:46 +01:00
|
|
|
$this->evalTemplate = $template;
|
|
|
|
$this->evalParameters = $parameters;
|
|
|
|
unset($template, $parameters);
|
2012-01-17 09:55:12 +00:00
|
|
|
|
2013-05-02 18:04:46 +01:00
|
|
|
if (isset($this->evalParameters['this'])) {
|
|
|
|
throw new \InvalidArgumentException('Invalid parameter (this)');
|
|
|
|
}
|
|
|
|
if (isset($this->evalParameters['view'])) {
|
|
|
|
throw new \InvalidArgumentException('Invalid parameter (view)');
|
2012-01-09 11:40:00 +00:00
|
|
|
}
|
2012-01-17 09:55:12 +00:00
|
|
|
|
2013-05-02 18:04:46 +01:00
|
|
|
$view = $this;
|
|
|
|
if ($this->evalTemplate instanceof FileStorage) {
|
|
|
|
extract($this->evalParameters, EXTR_SKIP);
|
|
|
|
$this->evalParameters = null;
|
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
ob_start();
|
2013-05-02 18:04:46 +01:00
|
|
|
require $this->evalTemplate;
|
|
|
|
|
|
|
|
$this->evalTemplate = null;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
return ob_get_clean();
|
2013-05-02 18:04:46 +01:00
|
|
|
} elseif ($this->evalTemplate instanceof StringStorage) {
|
|
|
|
extract($this->evalParameters, EXTR_SKIP);
|
|
|
|
$this->evalParameters = null;
|
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
ob_start();
|
2013-05-02 18:04:46 +01:00
|
|
|
eval('; ?>'.$this->evalTemplate.'<?php ;');
|
|
|
|
|
|
|
|
$this->evalTemplate = null;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a helper value.
|
|
|
|
*
|
|
|
|
* @param string $name The helper name
|
|
|
|
*
|
2013-04-08 13:26:52 +01:00
|
|
|
* @return HelperInterface The helper value
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the helper is not defined
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-08-19 14:52:47 +01:00
|
|
|
public function offsetGet($name)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-04-11 17:20:47 +01:00
|
|
|
return $this->get($name);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-20 12:50:14 +01:00
|
|
|
/**
|
2010-05-25 17:16:08 +01:00
|
|
|
* Returns true if the helper is defined.
|
2010-05-20 12:50:14 +01:00
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $name The helper name
|
2010-05-20 12:50:14 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool true if the helper is defined, false otherwise
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-20 12:50:14 +01:00
|
|
|
*/
|
2010-08-19 14:52:47 +01:00
|
|
|
public function offsetExists($name)
|
2010-05-20 12:50:14 +01:00
|
|
|
{
|
|
|
|
return isset($this->helpers[$name]);
|
|
|
|
}
|
|
|
|
|
2010-08-19 14:52:47 +01:00
|
|
|
/**
|
|
|
|
* Sets a helper.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param HelperInterface $name The helper instance
|
|
|
|
* @param string $value An alias
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-19 14:52:47 +01:00
|
|
|
*/
|
|
|
|
public function offsetSet($name, $value)
|
|
|
|
{
|
|
|
|
$this->set($name, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a helper.
|
|
|
|
*
|
|
|
|
* @param string $name The helper name
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @throws \LogicException
|
|
|
|
*
|
2011-07-20 09:27:25 +01:00
|
|
|
* @api
|
2010-08-19 14:52:47 +01:00
|
|
|
*/
|
|
|
|
public function offsetUnset($name)
|
|
|
|
{
|
|
|
|
throw new \LogicException(sprintf('You can\'t unset a helper (%s).', $name));
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-11-01 15:08:59 +00:00
|
|
|
* Adds some helpers.
|
2012-12-11 10:49:22 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param HelperInterface[] $helpers An array of helper
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-04-11 17:20:47 +01:00
|
|
|
public function addHelpers(array $helpers)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($helpers as $alias => $helper) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->set($helper, is_int($alias) ? null : $alias);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-07 13:11:46 +00:00
|
|
|
/**
|
|
|
|
* Sets the helpers.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param HelperInterface[] $helpers An array of helper
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-07 13:11:46 +00:00
|
|
|
*/
|
2011-01-13 13:58:17 +00:00
|
|
|
public function setHelpers(array $helpers)
|
|
|
|
{
|
|
|
|
$this->helpers = array();
|
|
|
|
$this->addHelpers($helpers);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets a helper.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param HelperInterface $helper The helper instance
|
|
|
|
* @param string $alias An alias
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function set(HelperInterface $helper, $alias = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers[$helper->getName()] = $helper;
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $alias) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers[$alias] = $helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
$helper->setCharset($this->charset);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the helper if defined.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $name The helper name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool true if the helper is defined, false otherwise
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function has($name)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return isset($this->helpers[$name]);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a helper value.
|
|
|
|
*
|
|
|
|
* @param string $name The helper name
|
|
|
|
*
|
|
|
|
* @return HelperInterface The helper instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the helper is not defined
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function get($name)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($this->helpers[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->helpers[$name];
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Decorates the current template with another one.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $template The decorator logical name
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function extend($template)
|
|
|
|
{
|
|
|
|
$this->parents[$this->current] = $template;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Escapes a string by using the current charset.
|
|
|
|
*
|
2011-02-07 13:11:46 +00:00
|
|
|
* @param mixed $value A variable to escape
|
|
|
|
* @param string $context The context name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-11-23 09:22:48 +00:00
|
|
|
* @return string The escaped value
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-11-23 09:22:48 +00:00
|
|
|
public function escape($value, $context = 'html')
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2012-07-30 21:58:40 +01:00
|
|
|
if (is_numeric($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2012-07-16 17:29:06 +01:00
|
|
|
// If we deal with a scalar value, we can cache the result to increase
|
|
|
|
// the performance when the same value is escaped multiple times (e.g. loops)
|
|
|
|
if (is_scalar($value)) {
|
2012-07-30 21:58:40 +01:00
|
|
|
if (!isset(self::$escaperCache[$context][$value])) {
|
|
|
|
self::$escaperCache[$context][$value] = call_user_func($this->getEscaper($context), $value);
|
2012-07-16 17:29:06 +01:00
|
|
|
}
|
|
|
|
|
2012-07-30 21:58:40 +01:00
|
|
|
return self::$escaperCache[$context][$value];
|
2012-07-16 17:29:06 +01:00
|
|
|
}
|
|
|
|
|
2010-11-23 09:22:48 +00:00
|
|
|
return call_user_func($this->getEscaper($context), $value);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the charset to use.
|
|
|
|
*
|
|
|
|
* @param string $charset The charset
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setCharset($charset)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->charset = $charset;
|
2014-04-09 17:01:45 +01:00
|
|
|
|
|
|
|
foreach ($this->helpers as $helper) {
|
|
|
|
$helper->setCharset($this->charset);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the current charset.
|
|
|
|
*
|
|
|
|
* @return string The current charset
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getCharset()
|
2010-02-05 14:54:34 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->charset;
|
2010-02-05 14:54:34 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 09:22:48 +00:00
|
|
|
/**
|
|
|
|
* Adds an escaper for the given context.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param string $context The escaper context (html, js, ...)
|
2010-11-23 09:22:48 +00:00
|
|
|
* @param mixed $escaper A PHP callable
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-11-23 09:22:48 +00:00
|
|
|
*/
|
|
|
|
public function setEscaper($context, $escaper)
|
|
|
|
{
|
|
|
|
$this->escapers[$context] = $escaper;
|
2012-07-30 21:58:40 +01:00
|
|
|
self::$escaperCache[$context] = array();
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an escaper for a given context.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param string $context The context name
|
2010-11-23 09:22:48 +00:00
|
|
|
*
|
|
|
|
* @return mixed $escaper A PHP callable
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
2011-07-20 09:27:25 +01:00
|
|
|
* @api
|
2010-11-23 09:22:48 +00:00
|
|
|
*/
|
|
|
|
public function getEscaper($context)
|
|
|
|
{
|
|
|
|
if (!isset($this->escapers[$context])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('No registered escaper for context "%s".', $context));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->escapers[$context];
|
|
|
|
}
|
|
|
|
|
2010-12-20 12:05:28 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param mixed $value
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-20 12:05:28 +00:00
|
|
|
*/
|
|
|
|
public function addGlobal($name, $value)
|
|
|
|
{
|
|
|
|
$this->globals[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-06 10:20:48 +00:00
|
|
|
* Returns the assigned globals.
|
2010-12-20 12:05:28 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2011-07-20 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-20 12:05:28 +00:00
|
|
|
*/
|
|
|
|
public function getGlobals()
|
|
|
|
{
|
|
|
|
return $this->globals;
|
|
|
|
}
|
|
|
|
|
2010-11-23 09:22:48 +00:00
|
|
|
/**
|
|
|
|
* Initializes the built-in escapers.
|
|
|
|
*
|
|
|
|
* Each function specifies a way for applying a transformation to a string
|
|
|
|
* passed to it. The purpose is for the string to be "escaped" so it is
|
|
|
|
* suitable for the format it is being displayed in.
|
|
|
|
*
|
|
|
|
* For example, the string: "It's required that you enter a username & password.\n"
|
|
|
|
* If this were to be displayed as HTML it would be sensible to turn the
|
|
|
|
* ampersand into '&' and the apostrophe into '&aps;'. However if it were
|
|
|
|
* going to be used as a string in JavaScript to be displayed in an alert box
|
|
|
|
* it would be right to leave the string as-is, but c-escape the apostrophe and
|
|
|
|
* the new line.
|
|
|
|
*
|
|
|
|
* For each function there is a define to avoid problems with strings being
|
|
|
|
* incorrectly specified.
|
|
|
|
*/
|
|
|
|
protected function initializeEscapers()
|
|
|
|
{
|
|
|
|
$that = $this;
|
|
|
|
|
|
|
|
$this->escapers = array(
|
|
|
|
'html' =>
|
|
|
|
/**
|
|
|
|
* Runs the PHP function htmlspecialchars on the value passed.
|
|
|
|
*
|
|
|
|
* @param string $value the value to escape
|
|
|
|
*
|
|
|
|
* @return string the escaped value
|
|
|
|
*/
|
2012-05-20 17:15:10 +01:00
|
|
|
function ($value) use ($that) {
|
2011-01-21 01:39:28 +00:00
|
|
|
// Numbers and Boolean values get turned into strings which can cause problems
|
2010-11-23 09:22:48 +00:00
|
|
|
// with type comparisons (e.g. === or is_int() etc).
|
2011-08-30 06:43:00 +01:00
|
|
|
return is_string($value) ? htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $that->getCharset(), false) : $value;
|
2010-11-23 09:22:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'js' =>
|
|
|
|
/**
|
|
|
|
* A function that escape all non-alphanumeric characters
|
|
|
|
* into their \xHH or \uHHHH representations
|
|
|
|
*
|
|
|
|
* @param string $value the value to escape
|
|
|
|
* @return string the escaped value
|
|
|
|
*/
|
2012-05-20 17:15:10 +01:00
|
|
|
function ($value) use ($that) {
|
2010-11-23 09:22:48 +00:00
|
|
|
if ('UTF-8' != $that->getCharset()) {
|
2011-03-03 22:00:42 +00:00
|
|
|
$value = $that->convertEncoding($value, 'UTF-8', $that->getCharset());
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 17:15:10 +01:00
|
|
|
$callback = function ($matches) use ($that) {
|
2010-11-23 09:22:48 +00:00
|
|
|
$char = $matches[0];
|
|
|
|
|
|
|
|
// \xHH
|
|
|
|
if (!isset($char[1])) {
|
|
|
|
return '\\x'.substr('00'.bin2hex($char), -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// \uHHHH
|
|
|
|
$char = $that->convertEncoding($char, 'UTF-16BE', 'UTF-8');
|
|
|
|
|
|
|
|
return '\\u'.substr('0000'.bin2hex($char), -4);
|
|
|
|
};
|
|
|
|
|
2011-03-03 22:00:42 +00:00
|
|
|
if (null === $value = preg_replace_callback('#[^\p{L}\p{N} ]#u', $callback, $value)) {
|
2010-11-23 09:22:48 +00:00
|
|
|
throw new \InvalidArgumentException('The string to escape is not a valid UTF-8 string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('UTF-8' != $that->getCharset()) {
|
2011-03-03 22:00:42 +00:00
|
|
|
$value = $that->convertEncoding($value, $that->getCharset(), 'UTF-8');
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 22:00:42 +00:00
|
|
|
return $value;
|
2010-11-23 09:22:48 +00:00
|
|
|
},
|
|
|
|
);
|
2012-07-16 17:29:06 +01:00
|
|
|
|
2012-07-30 21:58:40 +01:00
|
|
|
self::$escaperCache = array();
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
|
|
|
|
2011-02-07 13:11:46 +00:00
|
|
|
/**
|
|
|
|
* Convert a string from one encoding to another.
|
|
|
|
*
|
|
|
|
* @param string $string The string to convert
|
|
|
|
* @param string $to The input encoding
|
|
|
|
* @param string $from The output encoding
|
|
|
|
*
|
|
|
|
* @return string The string with the new encoding
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException if no suitable encoding function is found (iconv or mbstring)
|
|
|
|
*/
|
2010-11-23 09:22:48 +00:00
|
|
|
public function convertEncoding($string, $to, $from)
|
|
|
|
{
|
2012-05-05 09:55:15 +01:00
|
|
|
if (function_exists('mb_convert_encoding')) {
|
2010-11-23 09:22:48 +00:00
|
|
|
return mb_convert_encoding($string, $to, $from);
|
2012-05-05 09:55:15 +01:00
|
|
|
} elseif (function_exists('iconv')) {
|
|
|
|
return iconv($from, $to, $string);
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
2011-02-27 17:29:20 +00:00
|
|
|
|
|
|
|
throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
|
2010-11-23 09:22:48 +00:00
|
|
|
}
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the loader associated with this engine.
|
|
|
|
*
|
|
|
|
* @return LoaderInterface A LoaderInterface instance
|
|
|
|
*/
|
|
|
|
public function getLoader()
|
|
|
|
{
|
|
|
|
return $this->loader;
|
|
|
|
}
|
2011-02-04 18:28:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the given template.
|
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
|
2011-02-04 18:28:14 +00:00
|
|
|
*
|
|
|
|
* @return Storage A Storage instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the template cannot be found
|
|
|
|
*/
|
|
|
|
protected function load($name)
|
|
|
|
{
|
|
|
|
$template = $this->parser->parse($name);
|
|
|
|
|
2011-06-07 09:07:43 +01:00
|
|
|
$key = $template->getLogicalName();
|
2011-02-04 18:28:14 +00:00
|
|
|
if (isset($this->cache[$key])) {
|
|
|
|
return $this->cache[$key];
|
|
|
|
}
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
$storage = $this->loader->load($template);
|
2011-02-04 18:28:14 +00:00
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
if (false === $storage) {
|
2011-04-08 18:50:57 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $template));
|
2011-02-04 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
return $this->cache[$key] = $storage;
|
2011-02-04 18:28:14 +00:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|