PEEL Shopping
Open source ecommerce : PEEL Shopping
Filesystem.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 2009 Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
18 {
20  const MAIN_NAMESPACE = '__main__';
21 
22  protected $paths;
23  protected $cache;
24 
30  public function __construct($paths = array())
31  {
32  if ($paths) {
33  $this->setPaths($paths);
34  }
35  }
36 
44  public function getPaths($namespace = self::MAIN_NAMESPACE)
45  {
46  return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
47  }
48 
56  public function getNamespaces()
57  {
58  return array_keys($this->paths);
59  }
60 
67  public function setPaths($paths, $namespace = self::MAIN_NAMESPACE)
68  {
69  if (!is_array($paths)) {
70  $paths = array($paths);
71  }
72 
73  $this->paths[$namespace] = array();
74  foreach ($paths as $path) {
75  $this->addPath($path, $namespace);
76  }
77  }
78 
87  public function addPath($path, $namespace = self::MAIN_NAMESPACE)
88  {
89  // invalidate the cache
90  $this->cache = array();
91 
92  if (!is_dir($path)) {
93  throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
94  }
95 
96  $this->paths[$namespace][] = rtrim($path, '/\\');
97  }
98 
107  public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
108  {
109  // invalidate the cache
110  $this->cache = array();
111 
112  if (!is_dir($path)) {
113  throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
114  }
115 
116  $path = rtrim($path, '/\\');
117 
118  if (!isset($this->paths[$namespace])) {
119  $this->paths[$namespace][] = $path;
120  } else {
121  array_unshift($this->paths[$namespace], $path);
122  }
123  }
124 
128  public function getSource($name)
129  {
130  return file_get_contents($this->findTemplate($name));
131  }
132 
136  public function getCacheKey($name)
137  {
138  return $this->findTemplate($name);
139  }
140 
144  public function exists($name)
145  {
146  $name = (string) $name;
147  if (isset($this->cache[$name])) {
148  return true;
149  }
150 
151  try {
152  $this->findTemplate($name);
153 
154  return true;
155  } catch (Twig_Error_Loader $exception) {
156  return false;
157  }
158  }
159 
163  public function isFresh($name, $time)
164  {
165  return filemtime($this->findTemplate($name)) <= $time;
166  }
167 
168  protected function findTemplate($name)
169  {
170  $name = (string) $name;
171 
172  // normalize name
173  $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
174 
175  if (isset($this->cache[$name])) {
176  return $this->cache[$name];
177  }
178 
179  $this->validateName($name);
180 
181  $namespace = self::MAIN_NAMESPACE;
182  if (isset($name[0]) && '@' == $name[0]) {
183  if (false === $pos = strpos($name, '/')) {
184  throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
185  }
186 
187  $namespace = substr($name, 1, $pos - 1);
188 
189  $name = substr($name, $pos + 1);
190  }
191 
192  if (!isset($this->paths[$namespace])) {
193  throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
194  }
195 
196  foreach ($this->paths[$namespace] as $path) {
197  if (is_file($path.'/'.$name)) {
198  return $this->cache[$name] = $path.'/'.$name;
199  }
200  }
201 
202  throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
203  }
204 
205  protected function validateName($name)
206  {
207  if (false !== strpos($name, "\0")) {
208  throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
209  }
210 
211  $name = ltrim($name, '/');
212  $parts = explode('/', $name);
213  $level = 0;
214  foreach ($parts as $part) {
215  if ('..' === $part) {
216  --$level;
217  } elseif ('.' !== $part) {
218  ++$level;
219  }
220 
221  if ($level < 0) {
222  throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
223  }
224  }
225  }
226 }
Loads template from the filesystem.
Definition: Filesystem.php:17
prependPath($path, $namespace=self::MAIN_NAMESPACE)
Prepends a path where templates are stored.
Definition: Filesystem.php:107
getNamespaces()
Returns the path namespaces.
Definition: Filesystem.php:56
getSource($name)
{Gets the source code of a template, given its name.The name of the template to loadstring The templa...
Definition: Filesystem.php:128
Adds an exists() method for loaders.
isFresh($name, $time)
{Returns true if the template is still fresh.The template name The last modification time of the cach...
Definition: Filesystem.php:163
__construct($paths=array())
Constructor.
Definition: Filesystem.php:30
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
setPaths($paths, $namespace=self::MAIN_NAMESPACE)
Sets the paths where templates are stored.
Definition: Filesystem.php:67
getCacheKey($name)
{Gets the cache key to use for the cache for a given template name.The name of the template to loadst...
Definition: Filesystem.php:136
getPaths($namespace=self::MAIN_NAMESPACE)
Returns the paths to the templates.
Definition: Filesystem.php:44
addPath($path, $namespace=self::MAIN_NAMESPACE)
Adds a path where templates are stored.
Definition: Filesystem.php:87
Interface all loaders must implement.
const MAIN_NAMESPACE
Identifier of the main namespace.
Definition: Filesystem.php:20
Exception thrown when an error occurs during template loading.
Definition: Loader.php:25
exists($name)
{Check if we have the source code of a template, given its name.The name of the template to check if ...
Definition: Filesystem.php:144

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:18 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.