Skip to content

Commit

Permalink
New feature #15706: Support PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt authored and eddylackmann committed Jan 27, 2020
1 parent a835a41 commit 3a73006
Show file tree
Hide file tree
Showing 179 changed files with 13,193 additions and 897 deletions.
5 changes: 5 additions & 0 deletions application/third_party/Twig/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static function autoload($class)

if (is_file($file = dirname(__FILE__).'/../'.str_replace(array('_', "\0"), array('/', ''), $class).'.php')) {
require $file;
} else {
$file = str_replace('\\', '/', $file);
if (is_file($file)) {
require $file;
}
}
}
}
60 changes: 60 additions & 0 deletions application/third_party/Twig/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig\Cache;

/**
* Interface implemented by cache classes.
*
* It is highly recommended to always store templates on the filesystem to
* benefit from the PHP opcode cache. This interface is mostly useful if you
* need to implement a custom strategy for storing templates on the filesystem.
*
* @author Andrew Tch <andrew@noop.lv>
*/
interface CacheInterface
{
/**
* Generates a cache key for the given template class name.
*
* @param string $name The template name
* @param string $className The template class name
*
* @return string
*/
public function generateKey($name, $className);

/**
* Writes the compiled template to cache.
*
* @param string $key The cache key
* @param string $content The template representation as a PHP class
*/
public function write($key, $content);

/**
* Loads a template from the cache.
*
* @param string $key The cache key
*/
public function load($key);

/**
* Returns the modification timestamp of a key.
*
* @param string $key The cache key
*
* @return int
*/
public function getTimestamp($key);
}

class_alias('Twig\Cache\CacheInterface', 'Twig_CacheInterface');
93 changes: 93 additions & 0 deletions application/third_party/Twig/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig\Cache;

/**
* Implements a cache on the filesystem.
*
* @author Andrew Tch <andrew@noop.lv>
*/
class FilesystemCache implements CacheInterface
{
const FORCE_BYTECODE_INVALIDATION = 1;

private $directory;
private $options;

/**
* @param string $directory The root cache directory
* @param int $options A set of options
*/
public function __construct($directory, $options = 0)
{
$this->directory = rtrim($directory, '\/').'/';
$this->options = $options;
}

public function generateKey($name, $className)
{
$hash = hash('sha256', $className);

return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
}

public function load($key)
{
if (file_exists($key)) {
@include_once $key;
}
}

public function write($key, $content)
{
$dir = \dirname($key);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
clearstatcache(true, $dir);
if (!is_dir($dir)) {
throw new \RuntimeException(sprintf('Unable to create the cache directory (%s).', $dir));
}
}
} elseif (!is_writable($dir)) {
throw new \RuntimeException(sprintf('Unable to write in the cache directory (%s).', $dir));
}

$tmpFile = tempnam($dir, basename($key));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) {
@chmod($key, 0666 & ~umask());

if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) {
// Compile cached file into bytecode cache
if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
@opcache_invalidate($key, true);
} elseif (\function_exists('apc_compile_file')) {
apc_compile_file($key);
}
}

return;
}

throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $key));
}

public function getTimestamp($key)
{
if (!file_exists($key)) {
return 0;
}

return (int) @filemtime($key);
}
}

class_alias('Twig\Cache\FilesystemCache', 'Twig_Cache_Filesystem');
42 changes: 42 additions & 0 deletions application/third_party/Twig/Cache/NullCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig\Cache;

