summaryrefslogtreecommitdiff
path: root/inc/lib/Twig/Loader
diff options
context:
space:
mode:
authorBenjamin Southall <applemansigloo@gmail.com>2019-02-26 10:11:12 +1000
committerBenjamin Southall <applemansigloo@gmail.com>2019-02-26 10:11:12 +1000
commitf79395851b044689567e0e6c9504acbcaa742782 (patch)
treeee539aee9ffa90e8accd6f130b30a8ebed7a232e /inc/lib/Twig/Loader
parent28f32baf0125ca5085266e73dc5071e5814d8e3b (diff)
Twig update to latest Twig 1.x legacy as per vichan
Diffstat (limited to 'inc/lib/Twig/Loader')
-rw-r--r--inc/lib/Twig/Loader/Array.php42
-rw-r--r--inc/lib/Twig/Loader/Chain.php65
-rw-r--r--inc/lib/Twig/Loader/Filesystem.php158
-rw-r--r--inc/lib/Twig/Loader/String.php33
4 files changed, 188 insertions, 110 deletions
diff --git a/inc/lib/Twig/Loader/Array.php b/inc/lib/Twig/Loader/Array.php
index ac561048..0aac7690 100644
--- a/inc/lib/Twig/Loader/Array.php
+++ b/inc/lib/Twig/Loader/Array.php
@@ -3,7 +3,7 @@
/*
* This file is part of Twig.
*
- * (c) 2009 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@@ -17,20 +17,20 @@
* source code of the template). If you don't want to see your cache grows out of
* control, you need to take care of clearing the old cache file by yourself.
*
+ * This loader should only be used for unit testing.
+ *
+ * @final
+ *
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
+class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
{
protected $templates = array();
/**
- * Constructor.
- *
* @param array $templates An array of templates (keys are the names, and values are the source code)
- *
- * @see Twig_Loader
*/
- public function __construct(array $templates)
+ public function __construct(array $templates = array())
{
$this->templates = $templates;
}
@@ -46,11 +46,10 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
$this->templates[(string) $name] = $template;
}
- /**
- * {@inheritdoc}
- */
public function getSource($name)
{
+ @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
+
$name = (string) $name;
if (!isset($this->templates[$name])) {
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
@@ -59,17 +58,21 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
return $this->templates[$name];
}
- /**
- * {@inheritdoc}
- */
+ public function getSourceContext($name)
+ {
+ $name = (string) $name;
+ if (!isset($this->templates[$name])) {
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
+ }
+
+ return new Twig_Source($this->templates[$name], $name);
+ }
+
public function exists($name)
{
return isset($this->templates[(string) $name]);
}
- /**
- * {@inheritdoc}
- */
public function getCacheKey($name)
{
$name = (string) $name;
@@ -77,12 +80,9 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}
- return $this->templates[$name];
+ return $name.':'.$this->templates[$name];
}
- /**
- * {@inheritdoc}
- */
public function isFresh($name, $time)
{
$name = (string) $name;
@@ -93,3 +93,5 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
return true;
}
}
+
+class_alias('Twig_Loader_Array', 'Twig\Loader\ArrayLoader', false);
diff --git a/inc/lib/Twig/Loader/Chain.php b/inc/lib/Twig/Loader/Chain.php
index 7919eda6..59a33796 100644
--- a/inc/lib/Twig/Loader/Chain.php
+++ b/inc/lib/Twig/Loader/Chain.php
@@ -3,7 +3,7 @@
/*
* This file is part of Twig.
*
- * (c) 2011 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@@ -12,17 +12,17 @@
/**
* Loads templates from other loaders.
*
+ * @final
+ *
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
+class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
{
private $hasSourceCache = array();
protected $loaders = array();
/**
- * Constructor.
- *
- * @param Twig_LoaderInterface[] $loaders An array of loader instances
+ * @param Twig_LoaderInterface[] $loaders
*/
public function __construct(array $loaders = array())
{
@@ -31,22 +31,16 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
}
}
- /**
- * Adds a loader instance.
- *
- * @param Twig_LoaderInterface $loader A Loader instance
- */
public function addLoader(Twig_LoaderInterface $loader)
{
$this->loaders[] = $loader;
$this->hasSourceCache = array();
}
- /**
- * {@inheritdoc}
- */
public function getSource($name)
{
+ @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
+
$exceptions = array();
foreach ($this->loaders as $loader) {
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
@@ -60,12 +54,31 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
}
}
- throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
+ }
+
+ public function getSourceContext($name)
+ {
+ $exceptions = array();
+ foreach ($this->loaders as $loader) {
+ if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
+ continue;
+ }
+
+ try {
+ if ($loader instanceof Twig_SourceContextLoaderInterface) {
+ return $loader->getSourceContext($name);
+ }
+
+ return new Twig_Source($loader->getSource($name), $name);
+ } catch (Twig_Error_Loader $e) {
+ $exceptions[] = $e->getMessage();
+ }
+ }
+
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
- /**
- * {@inheritdoc}
- */
public function exists($name)
{
$name = (string) $name;
@@ -84,7 +97,11 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
}
try {
- $loader->getSource($name);
+ if ($loader instanceof Twig_SourceContextLoaderInterface) {
+ $loader->getSourceContext($name);
+ } else {
+ $loader->getSource($name);
+ }
return $this->hasSourceCache[$name] = true;
} catch (Twig_Error_Loader $e) {
@@ -94,9 +111,6 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
return $this->hasSourceCache[$name] = false;
}
- /**
- * {@inheritdoc}
- */
public function getCacheKey($name)
{
$exceptions = array();
@@ -112,12 +126,9 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
}
}
- throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
- /**
- * {@inheritdoc}
- */
public function isFresh($name, $time)
{
$exceptions = array();
@@ -133,6 +144,8 @@ class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
}
}
- throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
}
+
+class_alias('Twig_Loader_Chain', 'Twig\Loader\ChainLoader', false);
diff --git a/inc/lib/Twig/Loader/Filesystem.php b/inc/lib/Twig/Loader/Filesystem.php
index 23bac47d..4e8be0d5 100644
--- a/inc/lib/Twig/Loader/Filesystem.php
+++ b/inc/lib/Twig/Loader/Filesystem.php
@@ -3,7 +3,7 @@
/*
* This file is part of Twig.
*
- * (c) 2009 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@@ -14,21 +14,28 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
+class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
{
/** Identifier of the main namespace. */
const MAIN_NAMESPACE = '__main__';
protected $paths = array();
protected $cache = array();
+ protected $errorCache = array();
+
+ private $rootPath;
/**
- * Constructor.
- *
- * @param string|array $paths A path or an array of paths where to look for templates
+ * @param string|array $paths A path or an array of paths where to look for templates
+ * @param string|null $rootPath The root path common to all relative paths (null for getcwd())
*/
- public function __construct($paths = array())
+ public function __construct($paths = array(), $rootPath = null)
{
+ $this->rootPath = (null === $rootPath ? getcwd() : $rootPath).DIRECTORY_SEPARATOR;
+ if (false !== $realPath = realpath($rootPath)) {
+ $this->rootPath = $realPath.DIRECTORY_SEPARATOR;
+ }
+
if ($paths) {
$this->setPaths($paths);
}
@@ -80,17 +87,18 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
* Adds a path where templates are stored.
*
* @param string $path A path where to look for templates
- * @param string $namespace A path name
+ * @param string $namespace A path namespace
*
* @throws Twig_Error_Loader
*/
public function addPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
- $this->cache = array();
+ $this->cache = $this->errorCache = array();
- if (!is_dir($path)) {
- throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
+ $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
+ if (!is_dir($checkPath)) {
+ throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
}
$this->paths[$namespace][] = rtrim($path, '/\\');
@@ -100,17 +108,18 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
* Prepends a path where templates are stored.
*
* @param string $path A path where to look for templates
- * @param string $namespace A path name
+ * @param string $namespace A path namespace
*
* @throws Twig_Error_Loader
*/
public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
- $this->cache = array();
+ $this->cache = $this->errorCache = array();
- if (!is_dir($path)) {
- throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
+ $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
+ if (!is_dir($checkPath)) {
+ throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
}
$path = rtrim($path, '/\\');
@@ -122,84 +131,126 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
}
}
- /**
- * {@inheritdoc}
- */
public function getSource($name)
{
+ @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
+
return file_get_contents($this->findTemplate($name));
}
- /**
- * {@inheritdoc}
- */
+ public function getSourceContext($name)
+ {
+ $path = $this->findTemplate($name);
+
+ return new Twig_Source(file_get_contents($path), $name, $path);
+ }
+
public function getCacheKey($name)
{
- return $this->findTemplate($name);
+ $path = $this->findTemplate($name);
+ $len = strlen($this->rootPath);
+ if (0 === strncmp($this->rootPath, $path, $len)) {
+ return substr($path, $len);
+ }
+
+ return $path;
}
- /**
- * {@inheritdoc}
- */
public function exists($name)
{
- $name = (string) $name;
+ $name = $this->normalizeName($name);
+
if (isset($this->cache[$name])) {
return true;
}
try {
- $this->findTemplate($name);
-
- return true;
+ return false !== $this->findTemplate($name, false);
} catch (Twig_Error_Loader $exception) {
+ @trigger_error(sprintf('In %s::findTemplate(), you must accept a second argument that when set to "false" returns "false" instead of throwing an exception. Not supporting this argument is deprecated since version 1.27.', get_class($this)), E_USER_DEPRECATED);
+
return false;
}
}
- /**
- * {@inheritdoc}
- */
public function isFresh($name, $time)
{
- return filemtime($this->findTemplate($name)) <= $time;
+ return filemtime($this->findTemplate($name)) < $time;
}
protected function findTemplate($name)
{
- $name = (string) $name;
-
- // normalize name
- $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
+ $throw = func_num_args() > 1 ? func_get_arg(1) : true;
+ $name = $this->normalizeName($name);
if (isset($this->cache[$name])) {
return $this->cache[$name];
}
- $this->validateName($name);
-
- $namespace = self::MAIN_NAMESPACE;
- $shortname = $name;
- if (isset($name[0]) && '@' == $name[0]) {
- if (false === $pos = strpos($name, '/')) {
- throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
+ if (isset($this->errorCache[$name])) {
+ if (!$throw) {
+ return false;
}
- $namespace = substr($name, 1, $pos - 1);
- $shortname = substr($name, $pos + 1);
+ throw new Twig_Error_Loader($this->errorCache[$name]);
}
+ $this->validateName($name);
+
+ list($namespace, $shortname) = $this->parseName($name);
+
if (!isset($this->paths[$namespace])) {
- throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
+ $this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);
+
+ if (!$throw) {
+ return false;
+ }
+
+ throw new Twig_Error_Loader($this->errorCache[$name]);
}
foreach ($this->paths[$namespace] as $path) {
+ if (!$this->isAbsolutePath($path)) {
+ $path = $this->rootPath.'/'.$path;
+ }
+
if (is_file($path.'/'.$shortname)) {
+ if (false !== $realpath = realpath($path.'/'.$shortname)) {
+ return $this->cache[$name] = $realpath;
+ }
+
return $this->cache[$name] = $path.'/'.$shortname;
}
}
- throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
+ $this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]));
+
+ if (!$throw) {
+ return false;
+ }
+
+ throw new Twig_Error_Loader($this->errorCache[$name]);
+ }
+
+ protected function parseName($name, $default = self::MAIN_NAMESPACE)
+ {
+ if (isset($name[0]) && '@' == $name[0]) {
+ if (false === $pos = strpos($name, '/')) {
+ throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
+ }
+
+ $namespace = substr($name, 1, $pos - 1);
+ $shortname = substr($name, $pos + 1);
+
+ return array($namespace, $shortname);
+ }
+
+ return array($default, $name);
+ }
+
+ protected function normalizeName($name)
+ {
+ return preg_replace('#/{2,}#', '/', str_replace('\\', '/', (string) $name));
}
protected function validateName($name)
@@ -223,4 +274,17 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
}
}
}
+
+ private function isAbsolutePath($file)
+ {
+ return strspn($file, '/\\', 0, 1)
+ || (strlen($file) > 3 && ctype_alpha($file[0])
+ && ':' === substr($file, 1, 1)
+ && strspn($file, '/\\', 2, 1)
+ )
+ || null !== parse_url($file, PHP_URL_SCHEME)
+ ;
+ }
}
+
+class_alias('Twig_Loader_Filesystem', 'Twig\Loader\FilesystemLoader', false);
diff --git a/inc/lib/Twig/Loader/String.php b/inc/lib/Twig/Loader/String.php
index 8ad9856c..950bd35b 100644
--- a/inc/lib/Twig/Loader/String.php
+++ b/inc/lib/Twig/Loader/String.php
@@ -3,55 +3,54 @@
/*
* This file is part of Twig.
*
- * (c) 2009 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+@trigger_error('The Twig_Loader_String class is deprecated since version 1.18.1 and will be removed in 2.0. Use Twig_Loader_Array instead or Twig_Environment::createTemplate().', E_USER_DEPRECATED);
+
/**
* Loads a template from a string.
*
- * This loader should only be used for unit testing as it has many limitations
- * (for instance, the include or extends tag does not make any sense for a string
- * loader).
+ * This loader should NEVER be used. It only exists for Twig internal purposes.
*
* When using this loader with a cache mechanism, you should know that a new cache
* key is generated each time a template content "changes" (the cache key being the
* source code of the template). If you don't want to see your cache grows out of
* control, you need to take care of clearing the old cache file by yourself.
*
+ * @deprecated since 1.18.1 (to be removed in 2.0)
+ *
+ * @internal
+ *
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_String implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
+class Twig_Loader_String implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
{
- /**
- * {@inheritdoc}
- */
public function getSource($name)
{
+ @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
+
return $name;
}
- /**
- * {@inheritdoc}
- */
+ public function getSourceContext($name)
+ {
+ return new Twig_Source($name, $name);
+ }
+
public function exists($name)
{
return true;
}
- /**
- * {@inheritdoc}
- */
public function getCacheKey($name)
{
return $name;
}
- /**
- * {@inheritdoc}
- */
public function isFresh($name, $time)
{
return true;