PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/reechalee/joomla1.6
PHP | 121 lines | 57 code | 17 blank | 47 comment | 18 complexity | 018fa5c3bf2517c39adf5d7942b546c9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. * @version $Id: view.php 18013 2010-07-03 03:42:31Z infograf768 $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // No direct access
  9. defined('JPATH_BASE') or die;
  10. /**
  11. * Joomla! Cache view type object
  12. *
  13. * @package Joomla.Framework
  14. * @subpackage Cache
  15. * @since 1.6
  16. */
  17. class JCacheControllerView extends JCacheController
  18. {
  19. /**
  20. * Get the cached view data
  21. *
  22. * @param object $view The view object to cache output for
  23. * @param string $method The method name of the view method to cache output for
  24. * @param string $group The cache data group
  25. * @param string $id The cache data id
  26. * @return boolean True if the cache is hit (false else)
  27. * @since 1.6
  28. */
  29. public function get(&$view, $method, $id=false, $wrkarounds=true)
  30. {
  31. // If an id is not given generate it from the request
  32. if ($id == false) {
  33. $id = $this->_makeId($view, $method);
  34. }
  35. $data = false;
  36. $data = $this->cache->get($id);
  37. $locktest = new stdClass;
  38. $locktest->locked = null;
  39. $locktest->locklooped = null;
  40. if ($data === false) {
  41. $locktest = $this->cache->lock($id,null);
  42. // if the loop is completed and returned true the means the lock has been set
  43. // if looped is true try to get the cached data again; it could exist now
  44. if ($locktest->locked == true && $locktest->locklooped == true) {
  45. $data = $this->cache->get($id);
  46. }
  47. // false means that locking is either turned off or maxtime has been exceeeded, execute the view
  48. }
  49. if ($data !== false) {
  50. $data = unserialize(trim($data));
  51. if ($wrkarounds === true) {
  52. echo JCache::getWorkarounds($data);
  53. }
  54. else { // no workarounds, all data is stored in one piece
  55. echo (isset($data)) ? $data : null;
  56. }
  57. if ($locktest->locked == true) {
  58. $this->cache->unlock($id);
  59. }
  60. return true;
  61. }
  62. /*
  63. * No hit so we have to execute the view
  64. */
  65. if (method_exists($view, $method)) {
  66. // if previous lock failed try again
  67. if ($locktest->locked == false) {
  68. $locktest = $this->cache->lock($id);
  69. }
  70. // Capture and echo output
  71. ob_start();
  72. ob_implicit_flush(false);
  73. $view->$method();
  74. $data = ob_get_contents();
  75. ob_end_clean();
  76. echo $data;
  77. /*
  78. * For a view we have a special case. We need to cache not only the output from the view, but the state
  79. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  80. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  81. */
  82. $cached = array();
  83. $cached = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
  84. // Store the cache data
  85. $this->cache->store(serialize($cached), $id);
  86. if ($locktest->locked == true) {
  87. $this->cache->unlock($id);
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * Generate a view cache id.
  94. *
  95. * @param object $view The view object to cache output for
  96. * @param string $method The method name to cache for the view object
  97. * @return string MD5 Hash : view cache id
  98. * @since 1.6
  99. */
  100. private function _makeId(&$view, $method)
  101. {
  102. return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
  103. }
  104. }