PageCache.php 3.47 KB
Newer Older
resurtm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

use Yii;
use yii\base\ActionFilter;
use yii\base\Action;
use yii\base\View;
use yii\caching\Dependency;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class PageCache extends ActionFilter
{
	/**
	 * @var boolean whether the content being cached should be differentiated according to the route.
	 * A route consists of the requested controller ID and action ID. Defaults to true.
	 */
	public $varyByRoute = true;
	/**
	 * @var string the application component ID of the [[\yii\caching\Cache|cache]] object.
	 */
	public $cache = 'cache';
	/**
	 * @var integer number of seconds that the data can remain valid in cache.
	 * Use 0 to indicate that the cached data will never expire.
	 */
	public $duration = 60;
	/**
	 * @var array|Dependency the dependency that the cached content depends on.
	 * This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
	 * For example,
	 *
	 * ~~~
	 * array(
	 *     'class' => 'yii\caching\DbDependency',
	 *     'sql' => 'SELECT MAX(lastModified) FROM Post',
	 * )
	 * ~~~
	 *
	 * would make the output cache depends on the last modified time of all posts.
	 * If any post has its modification time changed, the cached content would be invalidated.
	 */
	public $dependency;
	/**
	 * @var array list of factors that would cause the variation of the content being cached.
	 * Each factor is a string representing a variation (e.g. the language, a GET parameter).
	 * The following variation setting will cause the content to be cached in different versions
	 * according to the current application language:
	 *
	 * ~~~
	 * array(
	 *     Yii::$app->language,
	 * )
	 */
	public $variations;
	/**
	 * @var boolean whether to enable the fragment cache. You may use this property to turn on and off
	 * the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
	 */
	public $enabled = true;
69 70 71 72 73
	/**
	 * @var View the view component to use for caching. If not set, the default application view component
	 * [[Application::view]] will be used.
	 */
	public $view;
resurtm committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96


	public function init()
	{
		parent::init();
		if ($this->view === null) {
			$this->view = Yii::$app->getView();
		}
	}

	/**
	 * This method is invoked right before an action is to be executed (after all possible filters.)
	 * You may override this method to do last-minute preparation for the action.
	 * @param Action $action the action to be executed.
	 * @return boolean whether the action should continue to be executed.
	 */
	public function beforeAction($action)
	{
		$properties = array();
		foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) {
			$properties[$name] = $this->$name;
		}
		$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__;
97 98 99 100 101 102 103 104
		ob_start();
		ob_implicit_flush(false);
		if ($this->view->beginCache($id, $properties)) {
			return true;
		} else {
			Yii::$app->getResponse()->content = ob_get_clean();
			return false;
		}
resurtm committed
105 106 107 108 109 110
	}

	/**
	 * This method is invoked right after an action is executed.
	 * You may override this method to do some postprocessing for the action.
	 * @param Action $action the action just executed.
111
	 * @param mixed $result the action execution result
resurtm committed
112
	 */
113
	public function afterAction($action, &$result)
resurtm committed
114
	{
115
		echo $result;
resurtm committed
116
		$this->view->endCache();
117
		$result = ob_get_clean();
resurtm committed
118
	}
Zander Baldwin committed
119
}