[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_cache/src/Model/ -> CacheModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_cache
   6   *
   7   * @copyright   (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Cache\Administrator\Model;
  12  
  13  use Joomla\CMS\Cache\Cache;
  14  use Joomla\CMS\Cache\CacheController;
  15  use Joomla\CMS\Cache\Exception\CacheConnectingException;
  16  use Joomla\CMS\Cache\Exception\UnsupportedCacheException;
  17  use Joomla\CMS\Factory;
  18  use Joomla\CMS\Language\Text;
  19  use Joomla\CMS\MVC\Model\ListModel;
  20  use Joomla\CMS\Pagination\Pagination;
  21  use Joomla\Utilities\ArrayHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Cache Model
  29   *
  30   * @since  1.6
  31   */
  32  class CacheModel extends ListModel
  33  {
  34      /**
  35       * An Array of CacheItems indexed by cache group ID
  36       *
  37       * @var array
  38       */
  39      protected $_data = array();
  40  
  41      /**
  42       * Group total
  43       *
  44       * @var integer
  45       */
  46      protected $_total = null;
  47  
  48      /**
  49       * Pagination object
  50       *
  51       * @var object
  52       */
  53      protected $_pagination = null;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param   array  $config  An optional associative array of configuration settings.
  59       *
  60       * @since   3.5
  61       */
  62      public function __construct($config = array())
  63      {
  64          if (empty($config['filter_fields'])) {
  65              $config['filter_fields'] = array(
  66                  'group',
  67                  'count',
  68                  'size',
  69                  'client_id',
  70              );
  71          }
  72  
  73          parent::__construct($config);
  74      }
  75  
  76      /**
  77       * Method to auto-populate the model state.
  78       *
  79       * Note. Calling getState in this method will result in recursion.
  80       *
  81       * @param   string  $ordering   Field for ordering.
  82       * @param   string  $direction  Direction of ordering.
  83       *
  84       * @return  void
  85       *
  86       * @since   1.6
  87       */
  88      protected function populateState($ordering = 'group', $direction = 'asc')
  89      {
  90          // Load the filter state.
  91          $this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
  92  
  93          parent::populateState($ordering, $direction);
  94      }
  95  
  96      /**
  97       * Method to get a store id based on model configuration state.
  98       *
  99       * This is necessary because the model is used by the component and
 100       * different modules that might need different sets of data or different
 101       * ordering requirements.
 102       *
 103       * @param   string  $id  A prefix for the store id.
 104       *
 105       * @return  string  A store id.
 106       *
 107       * @since   3.5
 108       */
 109      protected function getStoreId($id = '')
 110      {
 111          // Compile the store id.
 112          $id .= ':' . $this->getState('filter.search');
 113  
 114          return parent::getStoreId($id);
 115      }
 116  
 117      /**
 118       * Method to get cache data
 119       *
 120       * @return array
 121       */
 122      public function getData()
 123      {
 124          if (empty($this->_data)) {
 125              try {
 126                  $cache = $this->getCache();
 127                  $data  = $cache->getAll();
 128  
 129                  if ($data && \count($data) > 0) {
 130                      // Process filter by search term.
 131                      if ($search = $this->getState('filter.search')) {
 132                          foreach ($data as $key => $cacheItem) {
 133                              if (stripos($cacheItem->group, $search) === false) {
 134                                  unset($data[$key]);
 135                              }
 136                          }
 137                      }
 138  
 139                      // Process ordering.
 140                      $listOrder = $this->getState('list.ordering', 'group');
 141                      $listDirn  = $this->getState('list.direction', 'ASC');
 142  
 143                      $this->_data = ArrayHelper::sortObjects($data, $listOrder, strtolower($listDirn) === 'desc' ? -1 : 1, true, true);
 144  
 145                      // Process pagination.
 146                      $limit = (int) $this->getState('list.limit', 25);
 147  
 148                      if ($limit !== 0) {
 149                          $start = (int) $this->getState('list.start', 0);
 150  
 151                          return \array_slice($this->_data, $start, $limit);
 152                      }
 153                  } else {
 154                      $this->_data = array();
 155                  }
 156              } catch (CacheConnectingException $exception) {
 157                  $this->setError(Text::_('COM_CACHE_ERROR_CACHE_CONNECTION_FAILED'));
 158                  $this->_data = array();
 159              } catch (UnsupportedCacheException $exception) {
 160                  $this->setError(Text::_('COM_CACHE_ERROR_CACHE_DRIVER_UNSUPPORTED'));
 161                  $this->_data = array();
 162              }
 163          }
 164  
 165          return $this->_data;
 166      }
 167  
 168      /**
 169       * Method to get cache instance.
 170       *
 171       * @return CacheController
 172       */
 173      public function getCache()
 174      {
 175          $app = Factory::getApplication();
 176  
 177          $options = array(
 178              'defaultgroup' => '',
 179              'storage'      => $app->get('cache_handler', ''),
 180              'caching'      => true,
 181              'cachebase'    => $app->get('cache_path', JPATH_CACHE)
 182          );
 183  
 184          return Cache::getInstance('', $options);
 185      }
 186  
 187      /**
 188       * Get the number of current Cache Groups.
 189       *
 190       * @return  integer
 191       */
 192      public function getTotal()
 193      {
 194          if (empty($this->_total)) {
 195              $this->_total = count($this->getData());
 196          }
 197  
 198          return $this->_total;
 199      }
 200  
 201      /**
 202       * Method to get a pagination object for the cache.
 203       *
 204       * @return  Pagination
 205       */
 206      public function getPagination()
 207      {
 208          if (empty($this->_pagination)) {
 209              $this->_pagination = new Pagination($this->getTotal(), $this->getState('list.start'), $this->getState('list.limit'));
 210          }
 211  
 212          return $this->_pagination;
 213      }
 214  
 215      /**
 216       * Clean out a cache group as named by param.
 217       * If no param is passed clean all cache groups.
 218       *
 219       * @param   string  $group  Cache group name.
 220       *
 221       * @return  boolean  True on success, false otherwise
 222       */
 223      public function clean($group = '')
 224      {
 225          try {
 226              $this->getCache()->clean($group);
 227          } catch (CacheConnectingException $exception) {
 228              return false;
 229          } catch (UnsupportedCacheException $exception) {
 230              return false;
 231          }
 232  
 233          Factory::getApplication()->triggerEvent('onAfterPurge', array($group));
 234  
 235          return true;
 236      }
 237  
 238      /**
 239       * Purge an array of cache groups.
 240       *
 241       * @param   array  $array  Array of cache group names.
 242       *
 243       * @return  array  Array with errors, if they exist.
 244       */
 245      public function cleanlist($array)
 246      {
 247          $errors = array();
 248  
 249          foreach ($array as $group) {
 250              if (!$this->clean($group)) {
 251                  $errors[] = $group;
 252              }
 253          }
 254  
 255          return $errors;
 256      }
 257  
 258      /**
 259       * Purge all cache items.
 260       *
 261       * @return  boolean  True if successful; false otherwise.
 262       */
 263      public function purge()
 264      {
 265          try {
 266              Factory::getCache('')->gc();
 267          } catch (CacheConnectingException $exception) {
 268              return false;
 269          } catch (UnsupportedCacheException $exception) {
 270              return false;
 271          }
 272  
 273          Factory::getApplication()->triggerEvent('onAfterPurge', array());
 274  
 275          return true;
 276      }
 277  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer