/libraries/joomla/cache/controller.php

https://github.com/pollen8/joomla-platform · PHP · 362 lines · 205 code · 35 blank · 122 comment · 43 complexity · 322c5e9c11f3447ec8f239a8bfe05afc MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. <<<<<<< HEAD
  10. defined('JPATH_PLATFORM') or die;
  11. =======
  12. defined('JPATH_PLATFORM') or die();
  13. >>>>>>> upstream/master
  14. /**
  15. * Public cache handler
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage Cache
  19. * @since 11.1
  20. */
  21. class JCacheController
  22. {
  23. /**
  24. <<<<<<< HEAD
  25. * @var
  26. * @since 11.1
  27. =======
  28. * @var JCache
  29. * @since 11.1
  30. >>>>>>> upstream/master
  31. */
  32. public $cache;
  33. /**
  34. * @var array Array of options
  35. * @since 11.1
  36. */
  37. public $options;
  38. /**
  39. * Constructor
  40. *
  41. * @param array $options Array of options
  42. <<<<<<< HEAD
  43. =======
  44. *
  45. >>>>>>> upstream/master
  46. * @since 11.1
  47. */
  48. public function __construct($options)
  49. {
  50. <<<<<<< HEAD
  51. $this->cache = new JCache($options);
  52. $this->options = & $this->cache->_options;
  53. // Overwrite default options with given options
  54. foreach ($options AS $option=>$value) {
  55. if (isset($options[$option])) {
  56. =======
  57. $this->cache = new JCache($options);
  58. $this->options = & $this->cache->_options;
  59. // Overwrite default options with given options
  60. foreach ($options as $option => $value)
  61. {
  62. if (isset($options[$option]))
  63. {
  64. >>>>>>> upstream/master
  65. $this->options[$option] = $options[$option];
  66. }
  67. }
  68. }
  69. /**
  70. <<<<<<< HEAD
  71. *
  72. * @param $name
  73. * @param $arguments
  74. * @since 11.1
  75. */
  76. public function __call ($name, $arguments)
  77. {
  78. $nazaj = call_user_func_array (array ($this->cache, $name), $arguments);
  79. =======
  80. * Magic method to proxy JCacheControllerMethods
  81. *
  82. * @param string $name Name of the function
  83. * @param array $arguments Array of arguments for the function
  84. *
  85. * @return mixed
  86. *
  87. * @since 11.1
  88. */
  89. public function __call($name, $arguments)
  90. {
  91. $nazaj = call_user_func_array(array($this->cache, $name), $arguments);
  92. >>>>>>> upstream/master
  93. return $nazaj;
  94. }
  95. /**
  96. * Returns a reference to a cache adapter object, always creating it
  97. *
  98. <<<<<<< HEAD
  99. * @param string $type The cache object type to instantiate; default is output.
  100. * @param array $options Array of options
  101. *
  102. * @return JCache A JCache object
  103. =======
  104. * @param string $type The cache object type to instantiate; default is output.
  105. * @param array $options Array of options
  106. *
  107. * @return JCache A JCache object
  108. *
  109. >>>>>>> upstream/master
  110. * @since 11.1
  111. */
  112. public static function getInstance($type = 'output', $options = array())
  113. {
  114. <<<<<<< HEAD
  115. JCacheController::addIncludePath(JPATH_PLATFORM.DS.'joomla'.DS.'cache'.DS.'controller');
  116. $type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
  117. $class = 'JCacheController'.ucfirst($type);
  118. if (!class_exists($class)) {
  119. // Search for the class file in the JCache include paths.
  120. jimport('joomla.filesystem.path');
  121. if ($path = JPath::find(JCacheController::addIncludePath(), strtolower($type).'.php')) {
  122. require_once $path;
  123. } else {
  124. JError::raiseError(500, 'Unable to load Cache Controller: '.$type);
  125. =======
  126. JCacheController::addIncludePath(JPATH_PLATFORM . '/joomla/cache/controller');
  127. $type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
  128. $class = 'JCacheController' . ucfirst($type);
  129. if (!class_exists($class))
  130. {
  131. // Search for the class file in the JCache include paths.
  132. jimport('joomla.filesystem.path');
  133. if ($path = JPath::find(JCacheController::addIncludePath(), strtolower($type) . '.php'))
  134. {
  135. include_once $path;
  136. }
  137. else
  138. {
  139. JError::raiseError(500, 'Unable to load Cache Controller: ' . $type);
  140. >>>>>>> upstream/master
  141. }
  142. }
  143. return new $class($options);
  144. }
  145. /**
  146. * Set caching enabled state
  147. *
  148. * @param boolean $enabled True to enable caching
  149. *
  150. * @return void
  151. <<<<<<< HEAD
  152. =======
  153. *
  154. >>>>>>> upstream/master
  155. * @since 11.1
  156. */
  157. public function setCaching($enabled)
  158. {
  159. $this->cache->setCaching($enabled);
  160. }
  161. /**
  162. * Set cache lifetime
  163. *
  164. * @param integer $lt Cache lifetime
  165. *
  166. * @return void
  167. <<<<<<< HEAD
  168. =======
  169. *
  170. >>>>>>> upstream/master
  171. * @since 11.1
  172. */
  173. public function setLifeTime($lt)
  174. {
  175. $this->cache->setLifeTime($lt);
  176. }
  177. /**
  178. * Add a directory where JCache should search for controllers. You may
  179. * either pass a string or an array of directories.
  180. *
  181. <<<<<<< HEAD
  182. * @param string A path to search.
  183. *
  184. * @return array An array with directory elements
  185. * @since 11.1
  186. */
  187. public static function addIncludePath($path='')
  188. {
  189. static $paths;
  190. if (!isset($paths)) {
  191. $paths = array();
  192. }
  193. if (!empty($path) && !in_array($path, $paths)) {
  194. =======
  195. * @param string $path A path to search.
  196. *
  197. * @return array An array with directory elements
  198. *
  199. * @since 11.1
  200. */
  201. public static function addIncludePath($path = '')
  202. {
  203. static $paths;
  204. if (!isset($paths))
  205. {
  206. $paths = array();
  207. }
  208. if (!empty($path) && !in_array($path, $paths))
  209. {
  210. >>>>>>> upstream/master
  211. jimport('joomla.filesystem.path');
  212. array_unshift($paths, JPath::clean($path));
  213. }
  214. return $paths;
  215. }
  216. /**
  217. * Get stored cached data by id and group
  218. *
  219. <<<<<<< HEAD
  220. * @param string $id The cache data id
  221. * @param string $group The cache data group
  222. *
  223. * @return mixed False on no result, cached object otherwise
  224. * @since 11.1
  225. */
  226. public function get($id, $group=null)
  227. =======
  228. * @param string $id The cache data id
  229. * @param string $group The cache data group
  230. *
  231. * @return mixed False on no result, cached object otherwise
  232. *
  233. * @since 11.1
  234. */
  235. public function get($id, $group = null)
  236. >>>>>>> upstream/master
  237. {
  238. $data = false;
  239. $data = $this->cache->get($id, $group);
  240. <<<<<<< HEAD
  241. if ($data === false) {
  242. =======
  243. if ($data === false)
  244. {
  245. >>>>>>> upstream/master
  246. $locktest = new stdClass;
  247. $locktest->locked = null;
  248. $locktest->locklooped = null;
  249. $locktest = $this->cache->lock($id, $group);
  250. <<<<<<< HEAD
  251. if ($locktest->locked == true && $locktest->locklooped == true) {
  252. $data = $this->cache->get($id, $group);
  253. }
  254. if ($locktest->locked == true) $this->cache->unlock($id, $group);
  255. }
  256. // Check again because we might get it from second attempt
  257. if ($data !== false) {
  258. $data = unserialize(trim($data)); // trim to fix unserialize errors
  259. =======
  260. if ($locktest->locked == true && $locktest->locklooped == true)
  261. {
  262. $data = $this->cache->get($id, $group);
  263. }
  264. if ($locktest->locked == true)
  265. {
  266. $this->cache->unlock($id, $group);
  267. }
  268. }
  269. // Check again because we might get it from second attempt
  270. if ($data !== false)
  271. {
  272. $data = unserialize(trim($data)); // trim to fix unserialize errors
  273. >>>>>>> upstream/master
  274. }
  275. return $data;
  276. }
  277. /**
  278. * Store data to cache by id and group
  279. *
  280. <<<<<<< HEAD
  281. * @param string $id The cache data id
  282. * @param string $group The cache data group
  283. * @param mixed $data The data to store
  284. *
  285. * @return boolean True if cache was stored
  286. * @since 11.1
  287. */
  288. public function store($data, $id, $group=null)
  289. =======
  290. * @param mixed $data The data to store
  291. * @param string $id The cache data id
  292. * @param string $group The cache data group
  293. *
  294. * @return boolean True if cache was stored
  295. *
  296. * @since 11.1
  297. */
  298. public function store($data, $id, $group = null)
  299. >>>>>>> upstream/master
  300. {
  301. $locktest = new stdClass;
  302. $locktest->locked = null;
  303. $locktest->locklooped = null;
  304. $locktest = $this->cache->lock($id, $group);
  305. <<<<<<< HEAD
  306. if ($locktest->locked == false && $locktest->locklooped == true) {
  307. $locktest = $this->cache->lock($id, $group);
  308. }
  309. $sucess = $this->cache->store(serialize($data), $id, $group);
  310. if ($locktest->locked == true) $this->cache->unlock($id, $group);
  311. return $sucess;
  312. }
  313. }
  314. =======
  315. if ($locktest->locked == false && $locktest->locklooped == true)
  316. {
  317. $locktest = $this->cache->lock($id, $group);
  318. }
  319. $sucess = $this->cache->store(serialize($data), $id, $group);
  320. if ($locktest->locked == true)
  321. {
  322. $this->cache->unlock($id, $group);
  323. }
  324. return $sucess;
  325. }
  326. }
  327. >>>>>>> upstream/master