-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a835a41
commit 3a73006
Showing
179 changed files
with
13,193 additions
and
897 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.