Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit 863e56c

Browse files
author
Florens Verschelde
committed
Update Twig to 1.26.0
1 parent c882004 commit 863e56c

File tree

189 files changed

+249
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+249
-90
lines changed

lib/Twig/CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* 1.26.0 (2016-10-02)
2+
3+
* added template cache invalidation based on more environment options
4+
* added a missing deprecation notice
5+
* fixed template paths when a template is stored in a PHAR file
6+
* allowed filters/functions/tests implementation to use a different class than the extension they belong to
7+
* deprecated Twig_ExtensionInterface::getName()
8+
19
* 1.25.0 (2016-09-21)
210

311
* changed the way we store template source in template classes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

lib/Twig/Twig/Compiler.php renamed to lib/Twig/lib/Twig/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function outdent($step = 1)
270270
{
271271
// can't outdent by more steps than the current indentation level
272272
if ($this->indentation < $step) {
273-
throw new LogicException('Unable to call outdent() as the indentation would become negative');
273+
throw new LogicException('Unable to call outdent() as the indentation would become negative.');
274274
}
275275

276276
$this->indentation -= $step;
File renamed without changes.

lib/Twig/Twig/Environment.php renamed to lib/Twig/lib/Twig/Environment.php

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class Twig_Environment
1818
{
19-
const VERSION = '1.25.0';
19+
const VERSION = '1.26.0';
2020

2121
protected $charset;
2222
protected $loader;
@@ -49,6 +49,10 @@ class Twig_Environment
4949
private $bcWriteCacheFile = false;
5050
private $bcGetCacheFilename = false;
5151
private $lastModifiedExtension = 0;
52+
private $legacyExtensionNames = array();
53+
private $runtimeLoaders = array();
54+
private $runtimes = array();
55+
private $optionsHash;
5256

5357
/**
5458
* Constructor.
@@ -155,6 +159,7 @@ public function getBaseTemplateClass()
155159
public function setBaseTemplateClass($class)
156160
{
157161
$this->baseTemplateClass = $class;
162+
$this->updateOptionsHash();
158163
}
159164

160165
/**
@@ -163,6 +168,7 @@ public function setBaseTemplateClass($class)
163168
public function enableDebug()
164169
{
165170
$this->debug = true;
171+
$this->updateOptionsHash();
166172
}
167173

168174
/**
@@ -171,6 +177,7 @@ public function enableDebug()
171177
public function disableDebug()
172178
{
173179
$this->debug = false;
180+
$this->updateOptionsHash();
174181
}
175182

176183
/**
@@ -215,6 +222,7 @@ public function isAutoReload()
215222
public function enableStrictVariables()
216223
{
217224
$this->strictVariables = true;
225+
$this->updateOptionsHash();
218226
}
219227

220228
/**
@@ -223,6 +231,7 @@ public function enableStrictVariables()
223231
public function disableStrictVariables()
224232
{
225233
$this->strictVariables = false;
234+
$this->updateOptionsHash();
226235
}
227236

228237
/**
@@ -300,7 +309,10 @@ public function getCacheFilename($name)
300309
*
301310
* * The cache key for the given template;
302311
* * The currently enabled extensions;
303-
* * Whether the Twig C extension is available or not.
312+
* * Whether the Twig C extension is available or not;
313+
* * PHP version;
314+
* * Twig version;
315+
* * Options with what environment was created.
304316
*
305317
* @param string $name The name for which to calculate the template class name
306318
* @param int|null $index The index if it is an embedded template
@@ -309,10 +321,7 @@ public function getCacheFilename($name)
309321
*/
310322
public function getTemplateClass($name, $index = null)
311323
{
312-
$key = $this->getLoader()->getCacheKey($name);
313-
$key .= json_encode(array_keys($this->extensions));
314-
$key .= function_exists('twig_template_get_attributes');
315-
$key .= ':'.PHP_MAJOR_VERSION.':'.PHP_MINOR_VERSION;
324+
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;
316325

317326
return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index);
318327
}
@@ -771,29 +780,73 @@ public function initRuntime()
771780
/**
772781
* Returns true if the given extension is registered.
773782
*
774-
* @param string $name The extension name
783+
* @param string $class The extension class name
775784
*
776785
* @return bool Whether the extension is registered or not
777786
*/
778-
public function hasExtension($name)
787+
public function hasExtension($class)
788+
{
789+
if (isset($this->legacyExtensionNames[$class])) {
790+
$class = $this->legacyExtensionNames[$class];
791+
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
792+
}
793+
794+
return isset($this->extensions[ltrim($class, '\\')]);
795+
}
796+
797+
/**
798+
* Adds a runtime loader.
799+
*/
800+
public function addRuntimeLoader(Twig_RuntimeLoaderInterface $loader)
779801
{
780-
return isset($this->extensions[$name]);
802+
$this->runtimeLoaders[] = $loader;
781803
}
782804

783805
/**
784-
* Gets an extension by name.
806+
* Gets an extension by class name.
785807
*
786-
* @param string $name The extension name
808+
* @param string $class The extension class name
787809
*
788810
* @return Twig_ExtensionInterface A Twig_ExtensionInterface instance
789811
*/
790-
public function getExtension($name)
812+
public function getExtension($class)
813+
{
814+
if (isset($this->legacyExtensionNames[$class])) {
815+
$class = $this->legacyExtensionNames[$class];
816+
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
817+
}
818+
819+
$class = ltrim($class, '\\');
820+
821+
if (!isset($this->extensions[$class])) {
822+
throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $class));
823+
}
824+
825+
return $this->extensions[$class];
826+
}
827+
828+
/**
829+
* Returns the runtime implementation of a Twig element (filter/function/test).
830+
*
831+
* @param string $class A runtime class name
832+
*
833+
* @return object The runtime implementation
834+
*
835+
* @throws Twig_Error_Runtime When the template cannot be found
836+
*/
837+
public function getRuntime($class)
791838
{
792-
if (!isset($this->extensions[$name])) {
793-
throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $name));
839+
if (isset($this->runtimes[$class])) {
840+
return $this->runtimes[$class];
841+
}
842+
843+
foreach ($this->runtimeLoaders as $loader) {
844+
if (null !== $runtime = $loader->load($class)) {
845+
return $this->runtimes[$class] = $runtime;
846+
}
794847
}
795848

