Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.3 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
12 years ago
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
12 years ago
use yii\caching\Cache;
use yii\caching\Dependency;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FragmentCache extends Widget
{
/**
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
*/
public $cacheID = 'cache';
/**
12 years ago
* @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;
/**
12 years ago
* @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(
12 years ago
* 'class' => 'yii\caching\DbDependency',
* 'sql' => 'SELECT MAX(lastModified) FROM Post',
* )
12 years ago
* ~~~
*
* 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;
/**
12 years ago
* @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,
* )
*/
12 years ago
public $variations;
/**
12 years ago
* @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).
*/
12 years ago
public $enabled = true;
/**
* @var \yii\base\View the view object within which this widget is sued. If not set,
* the view registered with the application will be used. This is mainly used by dynamic content feature.
*/
public $view;
/**
* @var array a list of placeholders for embedding dynamic contents. This property
* is used internally to implement the content caching feature. Do not modify it.
*/
public $dynamicPlaceholders;
/**
* Marks the start of content to be cached.
* Content displayed after this method call and before {@link endCache()}
* will be captured and saved in cache.
* This method does nothing if valid content is already found in cache.
*/
public function init()
{
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
if ($this->getCache() !== null && $this->getCachedContent() === false) {
$this->view->cacheStack[] = $this;
ob_start();
ob_implicit_flush(false);
}
}
/**
* Marks the end of content to be cached.
* Content displayed before this method call and after {@link init()}
* will be captured and saved in cache.
* This method does nothing if valid content is already found in cache.
*/
public function run()
{
12 years ago
if (($content = $this->getCachedContent()) !== false) {
echo $content;
} elseif (($cache = $this->getCache()) !== null) {
12 years ago
$content = ob_get_clean();
array_pop($this->view->cacheStack);
if (is_array($this->dependency)) {
12 years ago
$this->dependency = Yii::createObject($this->dependency);
}
$data = array($content, $this->dynamicPlaceholders);
$cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) {
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
}
12 years ago
echo $content;
}
}
/**
12 years ago
* @var string|boolean the cached content. False if the content is not cached.
*/
12 years ago
private $_content;
/**
12 years ago
* Returns the cached content if available.
* @return string|boolean the cached content. False is returned if valid content is not found in the cache.
*/
12 years ago
public function getCachedContent()
{
12 years ago
if ($this->_content === null) {
$this->_content = false;
12 years ago
if (($cache = $this->getCache()) !== null) {
$key = $this->calculateKey();
$data = $cache->get($key);
if (is_array($data) && count($data) === 2) {
list ($content, $placeholders) = $data;
if (is_array($placeholders) && count($placeholders) > 0) {
if ($this->view->cacheStack === array()) {
// outermost cache: replace placeholder with dynamic content
$content = $this->updateDynamicContent($content, $placeholders);
}
foreach ($placeholders as $name => $statements) {
$this->view->addDynamicPlaceholder($name, $statements);
}
}
$this->_content = $content;
}
}
}
12 years ago
return $this->_content;
}
protected function updateDynamicContent($content, $placeholders)
{
foreach ($placeholders as $name => $statements) {
$placeholders[$name] = $this->view->evaluateDynamicContent($statements);
}
return strtr($content, $placeholders);
}
/**
12 years ago
* Generates a unique key used for storing the content in cache.
* The key generated depends on both [[id]] and [[variations]].
* @return string a valid cache key
*/
12 years ago
protected function calculateKey()
{
12 years ago
$factors = array(__CLASS__, $this->getId());
if (is_array($this->variations)) {
foreach ($this->variations as $factor) {
$factors[] = $factor;
}
}
return $this->getCache()->buildKey($factors);
}
/**
12 years ago
* @var Cache
*/
12 years ago
private $_cache;
/**
12 years ago
* Returns the cache instance used for storing content.
* @return Cache the cache instance. Null is returned if the cache component is not available
* or [[enabled]] is false.
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component.
*/
12 years ago
public function getCache()
{
12 years ago
if (!$this->enabled) {
return null;
}
if ($this->_cache === null) {
$cache = Yii::$app->getComponent($this->cacheID);
if ($cache instanceof Cache) {
$this->_cache = $cache;
} else {
throw new InvalidConfigException('FragmentCache::cacheID must refer to the ID of a cache application component.');
}
}
12 years ago
return $this->_cache;
}
/**
12 years ago
* Sets the cache instance used by the session component.
* @param Cache $value the cache instance
*/
12 years ago
public function setCache($value)
{
12 years ago
$this->_cache = $value;
}
}