ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_Loader_Filesystem Class Reference

Loads template from the filesystem. More...

+ Inheritance diagram for Twig_Loader_Filesystem:
+ Collaboration diagram for Twig_Loader_Filesystem:

Public Member Functions

 __construct ($paths=array(), $rootPath=null)
 
 getPaths ($namespace=self::MAIN_NAMESPACE)
 Returns the paths to the templates. More...
 
 getNamespaces ()
 Returns the path namespaces. More...
 
 setPaths ($paths, $namespace=self::MAIN_NAMESPACE)
 Sets the paths where templates are stored. More...
 
 addPath ($path, $namespace=self::MAIN_NAMESPACE)
 Adds a path where templates are stored. More...
 
 prependPath ($path, $namespace=self::MAIN_NAMESPACE)
 Prepends a path where templates are stored. More...
 
 getSource ($name)
 Gets the source code of a template, given its name. More...
 
 getSourceContext ($name)
 Returns the source context for a given template logical name. More...
 
 getCacheKey ($name)
 Gets the cache key to use for the cache for a given template name. More...
 
 exists ($name)
 Check if we have the source code of a template, given its name. More...
 
 isFresh ($name, $time)
 Returns true if the template is still fresh. More...
 
 getSource ($name)
 Gets the source code of a template, given its name. More...
 
 getCacheKey ($name)
 Gets the cache key to use for the cache for a given template name. More...
 
 isFresh ($name, $time)
 Returns true if the template is still fresh. More...
 
 exists ($name)
 Check if we have the source code of a template, given its name. More...
 
 getSourceContext ($name)
 Returns the source context for a given template logical name. More...
 

Data Fields

const MAIN_NAMESPACE = '__main__'
 Identifier of the main namespace. More...
 

Protected Member Functions

 findTemplate ($name)
 
 parseName ($name, $default=self::MAIN_NAMESPACE)
 
 normalizeName ($name)
 
 validateName ($name)
 

Protected Attributes

 $paths = array()
 
 $cache = array()
 
 $errorCache = array()
 

Private Member Functions

 isAbsolutePath ($file)
 

Private Attributes

 $rootPath
 

Detailed Description

Loads template from the filesystem.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 17 of file Filesystem.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Loader_Filesystem::__construct (   $paths = array(),
  $rootPath = null 
)
Parameters
string | array$pathsA path or an array of paths where to look for templates
string | null$rootPathThe root path common to all relative paths (null for getcwd())

Definition at line 32 of file Filesystem.php.

33 {
34 $this->rootPath = (null === $rootPath ? getcwd() : $rootPath).DIRECTORY_SEPARATOR;
35 if (false !== $realPath = realpath($rootPath)) {
36 $this->rootPath = $realPath.DIRECTORY_SEPARATOR;
37 }
38
39 if ($paths) {
40 $this->setPaths($paths);
41 }
42 }
setPaths($paths, $namespace=self::MAIN_NAMESPACE)
Sets the paths where templates are stored.
Definition: Filesystem.php:74

References $paths, $rootPath, and setPaths().

+ Here is the call graph for this function:

Member Function Documentation

◆ addPath()

Twig_Loader_Filesystem::addPath (   $path,
  $namespace = self::MAIN_NAMESPACE 
)

Adds a path where templates are stored.

Parameters
string$pathA path where to look for templates
string$namespaceA path namespace
Exceptions
Twig_Error_Loader

Definition at line 94 of file Filesystem.php.

95 {
96 // invalidate the cache
97 $this->cache = $this->errorCache = array();
98
99 $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
100 if (!is_dir($checkPath)) {
101 throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
102 }
103
104 $this->paths[$namespace][] = rtrim($path, '/\\');
105 }
sprintf('%.4f', $callTime)
Exception thrown when an error occurs during template loading.
Definition: Loader.php:26
if($err=$client->getError()) $namespace

References $namespace, $path, isAbsolutePath(), and sprintf.

Referenced by setPaths().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exists()

Twig_Loader_Filesystem::exists (   $name)

Check if we have the source code of a template, given its name.

Parameters
string$nameThe name of the template to check if we can load
Returns
bool If the template source code is handled by this loader or not

Implements Twig_ExistsLoaderInterface.

Definition at line 159 of file Filesystem.php.

160 {
161 $name = $this->normalizeName($name);
162
163 if (isset($this->cache[$name])) {
164 return true;
165 }
166
167 try {
168 return false !== $this->findTemplate($name, false);
169 } catch (Twig_Error_Loader $exception) {
170 @trigger_error(sprintf('In %s::findTemplate(), you must accept a second argument that when set to "false" returns "false" instead of throwing an exception. Not supporting this argument is deprecated since version 1.27.', get_class($this)), E_USER_DEPRECATED);
171
172 return false;
173 }
174 }
if($format !==null) $name
Definition: metadata.php:146

