mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
Improvements for loader speeds
This commit is contained in:
committed by
Fabien Potencier
parent
120cde3fa5
commit
08ecb0e1c4
@@ -112,6 +112,8 @@ class Twig_Error extends Exception
|
|||||||
* @param array $arguments The parameters to be passed to the method
|
* @param array $arguments The parameters to be passed to the method
|
||||||
*
|
*
|
||||||
* @return Exception The previous exception or null
|
* @return Exception The previous exception or null
|
||||||
|
*
|
||||||
|
* @throws BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public function __call($method, $arguments)
|
public function __call($method, $arguments)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Twig.
|
||||||
|
*
|
||||||
|
* (c) 2009 Fabien Potencier
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface all loaders must implement in order to provide extra functionality
|
||||||
|
* for the Twig core.
|
||||||
|
*
|
||||||
|
* @package twig
|
||||||
|
* @author Florin Patan <florinpatan@gmail.com>
|
||||||
|
*/
|
||||||
|
interface Twig_ExtendedLoaderInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if we have the source code of a template, given its name.
|
||||||
|
*
|
||||||
|
* @param string $name The name of the template to check if we can load
|
||||||
|
*
|
||||||
|
* @return boolean If the template source code is handled by this loader or not
|
||||||
|
*/
|
||||||
|
public function exists($name);
|
||||||
|
|
||||||
|
}
|
||||||
+12
-15
@@ -20,7 +20,7 @@
|
|||||||
* @package twig
|
* @package twig
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
*/
|
*/
|
||||||
class Twig_Loader_Array implements Twig_LoaderInterface
|
class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
|
||||||
{
|
{
|
||||||
protected $templates;
|
protected $templates;
|
||||||
|
|
||||||
@@ -51,11 +51,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the source code of a template, given its name.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The name of the template to load
|
|
||||||
*
|
|
||||||
* @return string The template source code
|
|
||||||
*/
|
*/
|
||||||
public function getSource($name)
|
public function getSource($name)
|
||||||
{
|
{
|
||||||
@@ -68,11 +64,15 @@ class Twig_Loader_Array implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the cache key to use for the cache for a given template name.
|
* {@inheritdoc}
|
||||||
*
|
*/
|
||||||
* @param string $name The name of the template to load
|
public function exists($name)
|
||||||
*
|
{
|
||||||
* @return string The cache key
|
return isset($this->templates[(string) $name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getCacheKey($name)
|
public function getCacheKey($name)
|
||||||
{
|
{
|
||||||
@@ -85,10 +85,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the template is still fresh.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The template name
|
|
||||||
* @param timestamp $time The last modification time of the cached template
|
|
||||||
*/
|
*/
|
||||||
public function isFresh($name, $time)
|
public function isFresh($name, $time)
|
||||||
{
|
{
|
||||||
|
|||||||
+45
-15
@@ -15,8 +15,9 @@
|
|||||||
* @package twig
|
* @package twig
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
*/
|
*/
|
||||||
class Twig_Loader_Chain implements Twig_LoaderInterface
|
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
|
||||||
{
|
{
|
||||||
|
private $hasSourceCache = array();
|
||||||
protected $loaders;
|
protected $loaders;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,19 +41,20 @@ class Twig_Loader_Chain implements Twig_LoaderInterface
|
|||||||
public function addLoader(Twig_LoaderInterface $loader)
|
public function addLoader(Twig_LoaderInterface $loader)
|
||||||
{
|
{
|
||||||
$this->loaders[] = $loader;
|
$this->loaders[] = $loader;
|
||||||
|
$this->hasSourceCache = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the source code of a template, given its name.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The name of the template to load
|
|
||||||
*
|
|
||||||
* @return string The template source code
|
|
||||||
*/
|
*/
|
||||||
public function getSource($name)
|
public function getSource($name)
|
||||||
{
|
{
|
||||||
$exceptions = array();
|
$exceptions = array();
|
||||||
foreach ($this->loaders as $loader) {
|
foreach ($this->loaders as $loader) {
|
||||||
|
if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $loader->getSource($name);
|
return $loader->getSource($name);
|
||||||
} catch (Twig_Error_Loader $e) {
|
} catch (Twig_Error_Loader $e) {
|
||||||
@@ -64,16 +66,43 @@ class Twig_Loader_Chain implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the cache key to use for the cache for a given template name.
|
* {@inheritdoc}
|
||||||
*
|
*/
|
||||||
* @param string $name The name of the template to load
|
public function exists($name)
|
||||||
*
|
{
|
||||||
* @return string The cache key
|
if (isset($this->hasSourceCache[$name])) {
|
||||||
|
return $this->hasSourceCache[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->loaders as $loader) {
|
||||||
|
if ($loader instanceof Twig_ExtendedLoaderInterface) {
|
||||||
|
if ($loader->exists($name)) {
|
||||||
|
return $this->hasSourceCache[$name] = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
$loader->getSource($name);
|
||||||
|
return $this->hasSourceCache[$name] = true;
|
||||||
|
} catch (Twig_Error_Loader $e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->hasSourceCache[$name] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getCacheKey($name)
|
public function getCacheKey($name)
|
||||||
{
|
{
|
||||||
$exceptions = array();
|
$exceptions = array();
|
||||||
foreach ($this->loaders as $loader) {
|
foreach ($this->loaders as $loader) {
|
||||||
|
if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $loader->getCacheKey($name);
|
return $loader->getCacheKey($name);
|
||||||
} catch (Twig_Error_Loader $e) {
|
} catch (Twig_Error_Loader $e) {
|
||||||
@@ -85,15 +114,16 @@ class Twig_Loader_Chain implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the template is still fresh.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The template name
|
|
||||||
* @param timestamp $time The last modification time of the cached template
|
|
||||||
*/
|
*/
|
||||||
public function isFresh($name, $time)
|
public function isFresh($name, $time)
|
||||||
{
|
{
|
||||||
$exceptions = array();
|
$exceptions = array();
|
||||||
foreach ($this->loaders as $loader) {
|
foreach ($this->loaders as $loader) {
|
||||||
|
if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $loader->isFresh($name, $time);
|
return $loader->isFresh($name, $time);
|
||||||
} catch (Twig_Error_Loader $e) {
|
} catch (Twig_Error_Loader $e) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
* @package twig
|
* @package twig
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
*/
|
*/
|
||||||
class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
|
||||||
{
|
{
|
||||||
protected $paths;
|
protected $paths;
|
||||||
protected $cache;
|
protected $cache;
|
||||||
@@ -77,6 +77,8 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
*
|
*
|
||||||
* @param string $path A path where to look for templates
|
* @param string $path A path where to look for templates
|
||||||
* @param string $namespace A path name
|
* @param string $namespace A path name
|
||||||
|
*
|
||||||
|
* @throws Twig_Error_Loader
|
||||||
*/
|
*/
|
||||||
public function addPath($path, $namespace = '__main__')
|
public function addPath($path, $namespace = '__main__')
|
||||||
{
|
{
|
||||||
@@ -95,6 +97,8 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
*
|
*
|
||||||
* @param string $path A path where to look for templates
|
* @param string $path A path where to look for templates
|
||||||
* @param string $namespace A path name
|
* @param string $namespace A path name
|
||||||
|
*
|
||||||
|
* @throws Twig_Error_Loader
|
||||||
*/
|
*/
|
||||||
public function prependPath($path, $namespace = '__main__')
|
public function prependPath($path, $namespace = '__main__')
|
||||||
{
|
{
|
||||||
@@ -115,11 +119,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the source code of a template, given its name.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The name of the template to load
|
|
||||||
*
|
|
||||||
* @return string The template source code
|
|
||||||
*/
|
*/
|
||||||
public function getSource($name)
|
public function getSource($name)
|
||||||
{
|
{
|
||||||
@@ -127,11 +127,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the cache key to use for the cache for a given template name.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The name of the template to load
|
|
||||||
*
|
|
||||||
* @return string The cache key
|
|
||||||
*/
|
*/
|
||||||
public function getCacheKey($name)
|
public function getCacheKey($name)
|
||||||
{
|
{
|
||||||
@@ -139,10 +135,25 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the template is still fresh.
|
* {@inheritdoc}
|
||||||
*
|
*/
|
||||||
* @param string $name The template name
|
public function exists($name)
|
||||||
* @param timestamp $time The last modification time of the cached template
|
{
|
||||||
|
if (isset($this->cache[$name])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->findTemplate($name);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (Twig_Error_Loader $exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function isFresh($name, $time)
|
public function isFresh($name, $time)
|
||||||
{
|
{
|
||||||
|
|||||||
+11
-10
@@ -24,20 +24,24 @@
|
|||||||
* @package twig
|
* @package twig
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
*/
|
*/
|
||||||
class Twig_Loader_String implements Twig_LoaderInterface
|
class Twig_Loader_String implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Gets the source code of a template, given its name.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The name of the template to load
|
|
||||||
*
|
|
||||||
* @return string The template source code
|
|
||||||
*/
|
*/
|
||||||
public function getSource($name)
|
public function getSource($name)
|
||||||
{
|
{
|
||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function exists($name)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the cache key to use for the cache for a given template name.
|
* Gets the cache key to use for the cache for a given template name.
|
||||||
*
|
*
|
||||||
@@ -51,10 +55,7 @@ class Twig_Loader_String implements Twig_LoaderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the template is still fresh.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @param string $name The template name
|
|
||||||
* @param timestamp $time The last modification time of the cached template
|
|
||||||
*/
|
*/
|
||||||
public function isFresh($name, $time)
|
public function isFresh($name, $time)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user