Fragment.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Kohana
  14. * @category Helpers
  15. * @author Kohana Team
  16. * @copyright (c) Kohana Team
  17. * @license https://koseven.ga/LICENSE.md
  18. * @uses Kohana::cache
  19. */
  20. class Kohana_Fragment {
  21. /**
  22. * @var integer default number of seconds to cache for
  23. */
  24. public static $lifetime = 30;
  25. /**
  26. * @var boolean use multilingual fragment support?
  27. */
  28. public static $i18n = FALSE;
  29. /**
  30. * @var array list of buffer => cache key
  31. */
  32. protected static $_caches = [];
  33. /**
  34. * Generate the cache key name for a fragment.
  35. *
  36. * $key = Fragment::_cache_key('footer', TRUE);
  37. *
  38. * @param string $name fragment name
  39. * @param boolean $i18n multilingual fragment support
  40. * @return string
  41. * @uses I18n::lang
  42. * @since 3.0.4
  43. */
  44. protected static function _cache_key($name, $i18n = NULL)
  45. {
  46. if ($i18n === NULL)
  47. {
  48. // Use the default setting
  49. $i18n = Fragment::$i18n;
  50. }
  51. // Language prefix for cache key
  52. $i18n = ($i18n === TRUE) ? I18n::lang() : '';
  53. // Note: $i18n and $name need to be delimited to prevent naming collisions
  54. return 'Fragment::cache('.$i18n.'+'.$name.')';
  55. }
  56. /**
  57. * Load a fragment from cache and display it. Multiple fragments can
  58. * be nested with different life times.
  59. *
  60. * if ( ! Fragment::load('footer')) {
  61. * // Anything that is echo'ed here will be saved
  62. * Fragment::save();
  63. * }
  64. *
  65. * @param string $name fragment name
  66. * @param integer $lifetime fragment cache lifetime
  67. * @param boolean $i18n multilingual fragment support
  68. * @return boolean
  69. */
  70. public static function load($name, $lifetime = NULL, $i18n = NULL)
  71. {
  72. // Set the cache lifetime
  73. $lifetime = ($lifetime === NULL) ? Fragment::$lifetime : (int) $lifetime;
  74. // Get the cache key name
  75. $cache_key = Fragment::_cache_key($name, $i18n);
  76. if ($fragment = Kohana::cache($cache_key, NULL, $lifetime))
  77. {
  78. // Display the cached fragment now
  79. echo $fragment;
  80. return TRUE;
  81. }
  82. else
  83. {
  84. // Start the output buffer
  85. ob_start();
  86. // Store the cache key by the buffer level
  87. Fragment::$_caches[ob_get_level()] = $cache_key;
  88. return FALSE;
  89. }
  90. }
  91. /**
  92. * Saves the currently open fragment in the cache.
  93. *
  94. * Fragment::save();
  95. *
  96. * @return void
  97. */
  98. public static function save()
  99. {
  100. // Get the buffer level
  101. $level = ob_get_level();
  102. if (isset(Fragment::$_caches[$level]))
  103. {
  104. // Get the cache key based on the level
  105. $cache_key = Fragment::$_caches[$level];
  106. // Delete the cache key, we don't need it anymore
  107. unset(Fragment::$_caches[$level]);
  108. // Get the output buffer and display it at the same time
  109. $fragment = ob_get_flush();
  110. // Cache the fragment
  111. Kohana::cache($cache_key, $fragment);
  112. }
  113. }
  114. /**
  115. * Delete a cached fragment.
  116. *
  117. * Fragment::delete($key);
  118. *
  119. * @param string $name fragment name
  120. * @param boolean $i18n multilingual fragment support
  121. * @return void
  122. */
  123. public static function delete($name, $i18n = NULL)
  124. {
  125. // Invalid the cache
  126. Kohana::cache(Fragment::_cache_key($name, $i18n), NULL, -3600);
  127. }
  128. }