796-
return $this->extensions[$name];
849+
throw new Twig_Error_Runtime(sprintf('Unable to load the "%s" runtime.', $class));
797850
}
798851

799852
/**
@@ -803,19 +856,26 @@ public function getExtension($name)
803856
*/
804857
public function addExtension(Twig_ExtensionInterface $extension)
805858
{
806-
$name = $extension->getName();
859+
$class = get_class($extension);
807860

808861
if ($this->extensionInitialized) {
809-
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $name));
862+
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $class));
810863
}
811864

812-
if (isset($this->extensions[$name])) {
813-
@trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $name), E_USER_DEPRECATED);
865+
$m = new ReflectionMethod($extension, 'getName');
866+
$legacyName = 'Twig_Extension' !== $m->getDeclaringClass()->getName() ? $extension->getName() : null;
867+
868+
if (isset($this->extensions[$class]) || (null !== $legacyName && isset($this->legacyExtensionNames[$legacyName]))) {
869+
unset($this->extensions[$this->legacyExtensionNames[$legacyName]], $this->legacyExtensionNames[$legacyName]);
870+
@trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $class), E_USER_DEPRECATED);
814871
}
815872

816873
$this->lastModifiedExtension = 0;
817-
818-
$this->extensions[$name] = $extension;
874+
if ($legacyName !== $class) {
875+
$this->legacyExtensionNames[$legacyName] = $class;
876+
}
877+
$this->extensions[$class] = $extension;
878+
$this->updateOptionsHash();
819879
}
820880

