[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Cache/Controller/ -> PageController.php (source)

   1  <?php
   2  /**
   3   * Joomla! Content Management System
   4   *
   5   * @copyright  (C) 2007 Open Source Matters, Inc. <https://www.joomla.org>
   6   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   7   */
   8  
   9  namespace Joomla\CMS\Cache\Controller;
  10  
  11  \defined('JPATH_PLATFORM') or die;
  12  
  13  use Joomla\CMS\Cache\Cache;
  14  use Joomla\CMS\Cache\CacheController;
  15  use Joomla\CMS\Factory;
  16  
  17  /**
  18   * Joomla! Cache page type object
  19   *
  20   * @since  1.7.0
  21   */
  22  class PageController extends CacheController
  23  {
  24      /**
  25       * ID property for the cache page object.
  26       *
  27       * @var    integer
  28       * @since  1.7.0
  29       */
  30      protected $_id;
  31  
  32      /**
  33       * Cache group
  34       *
  35       * @var    string
  36       * @since  1.7.0
  37       */
  38      protected $_group;
  39  
  40      /**
  41       * Cache lock test
  42       *
  43       * @var    \stdClass
  44       * @since  1.7.0
  45       */
  46      protected $_locktest = null;
  47  
  48      /**
  49       * Get the cached page data
  50       *
  51       * @param   boolean  $id     The cache data ID
  52       * @param   string   $group  The cache data group
  53       *
  54       * @return  mixed  Boolean false on no result, cached object otherwise
  55       *
  56       * @since   1.7.0
  57       */
  58  	public function get($id = false, $group = 'page')
  59      {
  60          // If an id is not given, generate it from the request
  61          if (!$id)
  62          {
  63              $id = $this->_makeId();
  64          }
  65  
  66          // If the etag matches the page id ... set a no change header and exit : utilize browser cache
  67          if (!headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH']))
  68          {
  69              $etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  70  
  71              if ($etag == $id)
  72              {
  73                  $browserCache = $this->options['browsercache'] ?? false;
  74  
  75                  if ($browserCache)
  76                  {
  77                      $this->_noChange();
  78                  }
  79              }
  80          }
  81  
  82          // We got a cache hit... set the etag header and echo the page data
  83          $data = $this->cache->get($id, $group);
  84  
  85          $this->_locktest = (object) array('locked' => null, 'locklooped' => null);
  86  
  87          if ($data === false)
  88          {
  89              $this->_locktest = $this->cache->lock($id, $group);
  90  
  91              // If locklooped is true try to get the cached data again; it could exist now.
  92              if ($this->_locktest->locked === true && $this->_locktest->locklooped === true)
  93              {
  94                  $data = $this->cache->get($id, $group);
  95              }
  96          }
  97  
  98          if ($data !== false)
  99          {
 100              if ($this->_locktest->locked === true)
 101              {
 102                  $this->cache->unlock($id, $group);
 103              }
 104  
 105              $data = unserialize(trim($data));
 106              $data = Cache::getWorkarounds($data);
 107  
 108              $this->_setEtag($id);
 109  
 110              return $data;
 111          }
 112  
 113          // Set ID and group placeholders
 114          $this->_id    = $id;
 115          $this->_group = $group;
 116  
 117          return false;
 118      }
 119  
 120      /**
 121       * Stop the cache buffer and store the cached data
 122       *
 123       * @param   mixed    $data        The data to store
 124       * @param   string   $id          The cache data ID
 125       * @param   string   $group       The cache data group
 126       * @param   boolean  $wrkarounds  True to use workarounds
 127       *
 128       * @return  boolean
 129       *
 130       * @since   1.7.0
 131       */
 132  	public function store($data, $id, $group = null, $wrkarounds = true)
 133      {
 134          if ($this->_locktest->locked === false && $this->_locktest->locklooped === true)
 135          {
 136              // We can not store data because another process is in the middle of saving
 137              return false;
 138          }
 139  
 140          // Get page data from the application object
 141          if (!$data)
 142          {
 143              $data = Factory::getApplication()->getBody();
 144  
 145              // Only attempt to store if page data exists.
 146              if (!$data)
 147              {
 148                  return false;
 149              }
 150          }
 151  
 152          // Get id and group and reset the placeholders
 153          if (!$id)
 154          {
 155              $id = $this->_id;
 156          }
 157  
 158          if (!$group)
 159          {
 160              $group = $this->_group;
 161          }
 162  
 163          if ($wrkarounds)
 164          {
 165              $data = Cache::setWorkarounds(
 166                  $data,
 167                  array(
 168                      'nopathway' => 1,
 169                      'nohead'    => 1,
 170                      'nomodules' => 1,
 171                      'headers'   => true,
 172                  )
 173              );
 174          }
 175  
 176          $result = $this->cache->store(serialize($data), $id, $group);
 177  
 178          if ($this->_locktest->locked === true)
 179          {
 180              $this->cache->unlock($id, $group);
 181          }
 182  
 183          return $result;
 184      }
 185  
 186      /**
 187       * Generate a page cache id
 188       *
 189       * @return  string  MD5 Hash
 190       *
 191       * @since   1.7.0
 192       * @todo    Discuss whether this should be coupled to a data hash or a request hash ... perhaps hashed with a serialized request
 193       */
 194  	protected function _makeId()
 195      {
 196          return Cache::makeId();
 197      }
 198  
 199      /**
 200       * There is no change in page data so send an unmodified header and die gracefully
 201       *
 202       * @return  void
 203       *
 204       * @since   1.7.0
 205       */
 206  	protected function _noChange()
 207      {
 208          $app = Factory::getApplication();
 209  
 210          // Send not modified header and exit gracefully
 211          $app->setHeader('Status', 304, true);
 212          $app->sendHeaders();
 213          $app->close();
 214      }
 215  
 216      /**
 217       * Set the ETag header in the response
 218       *
 219       * @param   string  $etag  The entity tag (etag) to set
 220       *
 221       * @return  void
 222       *
 223       * @since   1.7.0
 224       */
 225  	protected function _setEtag($etag)
 226      {
 227          Factory::getApplication()->setHeader('ETag', '"' . $etag . '"', true);
 228      }
 229  }


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