/administrator/components/com_cache/models/cache.php

https://github.com/ot2sen/Molajo · PHP · 188 lines · 89 code · 29 blank · 70 comment · 12 complexity · 48fdc12d43c3ef320f5532808e4ea80a MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: cache.php 21518 2011-06-10 21:38:12Z chdemko $
  4. * @package Joomla.Administrator
  5. * @subpackage com_cache
  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.txt
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.modellist');
  12. /**
  13. * Cache Model
  14. *
  15. * @package Joomla.Administrator
  16. * @subpackage com_cache
  17. * * * @since 1.0
  18. */
  19. class CacheModelCache extends JModelList
  20. {
  21. /**
  22. * An Array of CacheItems indexed by cache group ID
  23. *
  24. * @var Array
  25. */
  26. protected $_data = array();
  27. /**
  28. * Group total
  29. *
  30. * @var integer
  31. */
  32. protected $_total = null;
  33. /**
  34. * Pagination object
  35. *
  36. * @var object
  37. */
  38. protected $_pagination = null;
  39. /**
  40. * Method to auto-populate the model state.
  41. *
  42. * Note. Calling getState in this method will result in recursion.
  43. *
  44. * @since 1.0
  45. */
  46. protected function populateState($ordering = null, $direction = null)
  47. {
  48. $app = MolajoFactory::getApplication();
  49. $applicationId = $this->getUserStateFromRequest($this->context.'.filter.application_id', 'filter_application_id', 0, 'int');
  50. $this->setState('applicationId', $applicationId == 1 ? 1 : 0);
  51. $application = JApplicationHelper::getApplicationInfo($applicationId);
  52. $this->setState('application', $application);
  53. parent::populateState('group', 'asc');
  54. }
  55. /**
  56. * Method to get cache data
  57. *
  58. * @return array
  59. */
  60. public function getData()
  61. {
  62. if (empty($this->_data)) {
  63. $cache = $this->getCache();
  64. $data = $cache->getAll();
  65. if ($data != false) {
  66. $this->_data = $data;
  67. $this->_total = count($data);
  68. if ($this->_total) {
  69. // Apply custom ordering
  70. $ordering = $this->getState('list.ordering');
  71. $direction = ($this->getState('list.direction') == 'asc') ? 1 : -1;
  72. jimport('joomla.utilities.arrayhelper');
  73. $this->_data = JArrayHelper::sortObjects($data, $ordering, $direction);
  74. // Apply custom pagination
  75. if ($this->_total > $this->getState('list.limit') && $this->getState('list.limit')) {
  76. $this->_data = array_slice($this->_data, $this->getState('list.start'), $this->getState('list.limit'));
  77. }
  78. }
  79. } else {
  80. $this->_data = array();
  81. }
  82. }
  83. return $this->_data;
  84. }
  85. /**
  86. * Method to get cache instance
  87. *
  88. * @return object
  89. */
  90. public function getCache()
  91. {
  92. $conf = MolajoFactory::getConfig();
  93. $options = array(
  94. 'defaultgroup' => '',
  95. 'storage' => $conf->get('cache_handler', ''),
  96. 'caching' => true,
  97. 'cachebase' => ($this->getState('applicationId') == 1) ? JPATH_ADMINISTRATOR . '/cache' : $conf->get('cache_path', JPATH_SITE . '/cache')
  98. );
  99. jimport('joomla.cache.cache');
  100. $cache = JCache::getInstance('', $options);
  101. return $cache;
  102. }
  103. /**
  104. * Method to get application data
  105. *
  106. * @return array
  107. */
  108. public function getApplication()
  109. {
  110. return $this->getState('application');
  111. }
  112. /**
  113. * Get the number of current Cache Groups
  114. *
  115. * @return int
  116. */
  117. public function getTotal()
  118. {
  119. if (empty($this->_total)) {
  120. $this->_total = count($this->getData());
  121. }
  122. return $this->_total;
  123. }
  124. /**
  125. * Method to get a pagination object for the cache
  126. *
  127. * @return integer
  128. */
  129. public function getPagination()
  130. {
  131. if (empty($this->_pagination)) {
  132. jimport('joomla.html.pagination');
  133. $this->_pagination = new JPagination($this->getTotal(), $this->getState('list.start'), $this->getState('list.limit'));
  134. }
  135. return $this->_pagination;
  136. }
  137. /**
  138. * Clean out a cache group as named by param.
  139. * If no param is passed clean all cache groups.
  140. *
  141. * @param String $group
  142. */
  143. public function clean($group = '')
  144. {
  145. $cache = $this->getCache();
  146. $cache->clean($group);
  147. }
  148. public function cleanlist($array)
  149. {
  150. foreach ($array as $group) {
  151. $this->clean($group);
  152. }
  153. }
  154. public function purge()
  155. {
  156. $cache = MolajoFactory::getCache('');
  157. return $cache->gc();
  158. }
  159. }