[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/cache/handler/ -> view.php (source)

   1  <?php
   2  /**
   3  * @version        $Id: view.php 14401 2010-01-26 14:10:00Z louis $
   4  * @package        Joomla.Framework
   5  * @subpackage    Cache
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Joomla! Cache view type object
  20   *
  21   * @package        Joomla.Framework
  22   * @subpackage    Cache
  23   * @since        1.5
  24   */
  25  class JCacheView extends JCache
  26  {
  27      /**
  28       * Get the cached view data
  29       *
  30       * @access    public
  31       * @param    object    $view    The view object to cache output for
  32       * @param    string    $method    The method name of the view method to cache output for
  33       * @param    string    $group    The cache data group
  34       * @param    string    $id        The cache data id
  35       * @return    boolean    True if the cache is hit (false else)
  36       * @since    1.5
  37       */
  38  	function get( &$view, $method, $id=false )
  39      {
  40          global $mainframe;
  41  
  42          // Initialize variables
  43          $data = false;
  44  
  45          // If an id is not given generate it from the request
  46          if ($id == false) {
  47              $id = $this->_makeId($view, $method);
  48          }
  49  
  50          $data = parent::get($id);
  51          if ($data !== false) {
  52              $data        = unserialize($data);
  53              $document    = &JFactory::getDocument();
  54  
  55              // Get the document head out of the cache.
  56              $document->setHeadData((isset($data['head'])) ? $data['head'] : array());
  57  
  58              // If the pathway buffer is set in the cache data, get it.
  59              if (isset($data['pathway']) && is_array($data['pathway']))
  60              {
  61                  // Push the pathway data into the pathway object.
  62                  $pathway = &$mainframe->getPathWay();
  63                  $pathway->setPathway($data['pathway']);
  64              }
  65  
  66              // If a module buffer is set in the cache data, get it.
  67              if (isset($data['module']) && is_array($data['module']))
  68              {
  69                  // Iterate through the module positions and push them into the document buffer.
  70                  foreach ($data['module'] as $name => $contents) {
  71                      $document->setBuffer($contents, 'module', $name);
  72                  }
  73              }
  74  
  75              // Get the document body out of the cache.
  76              echo (isset($data['body'])) ? $data['body'] : null;
  77              return true;
  78          }
  79  
  80          /*
  81           * No hit so we have to execute the view
  82           */
  83          if (method_exists($view, $method))
  84          {
  85              $document = &JFactory::getDocument();
  86  
  87              // Get the modules buffer before component execution.
  88              $buffer1 = $document->getBuffer();
  89  
  90              // Make sure the module buffer is an array.
  91              if (!isset($buffer1['module']) || !is_array($buffer1['module'])) {
  92                  $buffer1['module'] = array();
  93              }
  94  
  95              // Capture and echo output
  96              ob_start();
  97              ob_implicit_flush( false );
  98              $view->$method();
  99              $data = ob_get_contents();
 100              ob_end_clean();
 101              echo $data;
 102  
 103              /*
 104               * For a view we have a special case.  We need to cache not only the output from the view, but the state
 105               * of the document head after the view has been rendered.  This will allow us to properly cache any attached
 106               * scripts or stylesheets or links or any other modifications that the view has made to the document object
 107               */
 108              $cached = array();
 109  
 110              // View body data
 111              $cached['body'] = $data;
 112  
 113              // Document head data
 114              $cached['head'] = $document->getHeadData();
 115  
 116              // Pathway data
 117              $pathway            = &$mainframe->getPathWay();
 118              $cached['pathway']    = $pathway->getPathway();
 119  
 120              // Get the module buffer after component execution.
 121              $buffer2 = $document->getBuffer();
 122  
 123              // Make sure the module buffer is an array.
 124              if (!isset($buffer2['module']) || !is_array($buffer2['module'])) {
 125                  $buffer2['module'] = array();
 126              }
 127  
 128              // Compare the second module buffer against the first buffer.
 129              $cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
 130  
 131              // Store the cache data
 132              $this->store(serialize($cached), $id);
 133          }
 134          return false;
 135      }
 136  
 137      /**
 138       * Generate a view cache id
 139       *
 140       * @access    private
 141       * @param    object    $view    The view object to cache output for
 142       * @param    string    $method    The method name to cache for the view object
 143       * @return    string    MD5 Hash : view cache id
 144       * @since    1.5
 145       */
 146  	function _makeId(&$view, $method)
 147      {
 148          return md5(serialize(array(JRequest::getURI(), get_class($view), $method)));
 149      }
 150  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1