feature #1822 changed the arguments of generateKey (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

changed the arguments of generateKey

We now pass the template name and the template class, which seems much better from a UX standpoint.

Passing the prefix was really just a hack and an implementation leak to be able to determine the variable part of the class name and generate sub-directories for templates. Now, we generate the cache key by taking the last characters instead of the first ones, to avoid the need for the class prefix.

That should also make Drupal happy :)

Commits
-------

c2e75ff changed the arguments of generateKey
0157315 deprecated Twig_Environment::getTemplateClassPrefix
This commit is contained in:
Fabien Potencier
2015-09-12 19:42:00 +02:00
7 changed files with 16 additions and 12 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
* changed template cache names to take into account enabled extensions
* deprecated Twig_Environment::clearCacheFiles(), Twig_Environment::getCacheFilename(),
and Twig_Environment::writeCacheFile()
Twig_Environment::writeCacheFile(), and Twig_Environment::getTemplateClassPrefix()
* added a way to override the filesystem template cache system
* added a way to get the original template source from Twig_Template
+2 -2
View File
@@ -141,8 +141,8 @@ Miscellaneous
-------------
* As of Twig 1.x, ``Twig_Environment::clearTemplateCache()``, ``Twig_Environment::writeCacheFile()``,
``Twig_Environment::clearCacheFiles()``, and ``Twig_Environment::getCacheFilename()`` are deprecated and
will be removed in 2.0.
``Twig_Environment::clearCacheFiles()``, ``Twig_Environment::getCacheFilename()``, and
``Twig_Environment::getTemplateClassPrefix()`` are deprecated and will be removed in 2.0.
* As of Twig 1.x, ``Twig_Template::getEnvironment()`` and
``Twig_TemplateInterface::getEnvironment()`` are deprecated and will be
+3 -3
View File
@@ -29,11 +29,11 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
/**
* {@inheritdoc}
*/
public function generateKey($className, $prefix)
public function generateKey($name, $className)
{
$class = substr($className, strlen($prefix));
$hash = hash('sha256', $className);
return $this->directory.'/'.$class[0].'/'.$class[1].'/'.$class.'.php';
return $this->directory.'/'.$hash[0].'/'.$hash[1].'/'.$hash.'.php';
}
/**
+1 -1
View File
@@ -19,7 +19,7 @@ class Twig_Cache_Null implements Twig_CacheInterface
/**
* {@inheritdoc}
*/
public function generateKey($className, $prefix)
public function generateKey($name, $className)
{
return '';
}
+2 -2
View File
@@ -23,12 +23,12 @@ interface Twig_CacheInterface
/**
* Generates a cache key for the given template class name.
*
* @param string $name The template name
* @param string $className The template class name
* @param string $prefix A template class prefix
*
* @return string
*/
public function generateKey($className, $prefix);
public function generateKey($name, $className);
/**
* Checks if the cache key exists.
+6 -2
View File
@@ -283,7 +283,7 @@ class Twig_Environment
{
@trigger_error(sprintf('The %s method is deprecated and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
$key = $this->cache->generateKey($this->getTemplateClass($name), $this->templateClassPrefix);
$key = $this->cache->generateKey($name, $this->getTemplateClass($name));
return !$key ? false : $key;
}
@@ -307,9 +307,13 @@ class Twig_Environment
* Gets the template class prefix.
*
* @return string The template class prefix
*
* @deprecated since 1.22 (to be removed in 2.0)
*/
public function getTemplateClassPrefix()
{
@trigger_error(sprintf('The %s method is deprecated and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
return $this->templateClassPrefix;
}
@@ -368,7 +372,7 @@ class Twig_Environment
if ($this->bcGetCacheFilename) {
$key = $this->getCacheFilename($name);
} else {
$key = $this->cache->generateKey($cls, $this->templateClassPrefix);
$key = $this->cache->generateKey($name, $cls);
}
if (!$this->cache->has($key) || ($this->isAutoReload() && !$this->isTemplateFresh($name, $this->cache->getTimestamp($key)))) {
+1 -1
View File
@@ -156,7 +156,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
// force compilation
$twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
$key = $cache->generateKey($twig->getTemplateClass('index'), $twig->getTemplateClassPrefix());
$key = $cache->generateKey('index', $twig->getTemplateClass('index'));
$cache->write($key, $twig->compileSource('{{ foo }}', 'index'));
// check that extensions won't be initialized when rendering a template that is already in the cache