PageRenderTime 93ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/libraries/joomla/cache/controller/view.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 137 lines | 67 code | 17 blank | 53 comment | 17 complexity | a23ef3c397af5f0b7ab1ac41a036d65a MD5 | raw file
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  Cache
  5 *
  6 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE
  8 */
  9
 10defined('JPATH_PLATFORM') or die;
 11
 12/**
 13 * Joomla! Cache view type object
 14 *
 15 * @package     Joomla.Platform
 16 * @subpackage  Cache
 17 * @since       11.1
 18 */
 19class JCacheControllerView extends JCacheController
 20{
 21	/**
 22	 * Get the cached view data
 23	 *
 24	 * @param   object   &$view       The view object to cache output for
 25	 * @param   string   $method      The method name of the view method to cache output for
 26	 * @param   string   $id          The cache data id
 27	 * @param   boolean  $wrkarounds  True to enable workarounds.
 28	 *
 29	 * @return  boolean  True if the cache is hit (false else)
 30	 *
 31	 * @since   11.1
 32	 */
 33	public function get(&$view, $method, $id = false, $wrkarounds = true)
 34	{
 35		// If an id is not given generate it from the request
 36		if ($id == false)
 37		{
 38			$id = $this->_makeId($view, $method);
 39		}
 40
 41		$data = false;
 42		$data = $this->cache->get($id);
 43
 44		$locktest = new stdClass;
 45		$locktest->locked = null;
 46		$locktest->locklooped = null;
 47
 48		if ($data === false)
 49		{
 50			$locktest = $this->cache->lock($id, null);
 51			// If the loop is completed and returned true it means the lock has been set
 52			// If looped is true try to get the cached data again; it could exist now
 53			if ($locktest->locked == true && $locktest->locklooped == true)
 54			{
 55				$data = $this->cache->get($id);
 56			}
 57
 58			// False means that locking is either turned off or maxtime has been exceeded.
 59			// Execute the view.
 60		}
 61
 62		if ($data !== false)
 63		{
 64			$data = unserialize(trim($data));
 65
 66			if ($wrkarounds === true)
 67			{
 68				echo JCache::getWorkarounds($data);
 69			}
 70			else
 71			{
 72				// No workarounds, so all data is stored in one piece
 73				echo (isset($data)) ? $data : null;
 74			}
 75
 76			if ($locktest->locked == true)
 77			{
 78				$this->cache->unlock($id);
 79			}
 80
 81			return true;
 82		}
 83
 84		/*
 85		 * No hit so we have to execute the view
 86		 */
 87		if (method_exists($view, $method))
 88		{
 89			// If previous lock failed try again
 90			if ($locktest->locked == false)
 91			{
 92				$locktest = $this->cache->lock($id);
 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			$cached = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
111
112			// Store the cache data
113			$this->cache->store(serialize($cached), $id);
114
115			if ($locktest->locked == true)
116			{
117				$this->cache->unlock($id);
118			}
119		}
120		return false;
121	}
122
123	/**
124	 * Generate a view cache id.
125	 *
126	 * @param   object  &$view   The view object to cache output for
127	 * @param   string  $method  The method name to cache for the view object
128	 *
129	 * @return  string  MD5 Hash : view cache id
130	 *
131	 * @since   11.1
132	 */
133	protected function _makeId(&$view, $method)
134	{
135		return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
136	}
137}