Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
controller.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Cache
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Public cache handler
14  *
15  * @package Joomla.Platform
16  * @subpackage Cache
17  * @since 11.1
18  */
20 {
21  /**
22  * JCache object
23  *
24  * @var JCache
25  * @since 11.1
26  */
27  public $cache;
28 
29  /**
30  * Array of options
31  *
32  * @var array
33  * @since 11.1
34  */
35  public $options;
36 
37  /**
38  * Constructor
39  *
40  * @param array $options Array of options
41  *
42  * @since 11.1
43  */
44  public function __construct($options)
45  {
46  $this->cache = new JCache($options);
47  $this->options = & $this->cache->_options;
48 
49  // Overwrite default options with given options
50  foreach ($options as $option => $value)
51  {
52  if (isset($options[$option]))
53  {
54  $this->options[$option] = $options[$option];
55  }
56  }
57  }
58 
59  /**
60  * Magic method to proxy JCacheControllerMethods
61  *
62  * @param string $name Name of the function
63  * @param array $arguments Array of arguments for the function
64  *
65  * @return mixed
66  *
67  * @since 11.1
68  */
69  public function __call($name, $arguments)
70  {
71  $nazaj = call_user_func_array(array($this->cache, $name), $arguments);
72  return $nazaj;
73  }
74 
75  /**
76  * Returns a reference to a cache adapter object, always creating it
77  *
78  * @param string $type The cache object type to instantiate; default is output.
79  * @param array $options Array of options
80  *
81  * @return JCache A JCache object
82  *
83  * @since 11.1
84  * @throws RuntimeException
85  */
86  public static function getInstance($type = 'output', $options = array())
87  {
88  self::addIncludePath(JPATH_PLATFORM . '/joomla/cache/controller');
89 
90  $type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
91 
92  $class = 'JCacheController' . ucfirst($type);
93 
94  if (!class_exists($class))
95  {
96  // Search for the class file in the JCache include paths.
97  jimport('joomla.filesystem.path');
98 
99  if ($path = JPath::find(self::addIncludePath(), strtolower($type) . '.php'))
100  {
101  include_once $path;
102  }
103  else
104  {
105  throw new RuntimeException('Unable to load Cache Controller: ' . $type, 500);
106  }
107  }
108 
109  return new $class($options);
110  }
111 
112  /**
113  * Set caching enabled state
114  *
115  * @param boolean $enabled True to enable caching
116  *
117  * @return void
118  *
119  * @since 11.1
120  */
121  public function setCaching($enabled)
122  {
123  $this->cache->setCaching($enabled);
124  }
125 
126  /**
127  * Set cache lifetime
128  *
129  * @param integer $lt Cache lifetime
130  *
131  * @return void
132  *
133  * @since 11.1
134  */
135  public function setLifeTime($lt)
136  {
137  $this->cache->setLifeTime($lt);
138  }
139 
140  /**
141  * Add a directory where JCache should search for controllers. You may
142  * either pass a string or an array of directories.
143  *
144  * @param string $path A path to search.
145  *
146  * @return array An array with directory elements
147  *
148  * @since 11.1
149  */
150  public static function addIncludePath($path = '')
151  {
152  static $paths;
153 
154  if (!isset($paths))
155  {
156  $paths = array();
157  }
158  if (!empty($path) && !in_array($path, $paths))
159  {
160  jimport('joomla.filesystem.path');
161  array_unshift($paths, JPath::clean($path));
162  }
163  return $paths;
164  }
165 
166  /**
167  * Get stored cached data by id and group
168  *
169  * @param string $id The cache data id
170  * @param string $group The cache data group
171  *
172  * @return mixed False on no result, cached object otherwise
173  *
174  * @since 11.1
175  */
176  public function get($id, $group = null)
177  {
178  $data = $this->cache->get($id, $group);
179 
180  if ($data === false)
181  {
182  $locktest = new stdClass;
183  $locktest->locked = null;
184  $locktest->locklooped = null;
185  $locktest = $this->cache->lock($id, $group);
186  if ($locktest->locked == true && $locktest->locklooped == true)
187  {
188  $data = $this->cache->get($id, $group);
189  }
190  if ($locktest->locked == true)
191  {
192  $this->cache->unlock($id, $group);
193  }
194  }
195 
196  // Check again because we might get it from second attempt
197  if ($data !== false)
198  {
199  // Trim to fix unserialize errors
200  $data = unserialize(trim($data));
201  }
202  return $data;
203  }
204 
205  /**
206  * Store data to cache by id and group
207  *
208  * @param mixed $data The data to store
209  * @param string $id The cache data id
210  * @param string $group The cache data group
211  * @param boolean $wrkarounds True to use wrkarounds
212  *
213  * @return boolean True if cache stored
214  *
215  * @since 11.1
216  */
217  public function store($data, $id, $group = null, $wrkarounds = true)
218  {
219  $locktest = new stdClass;
220  $locktest->locked = null;
221  $locktest->locklooped = null;
222 
223  $locktest = $this->cache->lock($id, $group);
224 
225  if ($locktest->locked == false && $locktest->locklooped == true)
226  {
227  $locktest = $this->cache->lock($id, $group);
228  }
229 
230  $sucess = $this->cache->store(serialize($data), $id, $group);
231 
232  if ($locktest->locked == true)
233  {
234  $this->cache->unlock($id, $group);
235  }
236 
237  return $sucess;
238  }
239 }