Fragment.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * View fragment caching. This is primarily used to cache small parts of a view
  4. * that rarely change. For instance, you may want to cache the footer of your
  5. * template because it has very little dynamic content. Or you could cache a
  6. * user profile page and delete the fragment when the user updates.
  7. *
  8. * For obvious reasons, fragment caching should not be applied to any
  9. * content that contains forms.
  10. *
  11. * [!!] Multiple language (I18n) support was added in v3.0.4.
  12. *
  13. * @package KO7
  14. * @category Helpers
  15. *
  16. * @copyright (c) 2007-2016 Kohana Team
  17. * @copyright (c) since 2016 Koseven Team
  18. * @license https://koseven.dev/LICENSE
  19. * @uses KO7::cache
  20. */
  21. class KO7_Fragment {
  22. /**
  23. * @var integer default number of seconds to cache for
  24. */
  25. public static $lifetime = 30;
  26. /**
  27. * @var boolean use multilingual fragment support?
  28. */
  29. public static $i18n = FALSE;
  30. /**
  31. * @var array list of buffer => cache key
  32. */
  33. protected static $_caches = [];
  34. /**
  35. * Generate the cache key name for a fragment.
  36. *
  37. * $key = Fragment::_cache_key('footer', TRUE);
  38. *
  39. * @param string $name fragment name
  40. * @param boolean $i18n multilingual fragment support
  41. * @return string
  42. * @uses I18n::lang
  43. * @since 3.0.4
  44. */
  45. protected static function _cache_key($name, $i18n = NULL)
  46. {
  47. if ($i18n === NULL)
  48. {
  49. // Use the default setting
  50. $i18n = Fragment::$i18n;
  51. }
  52. // Language prefix for cache key
  53. $i18n = ($i18n === TRUE) ? I18n::lang() : '';
  54. // Note: $i18n and $name need to be delimited to prevent naming collisions
  55. return 'Fragment::cache('.$i18n.'+'.$name.')';
  56. }
  57. /**
  58. * Load a fragment from cache and display it. Multiple fragments can
  59. * be nested with different life times.
  60. *
  61. * if ( ! Fragment::load('footer')) {
  62. * // Anything that is echo'ed here will be saved
  63. * Fragment::save();
  64. * }
  65. *
  66. * @param string $name fragment name
  67. * @param integer $lifetime fragment cache lifetime
  68. * @param boolean $i18n multilingual fragment support
  69. * @return boolean
  70. */
  71. public static function load($name, $lifetime = NULL, $i18n = NULL)
  72. {
  73. // Set the cache lifetime
  74. $lifetime = ($lifetime === NULL) ? Fragment::$lifetime : (int) $lifetime;
  75. // Get the cache key name
  76. $cache_key = Fragment::_cache_key($name, $i18n);
  77. if ($fragment = KO7::cache($cache_key, NULL, $lifetime))
  78. {
  79. // Display the cached fragment now
  80. echo $fragment;
  81. return TRUE;
  82. }
  83. else
  84. {
  85. // Start the output buffer
  86. ob_start();
  87. // Store the cache key by the buffer level
  88. Fragment::$_caches[ob_get_level()] = $cache_key;
  89. return FALSE;
  90. }
  91. }
  92. /**
  93. * Saves the currently open fragment in the cache.
  94. *
  95. * Fragment::save();
  96. *
  97. * @return void
  98. */
  99. public static function save()
  100. {
  101. // Get the buffer level
  102. $level = ob_get_level();
  103. if (isset(Fragment::$_caches[$level]))
  104. {
  105. // Get the cache key based on the level
  106. $cache_key = Fragment::$_caches[$level];
  107. // Delete the cache key, we don't need it anymore
  108. unset(Fragment::$_caches[$level]);
  109. // Get the output buffer and display it at the same time
  110. $fragment = ob_get_flush();
  111. // Cache the fragment
  112. KO7::cache($cache_key, $fragment);
  113. }
  114. }
  115. /**
  116. * Delete a cached fragment.
  117. *
  118. * Fragment::delete($key);
  119. *
  120. * @param string $name fragment name
  121. * @param boolean $i18n multilingual fragment support
  122. * @return void
  123. */
  124. public static function delete($name, $i18n = NULL)
  125. {
  126. // Invalid the cache
  127. KO7::cache(Fragment::_cache_key($name, $i18n), NULL, -3600);
  128. }
  129. }