mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-08 17:07:33 +08:00
* php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
202 lines
6.5 KiB
PHP
202 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* @link http://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license http://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace yii\widgets;
|
|
|
|
use Yii;
|
|
use yii\base\Widget;
|
|
use yii\caching\CacheInterface;
|
|
use yii\caching\Dependency;
|
|
use yii\di\Instance;
|
|
|
|
/**
|
|
* FragmentCache is used by [[\yii\base\View]] to provide caching of page fragments.
|
|
*
|
|
* @property string|false $cachedContent The cached content. False is returned if valid content is not found
|
|
* in the cache. This property is read-only.
|
|
*
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
* @since 2.0
|
|
*/
|
|
class FragmentCache extends Widget
|
|
{
|
|
/**
|
|
* @var CacheInterface|array|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,
|
|
* you should only assign it with a cache object.
|
|
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
|
|
*/
|
|
public $cache = 'cache';
|
|
/**
|
|
* @var int 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,
|
|
*
|
|
* ```php
|
|
* [
|
|
* 'class' => 'yii\caching\DbDependency',
|
|
* 'sql' => 'SELECT MAX(updated_at) 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:
|
|
*
|
|
* ```php
|
|
* [
|
|
* Yii::$app->language,
|
|
* ]
|
|
* ```
|
|
*/
|
|
public $variations;
|
|
/**
|
|
* @var bool 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;
|
|
/**
|
|
* @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;
|
|
|
|
|
|
/**
|
|
* Initializes the FragmentCache object.
|
|
*/
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
|
|
$this->cache = $this->enabled ? Instance::ensure($this->cache, 'yii\caching\CacheInterface') : null;
|
|
|
|
if ($this->cache instanceof CacheInterface && $this->getCachedContent() === false) {
|
|
$this->getView()->cacheStack[] = $this;
|
|
ob_start();
|
|
ob_implicit_flush(false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Marks the end of content to be cached.
|
|
* Content displayed before this method call and after [[init()]]
|
|
* will be captured and saved in cache.
|
|
* This method does nothing if valid content is already found in cache.
|
|
*/
|
|
public function run()
|
|
{
|
|
if (($content = $this->getCachedContent()) !== false) {
|
|
echo $content;
|
|
} elseif ($this->cache instanceof CacheInterface) {
|
|
array_pop($this->getView()->cacheStack);
|
|
|
|
$content = ob_get_clean();
|
|
if ($content === false || $content === '') {
|
|
return;
|
|
}
|
|
if (is_array($this->dependency)) {
|
|
$this->dependency = Yii::createObject($this->dependency);
|
|
}
|
|
$data = [$content, $this->dynamicPlaceholders];
|
|
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
|
|
|
|
if (empty($this->getView()->cacheStack) && !empty($this->dynamicPlaceholders)) {
|
|
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
|
|
}
|
|
echo $content;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @var string|bool the cached content. False if the content is not cached.
|
|
*/
|
|
private $_content;
|
|
|
|
/**
|
|
* Returns the cached content if available.
|
|
* @return string|false the cached content. False is returned if valid content is not found in the cache.
|
|
*/
|
|
public function getCachedContent()
|
|
{
|
|
if ($this->_content !== null) {
|
|
return $this->_content;
|
|
}
|
|
|
|
$this->_content = false;
|
|
|
|
if (!($this->cache instanceof CacheInterface)) {
|
|
return $this->_content;
|
|
}
|
|
|
|
$key = $this->calculateKey();
|
|
$data = $this->cache->get($key);
|
|
if (!is_array($data) || count($data) !== 2) {
|
|
return $this->_content;
|
|
}
|
|
|
|
list($this->_content, $placeholders) = $data;
|
|
if (!is_array($placeholders) || count($placeholders) === 0) {
|
|
return $this->_content;
|
|
}
|
|
|
|
if (empty($this->getView()->cacheStack)) {
|
|
// outermost cache: replace placeholder with dynamic content
|
|
$this->_content = $this->updateDynamicContent($this->_content, $placeholders);
|
|
}
|
|
foreach ($placeholders as $name => $statements) {
|
|
$this->getView()->addDynamicPlaceholder($name, $statements);
|
|
}
|
|
|
|
return $this->_content;
|
|
}
|
|
|
|
/**
|
|
* Replaces placeholders in content by results of evaluated dynamic statements.
|
|
*
|
|
* @param string $content
|
|
* @param array $placeholders
|
|
* @return string final content
|
|
*/
|
|
protected function updateDynamicContent($content, $placeholders)
|
|
{
|
|
foreach ($placeholders as $name => $statements) {
|
|
$placeholders[$name] = $this->getView()->evaluateDynamicContent($statements);
|
|
}
|
|
|
|
return strtr($content, $placeholders);
|
|
}
|
|
|
|
/**
|
|
* Generates a unique key used for storing the content in cache.
|
|
* The key generated depends on both [[id]] and [[variations]].
|
|
* @return mixed a valid cache key
|
|
*/
|
|
protected function calculateKey()
|
|
{
|
|
$factors = [__CLASS__, $this->getId()];
|
|
if (is_array($this->variations)) {
|
|
foreach ($this->variations as $factor) {
|
|
$factors[] = $factor;
|
|
}
|
|
}
|
|
|
|
return $factors;
|
|
}
|
|
}
|