Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
view.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Cache
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('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  */
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 = 'display' , $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 = $this->cache->get($id);
42 
43  $locktest = new stdClass;
44  $locktest->locked = null;
45  $locktest->locklooped = null;
46 
47  if ($data === false)
48  {
49  $locktest = $this->cache->lock($id, null);
50 
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 = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
109 
110  // Store the cache data
111  $this->cache->store(serialize($cached), $id);
112 
113  if ($locktest->locked == true)
114  {
115  $this->cache->unlock($id);
116  }
117  }
118  return false;
119  }
120 
121  /**
122  * Generate a view cache id.
123  *
124  * @param object &$view The view object to cache output for
125  * @param string $method The method name to cache for the view object
126  *
127  * @return string MD5 Hash : view cache id
128  *
129  * @since 11.1
130  */
131  protected function _makeId(&$view, $method)
132  {
133  return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
134  }
135 }