/**
* Implements a no-cache strategy.
*
* @final
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class NullCache implements CacheInterface
{
public function generateKey($name, $className)
{
return '';
}

public function write($key, $content)
{
}

public function load($key)
{
}

public function getTimestamp($key)
{
return 0;
}
}

class_alias('Twig\Cache\NullCache', 'Twig_Cache_Null');
57 changes: 32 additions & 25 deletions application/third_party/Twig/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig;

use Twig\Node\ModuleNode;

/**
* Compiles a node to PHP code.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Compiler implements Twig_CompilerInterface
class Compiler implements \Twig_CompilerInterface
{
protected $lastLine;
protected $source;
protected $indentation;
protected $env;
protected $debugInfo = array();
protected $debugInfo = [];
protected $sourceOffset;
protected $sourceLine;
protected $filename;
private $varNameSalt = 0;

public function __construct(Twig_Environment $env)
public function __construct(Environment $env)
{
$this->env = $env;
}
Expand All @@ -44,7 +49,7 @@ public function getFilename()
/**
* Returns the environment instance related to this compiler.
*
* @return Twig_Environment
* @return Environment
*/
public function getEnvironment()
{
Expand All @@ -64,22 +69,22 @@ public function getSource()
/**
* Compiles a node.
*
* @param Twig_NodeInterface $node The node to compile
* @param int $indentation The current indentation
* @param int $indentation The current indentation
*
* @return $this
*/
public function compile(Twig_NodeInterface $node, $indentation = 0)
public function compile(\Twig_NodeInterface $node, $indentation = 0)
{
$this->lastLine = null;
$this->source = '';
$this->debugInfo = array();
$this->debugInfo = [];
$this->sourceOffset = 0;
// source code starts at 1 (as we then increment it when we encounter new lines)
$this->sourceLine = 1;
$this->indentation = $indentation;
$this->varNameSalt = 0;

if ($node instanceof Twig_Node_Module) {
if ($node instanceof ModuleNode) {
// to be removed in 2.0
$this->filename = $node->getTemplateName();
}
Expand All @@ -89,7 +94,7 @@ public function compile(Twig_NodeInterface $node, $indentation = 0)
return $this;
}

public function subcompile(Twig_NodeInterface $node, $raw = true)
public function subcompile(\Twig_NodeInterface $node, $raw = true)
{
if (false === $raw) {
$this->source .= str_repeat(' ', $this->indentation * 4);
Expand Down Expand Up @@ -121,7 +126,7 @@ public function raw($string)
*/
public function write()
{
$strings = func_get_args();
$strings = \func_get_args();
foreach ($strings as $string) {
$this->source .= str_repeat(' ', $this->indentation * 4).$string;
}
Expand Down Expand Up @@ -168,22 +173,22 @@ public function string($value)
*/
public function repr($value)
{
if (is_int($value) || is_float($value)) {
if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
if (\is_int($value) || \is_float($value)) {
if (false !== $locale = setlocale(LC_NUMERIC, '0')) {
setlocale(LC_NUMERIC, 'C');
}

$this->raw($value);
$this->raw(var_export($value, true));

if (false !== $locale) {
setlocale(LC_NUMERIC, $locale);
}
} elseif (null === $value) {
$this->raw('null');
} elseif (is_bool($value)) {
} elseif (\is_bool($value)) {
$this->raw($value ? 'true' : 'false');
} elseif (is_array($value)) {
$this->raw('array(');
} elseif (\is_array($value)) {
$this->raw('[');
$first = true;
foreach ($value as $key => $v) {
if (!$first) {
Expand All @@ -194,7 +199,7 @@ public function repr($value)
$this->raw(' => ');
$this->repr($v);
}
$this->raw(')');
$this->raw(']');
} else {
$this->string($value);
}
Expand All @@ -207,7 +212,7 @@ public function repr($value)
*
* @return $this
*/
public function addDebugInfo(Twig_NodeInterface $node)
public function addDebugInfo(\Twig_NodeInterface $node)
{
if ($node->getTemplateLine() != $this->lastLine) {
$this->write(sprintf("// line %d\n", $node->getTemplateLine()));
Expand All @@ -223,7 +228,7 @@ public function addDebugInfo(Twig_NodeInterface $node)
} else {
$this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
}
$this->sourceOffset = strlen($this->source);
$this->sourceOffset = \strlen($this->source);
$this->debugInfo[$this->sourceLine] = $node->getTemplateLine();

$this->lastLine = $node->getTemplateLine();
Expand Down Expand Up @@ -260,13 +265,13 @@ public function indent($step = 1)
*
* @return $this
*
* @throws LogicException When trying to outdent too much so the indentation would become negative
* @throws \LogicException When trying to outdent too much so the indentation would become negative
*/
public function outdent($step = 1)
{
// can't outdent by more steps than the current indentation level
if ($this->indentation < $step) {
throw new LogicException('Unable to call outdent() as the indentation would become negative.');
throw new \LogicException('Unable to call outdent() as the indentation would become negative.');
}

$this->indentation -= $step;
Expand All @@ -276,6 +281,8 @@ public function outdent($step = 1)

public function getVarName()
{
return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->varNameSalt++));
}
}

class_alias('Twig\Compiler', 'Twig_Compiler');
Loading

0 comments on commit 3a73006

Please sign in to comment.