ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_Cache_Filesystem Class Reference

Implements a cache on the filesystem. More...

+ Inheritance diagram for Twig_Cache_Filesystem:
+ Collaboration diagram for Twig_Cache_Filesystem:

Public Member Functions

 __construct ($directory, $options=0)
 
 generateKey ($name, $className)
 Generates a cache key for the given template class name. More...
 
 load ($key)
 Loads a template from the cache. More...
 
 write ($key, $content)
 Writes the compiled template to cache. More...
 
 getTimestamp ($key)
 Returns the modification timestamp of a key. More...
 

Data Fields

const FORCE_BYTECODE_INVALIDATION = 1
 

Private Attributes

 $directory
 
 $options
 

Detailed Description

Implements a cache on the filesystem.

Author
Andrew Tch andre.nosp@m.w@no.nosp@m.op.lv

Definition at line 17 of file Filesystem.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Cache_Filesystem::__construct (   $directory,
  $options = 0 
)
Parameters
$directorystring The root cache directory
$optionsint A set of options

Definition at line 28 of file Filesystem.php.

References $directory, and $options.

29  {
30  $this->directory = rtrim($directory, '\/').'/';
31  $this->options = $options;
32  }

Member Function Documentation

◆ generateKey()

Twig_Cache_Filesystem::generateKey (   $name,
  $className 
)

Generates a cache key for the given template class name.

Parameters
string$nameThe template name
string$classNameThe template class name
Returns
string

Implements Twig_CacheInterface.

Definition at line 34 of file Filesystem.php.

References GuzzleHttp\Psr7\hash().

35  {
36  $hash = hash('sha256', $className);
37 
38  return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
39  }
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
+ Here is the call graph for this function:

◆ getTimestamp()

Twig_Cache_Filesystem::getTimestamp (   $key)

Returns the modification timestamp of a key.

Parameters
string$keyThe cache key
Returns
int

Implements Twig_CacheInterface.

Definition at line 83 of file Filesystem.php.

References $key.

84  {
85  if (!file_exists($key)) {
86  return 0;
87  }
88 
89  return (int) @filemtime($key);
90  }
$key
Definition: croninfo.php:18

◆ load()

Twig_Cache_Filesystem::load (   $key)

Loads a template from the cache.

Parameters
string$keyThe cache key

Implements Twig_CacheInterface.

Definition at line 41 of file Filesystem.php.

References $key.

42  {
43  if (file_exists($key)) {
44  @include_once $key;
45  }
46  }
$key
Definition: croninfo.php:18

◆ write()

Twig_Cache_Filesystem::write (   $key,
  $content 
)

Writes the compiled template to cache.

Parameters
string$keyThe cache key
string$contentThe template representation as a PHP class

Implements Twig_CacheInterface.

Definition at line 48 of file Filesystem.php.

References $key.

49  {
50  $dir = dirname($key);
51  if (!is_dir($dir)) {
52  if (false === @mkdir($dir, 0777, true)) {
53  if (PHP_VERSION_ID >= 50300) {
54  clearstatcache(true, $dir);
55  }
56  if (!is_dir($dir)) {
57  throw new RuntimeException(sprintf('Unable to create the cache directory (%s).', $dir));
58  }
59  }
60  } elseif (!is_writable($dir)) {
61  throw new RuntimeException(sprintf('Unable to write in the cache directory (%s).', $dir));
62  }
63 
64  $tmpFile = tempnam($dir, basename($key));
65  if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) {
66  @chmod($key, 0666 & ~umask());
67 
68  if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) {
69  // Compile cached file into bytecode cache
70  if (function_exists('opcache_invalidate')) {
71  opcache_invalidate($key, true);
72  } elseif (function_exists('apc_compile_file')) {
73  apc_compile_file($key);
74  }
75  }
76 
77  return;
78  }
79 
80  throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key));
81  }
$key
Definition: croninfo.php:18

Field Documentation

◆ $directory

Twig_Cache_Filesystem::$directory
private

Definition at line 21 of file Filesystem.php.

Referenced by __construct().

◆ $options

Twig_Cache_Filesystem::$options
private

Definition at line 22 of file Filesystem.php.

Referenced by __construct().

◆ FORCE_BYTECODE_INVALIDATION

const Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION = 1

Definition at line 19 of file Filesystem.php.


The documentation for this class was generated from the following file: