Added Route::$cache flag, fixes #3212. Includes tests.
[kohana-core.git] / classes / kohana / fragment.php
blob70ba7763d833d22fcda6a1eb9c2673bd04f6af3e
1 <?php defined('SYSPATH') or die('No direct script access.');
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.
8 * For obvious reasons, fragment caching should not be applied to any
9 * content that contains forms.
11 * [!!] Multiple language (I18n) support was added in v3.0.4.
13 * @package Kohana
14 * @category Helpers
15 * @author Kohana Team
16 * @copyright (c) 2009-2010 Kohana Team
17 * @license http://kohanaframework.org/license
18 * @uses Kohana::cache
20 class Kohana_Fragment {
22 /**
23 * @var integer default number of seconds to cache for
25 public static $lifetime = 30;
27 /**
28 * @var boolean use multilingual fragment support?
30 public static $i18n = FALSE;
32 // List of buffer => cache key
33 protected static $_caches = array();
35 /**
36 * Generate the cache key name for a fragment.
38 * $key = Fragment::_cache_key('footer', TRUE);
40 * @param string fragment name
41 * @param boolean multilingual fragment support
42 * @return string
43 * @uses I18n::lang
44 * @since 3.0.4
46 protected static function _cache_key($name, $i18n = NULL)
48 if ($i18n === NULL)
50 // Use the default setting
51 $i18n = Fragment::$i18n;
54 // Language prefix for cache key
55 $i18n = ($i18n === TRUE) ? I18n::lang() : '';
57 // Note: $i18n and $name need to be delimited to prevent naming collisions
58 return 'Fragment::cache('.$i18n.'+'.$name.')';
61 /**
62 * Load a fragment from cache and display it. Multiple fragments can
63 * be nested with different life times.
65 * if ( ! Fragment::load('footer')) {
66 * // Anything that is echo'ed here will be saved
67 * Fragment::save();
68 * }
70 * @param string fragment name
71 * @param integer fragment cache lifetime
72 * @param boolean multilingual fragment support
73 * @return boolean
75 public static function load($name, $lifetime = NULL, $i18n = NULL)
77 // Set the cache lifetime
78 $lifetime = ($lifetime === NULL) ? Fragment::$lifetime : (int) $lifetime;
80 // Get the cache key name
81 $cache_key = Fragment::_cache_key($name, $i18n);
83 if ($fragment = Kohana::cache($cache_key, NULL, $lifetime))
85 // Display the cached fragment now
86 echo $fragment;
88 return TRUE;
90 else
92 // Start the output buffer
93 ob_start();
95 // Store the cache key by the buffer level
96 Fragment::$_caches[ob_get_level()] = $cache_key;
98 return FALSE;
103 * Saves the currently open fragment in the cache.
105 * Fragment::save();
107 * @return void
109 public static function save()
111 // Get the buffer level
112 $level = ob_get_level();
114 if (isset(Fragment::$_caches[$level]))
116 // Get the cache key based on the level
117 $cache_key = Fragment::$_caches[$level];
119 // Delete the cache key, we don't need it anymore
120 unset(Fragment::$_caches[$level]);
122 // Get the output buffer and display it at the same time
123 $fragment = ob_get_flush();
125 // Cache the fragment
126 Kohana::cache($cache_key, $fragment);
131 * Delete a cached fragment.
133 * Fragment::delete($key);
135 * @param string fragment name
136 * @param boolean multilingual fragment support
137 * @return void
139 public static function delete($name, $i18n = NULL)
141 // Invalid the cache
142 Kohana::cache(Fragment::_cache_key($name, $i18n), NULL, -3600);
145 } // End Fragment