References $name, findTemplate(), normalizeName(), and sprintf.

+ Here is the call graph for this function:

◆ findTemplate()

Twig_Loader_Filesystem::findTemplate (   $name)
protected

Definition at line 181 of file Filesystem.php.

182 {
183 $throw = func_num_args() > 1 ? func_get_arg(1) : true;
184 $name = $this->normalizeName($name);
185
186 if (isset($this->cache[$name])) {
187 return $this->cache[$name];
188 }
189
190 if (isset($this->errorCache[$name])) {
191 if (!$throw) {
192 return false;
193 }
194
195 throw new Twig_Error_Loader($this->errorCache[$name]);
196 }
197
198 $this->validateName($name);
199
200 list($namespace, $shortname) = $this->parseName($name);
201
202 if (!isset($this->paths[$namespace])) {
203 $this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);
204
205 if (!$throw) {
206 return false;
207 }
208
209 throw new Twig_Error_Loader($this->errorCache[$name]);
210 }
211
212 foreach ($this->paths[$namespace] as $path) {
213 if (!$this->isAbsolutePath($path)) {
214 $path = $this->rootPath.'/'.$path;
215 }
216
217 if (is_file($path.'/'.$shortname)) {
218 if (false !== $realpath = realpath($path.'/'.$shortname)) {
219 return $this->cache[$name] = $realpath;
220 }
221
222 return $this->cache[$name] = $path.'/'.$shortname;
223 }
224 }
225
226 $this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]));
227
228 if (!$throw) {
229 return false;
230 }
231
232 throw new Twig_Error_Loader($this->errorCache[$name]);
233 }
parseName($name, $default=self::MAIN_NAMESPACE)
Definition: Filesystem.php:235

References $name, $namespace, $path, isAbsolutePath(), normalizeName(), parseName(), sprintf, and validateName().

Referenced by exists(), getCacheKey(), getSource(), getSourceContext(), and isFresh().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCacheKey()

Twig_Loader_Filesystem::getCacheKey (   $name)

Gets the cache key to use for the cache for a given template name.

Parameters
string$nameThe name of the template to load
Returns
string The cache key
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_LoaderInterface.

Definition at line 148 of file Filesystem.php.

149 {
150 $path = $this->findTemplate($name);
151 $len = strlen($this->rootPath);
152 if (0 === strncmp($this->rootPath, $path, $len)) {
153 return substr($path, $len);
154 }
155
156 return $path;
157 }

References $name, $path, and findTemplate().

+ Here is the call graph for this function:

◆ getNamespaces()

Twig_Loader_Filesystem::getNamespaces ( )

Returns the path namespaces.

The main namespace is always defined.

Returns
array The array of defined namespaces

Definition at line 63 of file Filesystem.php.

64 {
65 return array_keys($this->paths);
66 }

◆ getPaths()

Twig_Loader_Filesystem::getPaths (   $namespace = self::MAIN_NAMESPACE)

Returns the paths to the templates.

Parameters
string$namespaceA path namespace
Returns
array The array of paths where to look for templates

Definition at line 51 of file Filesystem.php.

52 {
53 return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
54 }

References $namespace.

◆ getSource()

Twig_Loader_Filesystem::getSource (   $name)

Gets the source code of a template, given its name.

Parameters
string$nameThe name of the template to load
Returns
string The template source code
Exceptions
Twig_Error_LoaderWhen $name is not found
Deprecated:
since 1.27 (to be removed in 2.0), implement Twig_SourceContextLoaderInterface

Implements Twig_LoaderInterface.

Definition at line 134 of file Filesystem.php.

135 {
136 @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
137
138 return file_get_contents($this->findTemplate($name));
139 }

References $name, findTemplate(), and sprintf.

+ Here is the call graph for this function:

◆ getSourceContext()

Twig_Loader_Filesystem::getSourceContext (   $name)

Returns the source context for a given template logical name.

Parameters
string$nameThe template logical name
Returns
Twig_Source
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_SourceContextLoaderInterface.

Definition at line 141 of file Filesystem.php.

142 {
143 $path = $this->findTemplate($name);
144
145 return new Twig_Source(file_get_contents($path), $name, $path);
146 }
Holds information about a non-compiled Twig template.
Definition: Source.php:20

References $name, $path, and findTemplate().

+ Here is the call graph for this function:

◆ isAbsolutePath()

Twig_Loader_Filesystem::isAbsolutePath (   $file)
private

Definition at line 278 of file Filesystem.php.

279 {
280 return strspn($file, '/\\', 0, 1)
281 || (strlen($file) > 3 && ctype_alpha($file[0])
282 && ':' === substr($file, 1, 1)
283 && strspn($file, '/\\', 2, 1)
284 )
285 || null !== parse_url($file, PHP_URL_SCHEME)
286 ;
287 }
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file

