resurtm
12 years ago
57 changed files with 1611 additions and 1640 deletions
File diff suppressed because it is too large
Load Diff
@ -1,104 +1,104 @@ |
|||||||
<?php |
<?php |
||||||
/** |
/** |
||||||
* @link http://www.yiiframework.com/ |
* @link http://www.yiiframework.com/ |
||||||
* @copyright Copyright (c) 2008 Yii Software LLC |
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
* @license http://www.yiiframework.com/license/ |
* @license http://www.yiiframework.com/license/ |
||||||
*/ |
*/ |
||||||
|
|
||||||
namespace yii\web; |
namespace yii\web; |
||||||
|
|
||||||
use Yii; |
use Yii; |
||||||
use yii\base\ActionFilter; |
use yii\base\ActionFilter; |
||||||
use yii\base\Action; |
use yii\base\Action; |
||||||
use yii\base\View; |
use yii\base\View; |
||||||
use yii\caching\Dependency; |
use yii\caching\Dependency; |
||||||
|
|
||||||
/** |
/** |
||||||
* @author Qiang Xue <qiang.xue@gmail.com> |
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
* @since 2.0 |
* @since 2.0 |
||||||
*/ |
*/ |
||||||
class PageCache extends ActionFilter |
class PageCache extends ActionFilter |
||||||
{ |
{ |
||||||
/** |
/** |
||||||
* @var boolean whether the content being cached should be differentiated according to the route. |
* @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. |
* A route consists of the requested controller ID and action ID. Defaults to true. |
||||||
*/ |
*/ |
||||||
public $varyByRoute = true; |
public $varyByRoute = true; |
||||||
/** |
/** |
||||||
* @var string the application component ID of the [[\yii\caching\Cache|cache]] object. |
* @var string the application component ID of the [[\yii\caching\Cache|cache]] object. |
||||||
*/ |
*/ |
||||||
public $cache = 'cache'; |
public $cache = 'cache'; |
||||||
/** |
/** |
||||||
* @var integer number of seconds that the data can remain valid in 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. |
* Use 0 to indicate that the cached data will never expire. |
||||||
*/ |
*/ |
||||||
public $duration = 60; |
public $duration = 60; |
||||||
/** |
/** |
||||||
* @var array|Dependency the dependency that the cached content depends on. |
* @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. |
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||||
* For example, |
* For example, |
||||||
* |
* |
||||||
* ~~~ |
* ~~~ |
||||||
* array( |
* array( |
||||||
* 'class' => 'yii\caching\DbDependency', |
* 'class' => 'yii\caching\DbDependency', |
||||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||||
* ) |
* ) |
||||||
* ~~~ |
* ~~~ |
||||||
* |
* |
||||||
* would make the output cache depends on the last modified time of all posts. |
* 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. |
* If any post has its modification time changed, the cached content would be invalidated. |
||||||
*/ |
*/ |
||||||
public $dependency; |
public $dependency; |
||||||
/** |
/** |
||||||
* @var array list of factors that would cause the variation of the content being cached. |
* @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). |
* 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 |
* The following variation setting will cause the content to be cached in different versions |
||||||
* according to the current application language: |
* according to the current application language: |
||||||
* |
* |
||||||
* ~~~ |
* ~~~ |
||||||
* array( |
* array( |
||||||
* Yii::$app->language, |
* Yii::$app->language, |
||||||
* ) |
* ) |
||||||
*/ |
*/ |
||||||
public $variations; |
public $variations; |
||||||
/** |
/** |
||||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
* @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). |
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||||
*/ |
*/ |
||||||
public $enabled = true; |
public $enabled = true; |
||||||
|
|
||||||
|
|
||||||
public function init() |
public function init() |
||||||
{ |
{ |
||||||
parent::init(); |
parent::init(); |
||||||
if ($this->view === null) { |
if ($this->view === null) { |
||||||
$this->view = Yii::$app->getView(); |
$this->view = Yii::$app->getView(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* This method is invoked right before an action is to be executed (after all possible filters.) |
* 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. |
* You may override this method to do last-minute preparation for the action. |
||||||
* @param Action $action the action to be executed. |
* @param Action $action the action to be executed. |
||||||
* @return boolean whether the action should continue to be executed. |
* @return boolean whether the action should continue to be executed. |
||||||
*/ |
*/ |
||||||
public function beforeAction($action) |
public function beforeAction($action) |
||||||
{ |
{ |
||||||
$properties = array(); |
$properties = array(); |
||||||
foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) { |
foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) { |
||||||
$properties[$name] = $this->$name; |
$properties[$name] = $this->$name; |
||||||
} |
} |
||||||
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__; |
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__; |
||||||
return $this->view->beginCache($id, $properties); |
return $this->view->beginCache($id, $properties); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* This method is invoked right after an action is executed. |
* This method is invoked right after an action is executed. |
||||||
* You may override this method to do some postprocessing for the action. |
* You may override this method to do some postprocessing for the action. |
||||||
* @param Action $action the action just executed. |
* @param Action $action the action just executed. |
||||||
*/ |
*/ |
||||||
public function afterAction($action) |
public function afterAction($action) |
||||||
{ |
{ |
||||||
$this->view->endCache(); |
$this->view->endCache(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,174 +1,174 @@ |
|||||||
<?php |
<?php |
||||||
/** |
/** |
||||||
* @link http://www.yiiframework.com/ |
* @link http://www.yiiframework.com/ |
||||||
* @copyright Copyright (c) 2008 Yii Software LLC |
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
* @license http://www.yiiframework.com/license/ |
* @license http://www.yiiframework.com/license/ |
||||||
*/ |
*/ |
||||||
|
|
||||||
namespace yii\widgets; |
namespace yii\widgets; |
||||||
|
|
||||||
use Yii; |
use Yii; |
||||||
use yii\base\Widget; |
use yii\base\Widget; |
||||||
use yii\caching\Cache; |
use yii\caching\Cache; |
||||||
use yii\caching\Dependency; |
use yii\caching\Dependency; |
||||||
|
|
||||||
/** |
/** |
||||||
* @author Qiang Xue <qiang.xue@gmail.com> |
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
* @since 2.0 |
* @since 2.0 |
||||||
*/ |
*/ |
||||||
class FragmentCache extends Widget |
class FragmentCache extends Widget |
||||||
{ |
{ |
||||||
/** |
/** |
||||||
* @var Cache|string the cache object or the application component ID of the cache object. |
* @var Cache|string the cache object or the application component ID of the cache object. |
||||||
* After the FragmentCache object is created, if you want to change this property, |
* After the FragmentCache object is created, if you want to change this property, |
||||||
* you should only assign it with a cache object. |
* you should only assign it with a cache object. |
||||||
*/ |
*/ |
||||||
public $cache = 'cache'; |
public $cache = 'cache'; |
||||||
/** |
/** |
||||||
* @var integer number of seconds that the data can remain valid in 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. |
* Use 0 to indicate that the cached data will never expire. |
||||||
*/ |
*/ |
||||||
public $duration = 60; |
public $duration = 60; |
||||||
/** |
/** |
||||||
* @var array|Dependency the dependency that the cached content depends on. |
* @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. |
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||||
* For example, |
* For example, |
||||||
* |
* |
||||||
* ~~~ |
* ~~~ |
||||||
* array( |
* array( |
||||||
* 'class' => 'yii\caching\DbDependency', |
* 'class' => 'yii\caching\DbDependency', |
||||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||||
* ) |
* ) |
||||||
* ~~~ |
* ~~~ |
||||||
* |
* |
||||||
* would make the output cache depends on the last modified time of all posts. |
* 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. |
* If any post has its modification time changed, the cached content would be invalidated. |
||||||
*/ |
*/ |
||||||
public $dependency; |
public $dependency; |
||||||
/** |
/** |
||||||
* @var array list of factors that would cause the variation of the content being cached. |
* @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). |
* 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 |
* The following variation setting will cause the content to be cached in different versions |
||||||
* according to the current application language: |
* according to the current application language: |
||||||
* |
* |
||||||
* ~~~ |
* ~~~ |
||||||
* array( |
* array( |
||||||
* Yii::$app->language, |
* Yii::$app->language, |
||||||
* ) |
* ) |
||||||
*/ |
*/ |
||||||
public $variations; |
public $variations; |
||||||
/** |
/** |
||||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
* @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). |
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||||
*/ |
*/ |
||||||
public $enabled = true; |
public $enabled = true; |
||||||
/** |
/** |
||||||
* @var array a list of placeholders for embedding dynamic contents. This property |
* @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. |
* is used internally to implement the content caching feature. Do not modify it. |
||||||
*/ |
*/ |
||||||
public $dynamicPlaceholders; |
public $dynamicPlaceholders; |
||||||
|
|
||||||
/** |
/** |
||||||
* Initializes the FragmentCache object. |
* Initializes the FragmentCache object. |
||||||
*/ |
*/ |
||||||
public function init() |
public function init() |
||||||
{ |
{ |
||||||
parent::init(); |
parent::init(); |
||||||
|
|
||||||
if (!$this->enabled) { |
if (!$this->enabled) { |
||||||
$this->cache = null; |
$this->cache = null; |
||||||
} elseif (is_string($this->cache)) { |
} elseif (is_string($this->cache)) { |
||||||
$this->cache = Yii::$app->getComponent($this->cache); |
$this->cache = Yii::$app->getComponent($this->cache); |
||||||
} |
} |
||||||
|
|
||||||
if ($this->getCachedContent() === false) { |
if ($this->getCachedContent() === false) { |
||||||
$this->view->cacheStack[] = $this; |
$this->view->cacheStack[] = $this; |
||||||
ob_start(); |
ob_start(); |
||||||
ob_implicit_flush(false); |
ob_implicit_flush(false); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Marks the end of content to be cached. |
* Marks the end of content to be cached. |
||||||
* Content displayed before this method call and after {@link init()} |
* Content displayed before this method call and after {@link init()} |
||||||
* will be captured and saved in cache. |
* will be captured and saved in cache. |
||||||
* This method does nothing if valid content is already found in cache. |
* This method does nothing if valid content is already found in cache. |
||||||
*/ |
*/ |
||||||
public function run() |
public function run() |
||||||
{ |
{ |
||||||
if (($content = $this->getCachedContent()) !== false) { |
if (($content = $this->getCachedContent()) !== false) { |
||||||
echo $content; |
echo $content; |
||||||
} elseif ($this->cache instanceof Cache) { |
} elseif ($this->cache instanceof Cache) { |
||||||
$content = ob_get_clean(); |
$content = ob_get_clean(); |
||||||
array_pop($this->view->cacheStack); |
array_pop($this->view->cacheStack); |
||||||
if (is_array($this->dependency)) { |
if (is_array($this->dependency)) { |
||||||
$this->dependency = Yii::createObject($this->dependency); |
$this->dependency = Yii::createObject($this->dependency); |
||||||
} |
} |
||||||
$data = array($content, $this->dynamicPlaceholders); |
$data = array($content, $this->dynamicPlaceholders); |
||||||
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency); |
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency); |
||||||
|
|
||||||
if (empty($this->view->cacheStack) && !empty($this->dynamicPlaceholders)) { |
if (empty($this->view->cacheStack) && !empty($this->dynamicPlaceholders)) { |
||||||
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); |
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); |
||||||
} |
} |
||||||
echo $content; |
echo $content; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* @var string|boolean the cached content. False if the content is not cached. |
* @var string|boolean the cached content. False if the content is not cached. |
||||||
*/ |
*/ |
||||||
private $_content; |
private $_content; |
||||||
|
|
||||||
/** |
/** |
||||||
* Returns the cached content if available. |
* Returns the cached content if available. |
||||||
* @return string|boolean the cached content. False is returned if valid content is not found in the cache. |
* @return string|boolean the cached content. False is returned if valid content is not found in the cache. |
||||||
*/ |
*/ |
||||||
public function getCachedContent() |
public function getCachedContent() |
||||||
{ |
{ |
||||||
if ($this->_content === null) { |
if ($this->_content === null) { |
||||||
$this->_content = false; |
$this->_content = false; |
||||||
if ($this->cache instanceof Cache) { |
if ($this->cache instanceof Cache) { |
||||||
$key = $this->calculateKey(); |
$key = $this->calculateKey(); |
||||||
$data = $this->cache->get($key); |
$data = $this->cache->get($key); |
||||||
if (is_array($data) && count($data) === 2) { |
if (is_array($data) && count($data) === 2) { |
||||||
list ($content, $placeholders) = $data; |
list ($content, $placeholders) = $data; |
||||||
if (is_array($placeholders) && count($placeholders) > 0) { |
if (is_array($placeholders) && count($placeholders) > 0) { |
||||||
if (empty($this->view->cacheStack)) { |
if (empty($this->view->cacheStack)) { |
||||||
// outermost cache: replace placeholder with dynamic content |
// outermost cache: replace placeholder with dynamic content |
||||||
$content = $this->updateDynamicContent($content, $placeholders); |
$content = $this->updateDynamicContent($content, $placeholders); |
||||||
} |
} |
||||||
foreach ($placeholders as $name => $statements) { |
foreach ($placeholders as $name => $statements) { |
||||||
$this->view->addDynamicPlaceholder($name, $statements); |
$this->view->addDynamicPlaceholder($name, $statements); |
||||||
} |
} |
||||||
} |
} |
||||||
$this->_content = $content; |
$this->_content = $content; |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
return $this->_content; |
return $this->_content; |
||||||
} |
} |
||||||
|
|
||||||
protected function updateDynamicContent($content, $placeholders) |
protected function updateDynamicContent($content, $placeholders) |
||||||
{ |
{ |
||||||
foreach ($placeholders as $name => $statements) { |
foreach ($placeholders as $name => $statements) { |
||||||
$placeholders[$name] = $this->view->evaluateDynamicContent($statements); |
$placeholders[$name] = $this->view->evaluateDynamicContent($statements); |
||||||
} |
} |
||||||
return strtr($content, $placeholders); |
return strtr($content, $placeholders); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Generates a unique key used for storing the content in cache. |
* Generates a unique key used for storing the content in cache. |
||||||
* The key generated depends on both [[id]] and [[variations]]. |
* The key generated depends on both [[id]] and [[variations]]. |
||||||
* @return mixed a valid cache key |
* @return mixed a valid cache key |
||||||
*/ |
*/ |
||||||
protected function calculateKey() |
protected function calculateKey() |
||||||
{ |
{ |
||||||
$factors = array(__CLASS__, $this->getId()); |
$factors = array(__CLASS__, $this->getId()); |
||||||
if (is_array($this->variations)) { |
if (is_array($this->variations)) { |
||||||
foreach ($this->variations as $factor) { |
foreach ($this->variations as $factor) { |
||||||
$factors[] = $factor; |
$factors[] = $factor; |
||||||
} |
} |
||||||
} |
} |
||||||
return $factors; |
return $factors; |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue