PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/modules/fuel/libraries/Fuel_cache.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 521 lines | 178 code | 57 blank | 286 comment | 16 complexity | 871b2d9896b59a4acbbd315700bbbe17 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2018, Daylight Studio LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL cache object
  19. *
  20. * This class is used for managing the different kinds of cached files used
  21. * with FUEL including pages, compiled templates and asset files.
  22. *
  23. * @package FUEL CMS
  24. * @subpackage Libraries
  25. * @category Libraries
  26. * @author David McReynolds @ Daylight Studio
  27. * @link http://docs.getfuelcms.com/libraries/fuel_cache
  28. */
  29. // --------------------------------------------------------------------
  30. class Fuel_cache extends Fuel_base_library {
  31. public $ignore = '#^(\..+)|(index\.html)#'; // Regular expression of files to exclude from clearing like .gitignore and .htaccess
  32. public $cache_path = ''; // The cache path. If no path is provided it will use the cache path value found in the main CI config file.
  33. protected $_cache; // the Cache object used for saving, retrieving and deleting cached files
  34. protected $_types = array(
  35. 'compiled',
  36. 'pages',
  37. 'assets',
  38. ); // method names used when doing clear()
  39. // --------------------------------------------------------------------
  40. /**
  41. * Constructor
  42. *
  43. * Accepts an associative array as input, containing preferences (optional)
  44. *
  45. * @access public
  46. * @param array config preferences
  47. * @return void
  48. */
  49. public function __construct($params = array())
  50. {
  51. parent::__construct();
  52. $this->CI->load->library('cache');
  53. $this->_cache = & $this->CI->cache;
  54. $this->initialize($params);
  55. }
  56. // --------------------------------------------------------------------
  57. /**
  58. * Initialize the object and set object parameters
  59. *
  60. * Accepts an associative array as input, containing object preferences.
  61. *
  62. * @access public
  63. * @param array Array of initialization parameters (optional)
  64. * @return void
  65. */
  66. public function initialize($params = array())
  67. {
  68. parent::initialize($params);
  69. // set the cache path to the configs cache path if left empty
  70. if (empty($this->cache_path))
  71. {
  72. $this->set_cache_path($this->CI->config->item('cache_path'));
  73. }
  74. // set the compile templates path
  75. if (empty($this->compiled_path))
  76. {
  77. $this->set_compiled_path($this->fuel->parser->get_compiled_dir());
  78. }
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Sets the cache path
  83. *
  84. * @access public
  85. * @param string The path to the cache folder
  86. * @return void
  87. */
  88. public function set_cache_path($path)
  89. {
  90. $this->cache_path = $path;
  91. $this->_cache->set_cache_path($this->cache_path);
  92. }
  93. // --------------------------------------------------------------------
  94. /**
  95. * Sets the compiled templates path
  96. *
  97. * @access public
  98. * @param string The path to the compiled templates folder
  99. * @return void
  100. */
  101. public function set_compiled_path($path)
  102. {
  103. $this->compiled_path = $path;
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Creates a cache ID based on the page location
  108. *
  109. * If no $location value is provided, then the ID will be based on the current URI segments
  110. *
  111. * <code>
  112. * $cache_id = $this->fuel->cache->create_id(); // create a cache id... this will be based on the current URI location if no parameters are passed
  113. * </code>
  114. *
  115. * @access public
  116. * @param string Location used in creating the ID (optional)
  117. * @param string Language of page (optional)
  118. * @return string
  119. */
  120. public function create_id($location = NULL, $lang = NULL)
  121. {
  122. if (empty($lang))
  123. {
  124. $lang = ($this->fuel->language->has_multiple()) ? $this->fuel->language->detect() : $this->fuel->language->default_option();
  125. }
  126. if (empty($location))
  127. {
  128. // $segs = $this->CI->uri->segment_array();
  129. $uri = str_replace('/', '.', uri_path());
  130. if (empty($uri))
  131. {
  132. return 'home.'.$lang;
  133. }
  134. return $lang.'.'.$uri;
  135. }
  136. $id = $lang.'.'.$location;
  137. return str_replace('/', '.', $id);
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Saves an item to the cache
  142. *
  143. * <code>
  144. * $cache_id = $this->fuel->cache->create_id();
  145. * $data = 'These are not the droids you are looking for.';
  146. * $file = $this->fuel->cache->save($cache_id, $data, NULL, 3600); // sets the cached item with a TTL of one hour
  147. * </code>
  148. *
  149. * @access public
  150. * @param string Cache ID
  151. * @param string Cache group ID
  152. * @param mixed Data to save to the cache (optional)
  153. * @param int Time to live in seconds for cache (optional)
  154. * @return void
  155. */
  156. public function save($cache_id, $data, $group = NULL, $ttl = NULL)
  157. {
  158. $this->_cache->save($cache_id, $data, $group, $ttl);
  159. }
  160. // --------------------------------------------------------------------
  161. /**
  162. * Get and return an item from the cache
  163. *
  164. * <code>
  165. * $cache_id = $this->fuel->cache->create_id();
  166. * $file = $this->fuel->cache->get($cache_id, 'pages', FALSE);
  167. * </code>
  168. *
  169. * @access public
  170. * @param string Cache ID
  171. * @param string Cache group ID (optional)
  172. * @param boolean Skip checking if it is in the cache or not (optional)
  173. * @return string The contents of the cached file or NULL if it doesn't exist
  174. */
  175. public function get($cache_id, $cache_group = NULL, $skip_checking = FALSE)
  176. {
  177. return $this->_cache->get($cache_id, $cache_group, $skip_checking);
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Checks if the file is cached based on the cache_id passed
  182. *
  183. * <code>
  184. * $cache_id = $this->fuel->cache->create_id();
  185. * if ($this->fuel->cache->is_cached($cache_id)){
  186. * echo 'cached';
  187. * } else {
  188. * echo 'not cached';
  189. * }
  190. * </code>
  191. *
  192. * @access public
  193. * @param string Cache ID
  194. * @param string Cache group ID (optional)
  195. * @return boolean
  196. */
  197. public function is_cached($cache_id, $group = NULL)
  198. {
  199. return $this->_cache->is_cached($cache_id, $group);
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Clears the various types of caches
  204. *
  205. * Value passed can be either a string or an array.
  206. * If a string, the value must be "compiled", "pages" or "assets".
  207. * If an array, the array must contain one or more of the values (e.g. array("compiled", "pages", "assets"))
  208. * If no parameters are passed, then all caches are cleared
  209. *
  210. * <code>
  211. * $this->fuel->cache->clear(array('compiled', 'pages')); // as an array
  212. * $this->fuel->cache->clear('assets'); // as string
  213. * </code>
  214. *
  215. * @access public
  216. * @param mixed Value can be either a string of one value or an array of multiple values. Valid values are compiled, pages and assets. (optional)
  217. * @return void
  218. */
  219. public function clear($what = NULL)
  220. {
  221. if (!empty($what) AND is_array($what))
  222. {
  223. foreach($what as $w)
  224. {
  225. if (in_array($w, $this->_types))
  226. {
  227. $method = 'clear_'.$w;
  228. if (method_exists($this, $method))
  229. {
  230. $this->$method();
  231. }
  232. }
  233. }
  234. }
  235. else if (!empty($what) AND is_string($what))
  236. {
  237. if (in_array($what, $this->_types))
  238. {
  239. $this->$what();
  240. }
  241. }
  242. else
  243. {
  244. $this->clear_all();
  245. }
  246. }
  247. // --------------------------------------------------------------------
  248. /**
  249. * Clears the compiled templating files
  250. *
  251. * <code>
  252. * $this->fuel->cache->clear(array('compiled', 'pages')); // as an array
  253. * $this->fuel->cache->clear('assets'); // as string
  254. * </code>
  255. *
  256. * @access public
  257. * @return void
  258. */
  259. public function clear_compiled()
  260. {
  261. // also delete DWOO compiled files
  262. $this->CI->load->helper('file');
  263. // remove all compiled files
  264. $parser_compile_path = $this->fuel->parser->get_compiled_dir();
  265. if (is_dir($parser_compile_path) AND is_writable($parser_compile_path))
  266. {
  267. $this->_delete_files($parser_compile_path, TRUE);
  268. }
  269. // remove all cache files
  270. // $parser_cache_path =& $config['parser_cache_dir'];
  271. // if (is_dir($parser_cache_path) AND is_writable($parser_cache_path))
  272. // {
  273. // $compiled_folder = trim(str_replace($dwoo_cache_path, '', $dwoo_compile_path), '/');
  274. // $ignore = array($compiled_folder, 'index.html');
  275. // delete_files($dwoo_cache_path, TRUE, $ignore);
  276. // }
  277. // remove asset cache files if exist
  278. $modules = $this->fuel->config('modules_allowed');
  279. $modules[] = FUEL_FOLDER; // fuel
  280. $modules[] = ''; // main application assets
  281. foreach($modules as $module)
  282. {
  283. // check if there is a css module assets file and load it so it will be ready when the page is ajaxed in
  284. $cache_folder = assets_server_path($this->CI->asset->assets_cache_folder, 'cache', $module);
  285. if (is_dir($cache_folder) AND is_writable($cache_folder))
  286. {
  287. $this->_delete_files($cache_folder);
  288. }
  289. }
  290. }
  291. // --------------------------------------------------------------------
  292. /**
  293. * Clears the pages cache
  294. *
  295. * <code>
  296. * $this->fuel->cache->clear_pages();
  297. * </code>
  298. *
  299. * @access public
  300. * @return void
  301. */
  302. public function clear_pages()
  303. {
  304. $cache_group = $this->fuel->config('page_cache_group');
  305. $this->_cache->remove_group($cache_group);
  306. }
  307. // --------------------------------------------------------------------
  308. /**
  309. * Clear a single page from the cache
  310. *
  311. * <code>
  312. * $this->fuel->cache->clear_page('about/history');
  313. * </code>
  314. *
  315. * @access public
  316. * @param string Page location
  317. * @return void
  318. */
  319. public function clear_page($location)
  320. {
  321. $cache_group = $this->fuel->config('page_cache_group');
  322. $cache_id = $this->create_id($location);
  323. $this->_cache->remove($cache_id, $cache_group);
  324. }
  325. // --------------------------------------------------------------------
  326. /**
  327. * Clears the assets cache
  328. *
  329. * Will look in module asset folders if the <dfn>fuel/{module}/assets/cache/</dfn> directory exist for that module
  330. *
  331. * <code>
  332. * $this->fuel->cache->clear_assets();
  333. * </code>
  334. *
  335. * @access public
  336. * @return void
  337. */
  338. public function clear_assets()
  339. {
  340. // remove asset cache files if exist
  341. $modules = $this->fuel->config('modules_allowed');
  342. $modules[] = FUEL_FOLDER; // fuel
  343. $modules[] = ''; // main application assets
  344. foreach($modules as $module)
  345. {
  346. // check if there is a css module assets file and load it so it will be ready when the page is ajaxed in
  347. $cache_folder = assets_server_path($this->CI->asset->assets_cache_folder, 'cache', $module);
  348. if (is_dir($cache_folder) AND is_writable($cache_folder))
  349. {
  350. $this->_delete_files($cache_folder);
  351. }
  352. }
  353. }
  354. // --------------------------------------------------------------------
  355. /**
  356. * Clears a single cache file
  357. *
  358. * <code>
  359. * $cache_id = $this->fuel->cache->create_id();
  360. * $this->fuel->cache->clear_file($cache_id);
  361. * </code>
  362. *
  363. * @access public
  364. * @param string Cache ID
  365. * @return void
  366. */
  367. public function clear_file($cache_id)
  368. {
  369. $this->_cache->remove($cache_id);
  370. }
  371. // --------------------------------------------------------------------
  372. /**
  373. * Clears a group of cached files
  374. *
  375. * <code>
  376. * $group = 'pages';
  377. * $this->fuel->cache->clear_group($group);
  378. * </code>
  379. *
  380. * @access public
  381. * @param string Cache group ID
  382. * @return void
  383. */
  384. public function clear_group($group)
  385. {
  386. $this->_cache->remove_group($group);
  387. }
  388. // --------------------------------------------------------------------
  389. /**
  390. * Clears the cache folder for a particular module
  391. *
  392. * <code>
  393. * $module = 'my_module';
  394. * $this->fuel->cache->clear_module($module);
  395. * </code>
  396. *
  397. * @access public
  398. * @param string Module name
  399. * @return void
  400. */
  401. public function clear_module($module)
  402. {
  403. $module_path = MODULES_PATH.$module.'/cache';
  404. if (file_exists($module_path))
  405. {
  406. $this->_delete_files($module_path);
  407. }
  408. }
  409. // --------------------------------------------------------------------
  410. /**
  411. * Clears all the allowed modules cache folders
  412. *
  413. * <code>
  414. * $this->fuel->cache->clear_all_modules();
  415. * </code>
  416. *
  417. * @access public
  418. * @return void
  419. */
  420. public function clear_all_modules()
  421. {
  422. $modules = $this->CI->fuel->config('modules_allowed');
  423. foreach($modules as $module)
  424. {
  425. $this->clear_module($module);
  426. }
  427. }
  428. // --------------------------------------------------------------------
  429. /**
  430. * Clears all cache types
  431. *
  432. * Will remove page, compiled, and cached asset files
  433. *
  434. * <code>
  435. * $this->fuel->cache->clear_all();
  436. * </code>
  437. *
  438. * @access public
  439. * @return void
  440. */
  441. public function clear_all()
  442. {
  443. $this->clear_pages();
  444. $this->clear_compiled();
  445. $this->clear_assets();
  446. $this->clear_all_modules();
  447. $this->_delete_files($this->cache_path);
  448. }
  449. // --------------------------------------------------------------------
  450. /**
  451. * Deletes cached files from specified path
  452. *
  453. * @access protected
  454. * @param string path to file
  455. * @param bool delete the directories (optional)
  456. * @return void
  457. */
  458. protected function _delete_files($path, $include_dir = FALSE)
  459. {
  460. @delete_files($path, $include_dir, $this->ignore);
  461. }
  462. }
  463. /* End of file Fuel_cache.php */
  464. /* Location: ./modules/fuel/libraries/Fuel_cache.php */