References $file.

Referenced by addPath(), findTemplate(), and prependPath().

+ Here is the caller graph for this function:

◆ isFresh()

Twig_Loader_Filesystem::isFresh (   $name,
  $time 
)

Returns true if the template is still fresh.

Parameters
string$nameThe template name
int$timeTimestamp of the last modification time of the cached template
Returns
bool true if the template is fresh, false otherwise
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_LoaderInterface.

Definition at line 176 of file Filesystem.php.

177 {
178 return filemtime($this->findTemplate($name)) <= $time;
179 }
$time
Definition: cron.php:21

References $name, $time, and findTemplate().

+ Here is the call graph for this function:

◆ normalizeName()

Twig_Loader_Filesystem::normalizeName (   $name)
protected

Definition at line 251 of file Filesystem.php.

252 {
253 return preg_replace('#/{2,}#', '/', str_replace('\\', '/', (string) $name));
254 }

References $name.

Referenced by exists(), and findTemplate().

+ Here is the caller graph for this function:

◆ parseName()

Twig_Loader_Filesystem::parseName (   $name,
  $default = self::MAIN_NAMESPACE 
)
protected

Definition at line 235 of file Filesystem.php.

236 {
237 if (isset($name[0]) && '@' == $name[0]) {
238 if (false === $pos = strpos($name, '/')) {
239 throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
240 }
241
242 $namespace = substr($name, 1, $pos - 1);
243 $shortname = substr($name, $pos + 1);
244
245 return array($namespace, $shortname);
246 }
247
248 return array($default, $name);
249 }

References $name, $namespace, and sprintf.

Referenced by findTemplate().

+ Here is the caller graph for this function:

◆ prependPath()

Twig_Loader_Filesystem::prependPath (   $path,
  $namespace = self::MAIN_NAMESPACE 
)

Prepends a path where templates are stored.

Parameters
string$pathA path where to look for templates
string$namespaceA path namespace
Exceptions
Twig_Error_Loader

Definition at line 115 of file Filesystem.php.

116 {
117 // invalidate the cache
118 $this->cache = $this->errorCache = array();
119
120 $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
121 if (!is_dir($checkPath)) {
122 throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath));
123 }
124
125 $path = rtrim($path, '/\\');
126
127 if (!isset($this->paths[$namespace])) {
128 $this->paths[$namespace][] = $path;
129 } else {
130 array_unshift($this->paths[$namespace], $path);
131 }
132 }

References $namespace, $path, isAbsolutePath(), and sprintf.

+ Here is the call graph for this function:

◆ setPaths()

Twig_Loader_Filesystem::setPaths (   $paths,
  $namespace = self::MAIN_NAMESPACE 
)

Sets the paths where templates are stored.

Parameters
string | array$pathsA path or an array of paths where to look for templates
string$namespaceA path namespace

Definition at line 74 of file Filesystem.php.

75 {
76 if (!is_array($paths)) {
77 $paths = array($paths);
78 }
79
80 $this->paths[$namespace] = array();
81 foreach ($paths as $path) {
82 $this->addPath($path, $namespace);
83 }
84 }
addPath($path, $namespace=self::MAIN_NAMESPACE)
Adds a path where templates are stored.
Definition: Filesystem.php:94

References $namespace, $path, $paths, and addPath().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateName()

Twig_Loader_Filesystem::validateName (   $name)
protected

Definition at line 256 of file Filesystem.php.

257 {
258 if (false !== strpos($name, "\0")) {
259 throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
260 }
261
262 $name = ltrim($name, '/');
263 $parts = explode('/', $name);
264 $level = 0;
265 foreach ($parts as $part) {
266 if ('..' === $part) {
267 --$level;
268 } elseif ('.' !== $part) {
269 ++$level;
270 }
271
272 if ($level < 0) {
273 throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
274 }
275 }
276 }

References $name, and sprintf.

Referenced by findTemplate().

+ Here is the caller graph for this function:

Field Documentation

◆ $cache

Twig_Loader_Filesystem::$cache = array()
protected

Definition at line 23 of file Filesystem.php.

◆ $errorCache

Twig_Loader_Filesystem::$errorCache = array()
protected

Definition at line 24 of file Filesystem.php.

◆ $paths

Twig_Loader_Filesystem::$paths = array()
protected

Definition at line 22 of file Filesystem.php.

Referenced by __construct(), and setPaths().

◆ $rootPath

Twig_Loader_Filesystem::$rootPath
private

Definition at line 26 of file Filesystem.php.

Referenced by __construct().

◆ MAIN_NAMESPACE

const Twig_Loader_Filesystem::MAIN_NAMESPACE = '__main__'

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