821881
/**
@@ -831,11 +891,17 @@ public function removeExtension($name)
831891
{
832892
@trigger_error(sprintf('The %s method is deprecated since version 1.12 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
833893

894+
if (isset($this->legacyExtensionNames[$name])) {
895+
$name = $this->legacyExtensionNames[$name];
896+
@trigger_error(sprintf('Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $name), E_USER_DEPRECATED);
897+
}
898+
834899
if ($this->extensionInitialized) {
835900
throw new LogicException(sprintf('Unable to remove extension "%s" as extensions have already been initialized.', $name));
836901
}
837902

838-
unset($this->extensions[$name]);
903+
unset($this->extensions[ltrim($name, '\\')]);
904+
$this->updateOptionsHash();
839905
}
840906

841907
/**
@@ -853,7 +919,7 @@ public function setExtensions(array $extensions)
853919
/**
854920
* Returns all registered extensions.
855921
*
856-
* @return array An array of extensions
922+
* @return Twig_ExtensionInterface[] An array of extensions (keys are for internal usage only and should not be relied on)
857923
*/
858924
public function getExtensions()
859925
{
@@ -950,7 +1016,7 @@ public function getNodeVisitors()
9501016
public function addFilter($name, $filter = null)
9511017
{
9521018
if (!$name instanceof Twig_SimpleFilter && !($filter instanceof Twig_SimpleFilter || $filter instanceof Twig_FilterInterface)) {
953-
throw new LogicException('A filter must be an instance of Twig_FilterInterface or Twig_SimpleFilter');
1019+
throw new LogicException('A filter must be an instance of Twig_FilterInterface or Twig_SimpleFilter.');
9541020
}
9551021

9561022
if ($name instanceof Twig_SimpleFilter) {
@@ -1045,7 +1111,7 @@ public function getFilters()
10451111
public function addTest($name, $test = null)
10461112
{
10471113
if (!$name instanceof Twig_SimpleTest && !($test instanceof Twig_SimpleTest || $test instanceof Twig_TestInterface)) {
1048-
throw new LogicException('A test must be an instance of Twig_TestInterface or Twig_SimpleTest');
1114+
throw new LogicException('A test must be an instance of Twig_TestInterface or Twig_SimpleTest.');
10491115
}
10501116

10511117
if ($name instanceof Twig_SimpleTest) {
@@ -1109,7 +1175,7 @@ public function getTest($name)
11091175
public function addFunction($name, $function = null)
11101176
{
11111177
if (!$name instanceof Twig_SimpleFunction && !($function instanceof Twig_SimpleFunction || $function instanceof Twig_FunctionInterface)) {
1112-
throw new LogicException('A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction');
1178+
throw new LogicException('A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction.');
11131179
}
11141180

11151181
if ($name instanceof Twig_SimpleFunction) {
@@ -1407,7 +1473,7 @@ protected function initExtension(Twig_ExtensionInterface $extension)
14071473

14081474
$this->parsers->addTokenParserBroker($parser);
14091475
} else {
1410-
throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
1476+
throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances.');
14111477
}
14121478
}
14131479

@@ -1434,4 +1500,21 @@ protected function writeCacheFile($file, $content)
14341500
{
14351501
$this->cache->write($file, $content);
14361502
}
1503+
1504+
private function updateOptionsHash()
1505+
{
1506+
$hashParts = array_merge(
1507+
array_keys($this->extensions),
1508+
array(
1509+
(int) function_exists('twig_template_get_attributes'),
1510+
PHP_MAJOR_VERSION,
1511+
PHP_MINOR_VERSION,
1512+
self::VERSION,
1513+
(int) $this->debug,
1514+
$this->baseTemplateClass,
1515+
(int) $this->strictVariables,
1516+
)
1517+
);
1518+
$this->optionsHash = implode(':', $hashParts);
1519+
}
14371520
}
File renamed without changes.

0 commit comments

Comments
 (0)