|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\Widget;
- use yii\caching\Cache;
- use yii\caching\Dependency;
- use yii\di\Instance;
-
-
- class FragmentCache extends Widget
- {
-
-
- public $cache = 'cache';
-
-
- public $duration = 60;
-
-
- public $dependency;
-
-
- public $variations;
-
-
- public $enabled = true;
-
-
- public $dynamicPlaceholders;
-
-
-
-
- public function init()
- {
- parent::init();
-
- $this->cache = $this->enabled ? Instance::ensure($this->cache, Cache::className()) : null;
-
- if ($this->cache instanceof Cache && $this->getCachedContent() === false) {
- $this->getView()->cacheStack[] = $this;
- ob_start();
- ob_implicit_flush(false);
- }
- }
-
-
-
- public function run()
- {
- if (($content = $this->getCachedContent()) !== false) {
- echo $content;
- } elseif ($this->cache instanceof Cache) {
- 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;
- }
- }
-
-
-
- private $_content;
-
-
-
- public function getCachedContent()
- {
- if ($this->_content === null) {
- $this->_content = false;
- if ($this->cache instanceof Cache) {
- $key = $this->calculateKey();
- $data = $this->cache->get($key);
- if (is_array($data) && count($data) === 2) {
- list ($content, $placeholders) = $data;
- if (is_array($placeholders) && count($placeholders) > 0) {
- if (empty($this->getView()->cacheStack)) {
-
- $content = $this->updateDynamicContent($content, $placeholders);
- }
- foreach ($placeholders as $name => $statements) {
- $this->getView()->addDynamicPlaceholder($name, $statements);
- }
- }
- $this->_content = $content;
- }
- }
- }
-
- return $this->_content;
- }
-
-
-
- protected function updateDynamicContent($content, $placeholders)
- {
- foreach ($placeholders as $name => $statements) {
- $placeholders[$name] = $this->getView()->evaluateDynamicContent($statements);
- }
-
- return strtr($content, $placeholders);
- }
-
-
-
- protected function calculateKey()
- {
- $factors = [__CLASS__, $this->getId()];
- if (is_array($this->variations)) {
- foreach ($this->variations as $factor) {
- $factors[] = $factor;
- }
- }
-
- return $factors;
- }
- }
|