diff --git a/framework/base/ActionFilter.php b/framework/base/ActionFilter.php index d572a61..b2850f1 100644 --- a/framework/base/ActionFilter.php +++ b/framework/base/ActionFilter.php @@ -9,8 +9,6 @@ namespace yii\base; -use yii\util\ArrayHelper; - /** * ActionFilter is the base class for all action filters. * @@ -36,7 +34,7 @@ class ActionFilter extends Behavior */ public $owner; /** - * @var array IDs (case-insensitive) of actions that this filter applies to. + * @var array IDs of actions that this filter applies to. * If this property is empty or not set, it means this filter applies to all actions. * Note that if an action appears in [[except]], the filter will not apply to this action, even * if the action also appears in [[only]]. @@ -44,7 +42,7 @@ class ActionFilter extends Behavior */ public $only; /** - * @var array IDs (case-insensitive) of actions that this filter does NOT apply to. + * @var array IDs of actions that this filter does NOT apply to. */ public $except; @@ -86,7 +84,7 @@ class ActionFilter extends Behavior public function applyTo(Action $action) { - return (empty($this->only) || ArrayHelper::search($action->id, $this->only, false) !== false) - && (empty($this->except) || ArrayHelper::search($action->id, $this->except, false) === false); + return (empty($this->only) || in_array($action->id, $this->only, false) !== false) + && (empty($this->except) || in_array($action->id, $this->except, false) === false); } } \ No newline at end of file diff --git a/framework/base/View.php b/framework/base/View.php index edafb73..802855e 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -55,6 +55,10 @@ class View extends Component * @var mixed custom parameters that are available in the view template */ public $params; + /** + * @var Widget[] the widgets that are currently not ended + */ + protected $widgetStack = array(); /** * Constructor. @@ -128,19 +132,22 @@ class View extends Component return \Yii::createObject($properties, $this->context); } - public function widget($class, $properties = array()) + public function widget($class, $properties = array(), $captureOutput = false) { - $widget = $this->createWidget($class, $properties); - echo $widget->run(); - return $widget; + if ($captureOutput) { + ob_start(); + ob_implicit_flush(false); + $widget = $this->createWidget($class, $properties); + $widget->run(); + return ob_get_clean(); + } else { + $widget = $this->createWidget($class, $properties); + $widget->run(); + return $widget; + } } /** - * @var Widget[] the widgets that are currently not ended - */ - private $_widgetStack = array(); - - /** * Begins a widget. * @param string $class the widget class * @param array $properties the initial property values of the widget @@ -149,7 +156,7 @@ class View extends Component public function beginWidget($class, $properties = array()) { $widget = $this->createWidget($class, $properties); - $this->_widgetStack[] = $widget; + $this->widgetStack[] = $widget; return $widget; } @@ -163,8 +170,8 @@ class View extends Component */ public function endWidget() { - if (($widget = array_pop($this->_widgetStack)) !== null) { - echo $widget->run(); + if (($widget = array_pop($this->widgetStack)) !== null) { + $widget->run(); return $widget; } else { throw new Exception("Unmatched beginWidget() and endWidget() calls."); diff --git a/framework/base/Widget.php b/framework/base/Widget.php index 14ee4d5..c95ea7f 100644 --- a/framework/base/Widget.php +++ b/framework/base/Widget.php @@ -70,7 +70,6 @@ class Widget extends Component implements Initable /** * Executes the widget. - * @return string the rendering result of the widget */ public function run() { diff --git a/framework/caching/ChainedDependency.php b/framework/caching/ChainedDependency.php index 28c0da6..fef56ce 100644 --- a/framework/caching/ChainedDependency.php +++ b/framework/caching/ChainedDependency.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CChainedCacheDependency extends CComponent implements ICacheDependency +class ChainedDependency extends Dependency { private $_dependencies=null; diff --git a/framework/caching/DbDependency.php b/framework/caching/DbDependency.php index 6a38a6c..005a969 100644 --- a/framework/caching/DbDependency.php +++ b/framework/caching/DbDependency.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CDbCacheDependency extends CCacheDependency +class DbDependency extends CacheDependency { /** * @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'. */ - public $connectionID='db'; + public $connectionID = 'db'; /** * @var string the SQL statement whose result is used to determine if the dependency has been changed. * Note, the SQL statement should return back a single value. @@ -44,9 +44,9 @@ class CDbCacheDependency extends CCacheDependency * Constructor. * @param string $sql the SQL statement whose result is used to determine if the dependency has been changed. */ - public function __construct($sql=null) + public function __construct($sql = null) { - $this->sql=$sql; + $this->sql = $sql; } /** @@ -56,7 +56,7 @@ class CDbCacheDependency extends CCacheDependency */ public function __sleep() { - $this->_db=null; + $this->_db = null; return array_keys((array)$this); } @@ -65,31 +65,29 @@ class CDbCacheDependency extends CCacheDependency * This method returns the value of the global state. * @return mixed the data needed to determine if dependency has been changed. */ - protected function generateDependentData() + protected function generateDependencyData() { - if($this->sql!==null) - { - $db=$this->getDbConnection(); - $command=$db->createCommand($this->sql); - if(is_array($this->params)) - { - foreach($this->params as $name=>$value) - $command->bindValue($name,$value); + if ($this->sql !== null) { + $db = $this->getDbConnection(); + $command = $db->createCommand($this->sql); + if (is_array($this->params)) { + foreach ($this->params as $name => $value) { + $command->bindValue($name, $value); + } } - if($db->queryCachingDuration>0) - { + if ($db->queryCachingDuration > 0) { // temporarily disable and re-enable query caching - $duration=$db->queryCachingDuration; - $db->queryCachingDuration=0; - $result=$command->queryRow(); - $db->queryCachingDuration=$duration; + $duration = $db->queryCachingDuration; + $db->queryCachingDuration = 0; + $result = $command->queryRow(); + $db->queryCachingDuration = $duration; + } else { + $result = $command->queryRow(); } - else - $result=$command->queryRow(); return $result; + } else { + throw new CException(Yii::t('yii', 'DbDependency.sql cannot be empty.')); } - else - throw new CException(Yii::t('yii','CDbCacheDependency.sql cannot be empty.')); } /** @@ -98,15 +96,15 @@ class CDbCacheDependency extends CCacheDependency */ protected function getDbConnection() { - if($this->_db!==null) + if ($this->_db !== null) { return $this->_db; - else - { - if(($this->_db=\Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) + } else { + if (($this->_db = \Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) { return $this->_db; - else - throw new CException(Yii::t('yii','CDbCacheDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.', - array('{id}'=>$this->connectionID))); + } else { + throw new CException(Yii::t('yii', 'DbDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.', + array('{id}' => $this->connectionID))); + } } } } diff --git a/framework/caching/Dependency.php b/framework/caching/Dependency.php index 6f6bb4b..48b637c 100644 --- a/framework/caching/Dependency.php +++ b/framework/caching/Dependency.php @@ -12,37 +12,21 @@ namespace yii\caching; /** * Dependency is the base class for cache dependency classes. * - * Dependency implements the {@link ICacheDependency} interface. - * Child classes should override its {@link generateDependentData} for - * actual dependency checking. + * Child classes should override its [[generateDependencyData()]] for generating + * the actual dependency data. * * @property boolean $hasChanged Whether the dependency has changed. - * @property mixed $dependentData The data used to determine if dependency has been changed. - * This data is available after {@link evaluateDependency} is called. * * @author Qiang Xue * @since 2.0 */ -class Dependency extends \yii\base\Object +abstract class Dependency extends \yii\base\Object { /** - * @var boolean Whether this dependency is reusable or not. - * If set to true, dependent data for this cache dependency will only be generated once per request. - * You can then use the same cache dependency for multiple separate cache calls on the same page - * without the overhead of re-evaluating the dependency each time. - * Defaults to false; - * @since 1.1.11 + * @var mixed the dependency data that is saved in cache and later is compared with the + * latest dependency data. */ - public $reuseDependentData=false; - - /** - * @var array cached data for reusable dependencies. - * @since 1.1.11 - */ - private static $_reusableData=array(); - - private $_hash; - private $_data; + public $data; /** * Evaluates the dependency by generating and saving the data related with dependency. @@ -50,15 +34,7 @@ class Dependency extends \yii\base\Object */ public function evaluateDependency() { - if ($this->reuseDependentData) - { - $hash=$this->getHash(); - if (!isset(self::$_reusableData[$hash]['dependentData'])) - self::$_reusableData[$hash]['dependentData']=$this->generateDependentData(); - $this->_data=self::$_reusableData[$hash]['dependentData']; - } - else - $this->_data=$this->generateDependentData(); + $this->data = $this->generateDependencyData(); } /** @@ -66,47 +42,13 @@ class Dependency extends \yii\base\Object */ public function getHasChanged() { - if ($this->reuseDependentData) - { - $hash=$this->getHash(); - if (!isset(self::$_reusableData[$hash]['hasChanged'])) - { - if (!isset(self::$_reusableData[$hash]['dependentData'])) - self::$_reusableData[$hash]['dependentData']=$this->generateDependentData(); - self::$_reusableData[$hash]['hasChanged']=self::$_reusableData[$hash]['dependentData']!=$this->_data; - } - return self::$_reusableData[$hash]['hasChanged']; - } - else - return $this->generateDependentData()!=$this->_data; - } - - /** - * @return mixed the data used to determine if dependency has been changed. - * This data is available after {@link evaluateDependency} is called. - */ - public function getDependentData() - { - return $this->_data; + return $this->generateDependencyData() != $this->data; } /** * Generates the data needed to determine if dependency has been changed. - * Derived classes should override this method to generate actual dependent data. + * Derived classes should override this method to generate the actual dependency data. * @return mixed the data needed to determine if dependency has been changed. */ - protected function generateDependentData() - { - return null; - } - /** - * Generates a unique hash that identifies this cache dependency. - * @return string the hash for this cache dependency - */ - private function getHash() - { - if($this->_hash===null) - $this->_hash=sha1(serialize($this)); - return $this->_hash; - } + abstract protected function generateDependencyData(); } \ No newline at end of file diff --git a/framework/caching/DirectoryDependency.php b/framework/caching/DirectoryDependency.php index 7244713..f977bad 100644 --- a/framework/caching/DirectoryDependency.php +++ b/framework/caching/DirectoryDependency.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CDirectoryCacheDependency extends CCacheDependency +class DirectoryDependency extends Dependency { /** * @var string the directory whose change is used to determine if the dependency has been changed. @@ -41,7 +41,7 @@ class CDirectoryCacheDependency extends CCacheDependency * If the value is less than 0, it means unlimited depth. * If the value is 0, it means checking the files directly under the specified directory. */ - public $recursiveLevel=-1; + public $recursiveLevel = -1; /** * @var string the regular expression matching valid file/directory names. * Only the matching files or directories will be checked for changes. @@ -53,9 +53,9 @@ class CDirectoryCacheDependency extends CCacheDependency * Constructor. * @param string $directory the directory to be checked */ - public function __construct($directory=null) + public function __construct($directory = null) { - $this->directory=$directory; + $this->directory = $directory; } /** @@ -63,12 +63,13 @@ class CDirectoryCacheDependency extends CCacheDependency * This method returns the modification timestamps for files under the directory. * @return mixed the data needed to determine if dependency has been changed. */ - protected function generateDependentData() + protected function generateDependencyData() { - if($this->directory!==null) + if ($this->directory !== null) { return $this->generateTimestamps($this->directory); - else - throw new CException(Yii::t('yii','CDirectoryCacheDependency.directory cannot be empty.')); + } else { + throw new CException(Yii::t('yii', 'DirectoryDependency.directory cannot be empty.')); + } } /** @@ -78,28 +79,29 @@ class CDirectoryCacheDependency extends CCacheDependency * @param integer $level level of the recursion * @return array list of file modification time indexed by the file path */ - protected function generateTimestamps($directory,$level=0) + protected function generateTimestamps($directory, $level = 0) { - if(($dir=@opendir($directory))===false) - throw new CException(Yii::t('yii','"{path}" is not a valid directory.', - array('{path}'=>$directory))); - $timestamps=array(); - while(($file=readdir($dir))!==false) - { - $path=$directory.DIRECTORY_SEPARATOR.$file; - if($file==='.' || $file==='..') + if (($dir = @opendir($directory)) === false) { + throw new CException(Yii::t('yii', '"{path}" is not a valid directory.', + array('{path}' => $directory))); + } + $timestamps = array(); + while (($file = readdir($dir)) !== false) { + $path = $directory . DIRECTORY_SEPARATOR . $file; + if ($file === '.' || $file === '..') { continue; - if($this->namePattern!==null && !preg_match($this->namePattern,$file)) + } + if ($this->namePattern !== null && !preg_match($this->namePattern, $file)) { continue; - if(is_file($path)) - { - if($this->validateFile($path)) - $timestamps[$path]=filemtime($path); } - else - { - if(($this->recursiveLevel<0 || $level<$this->recursiveLevel) && $this->validateDirectory($path)) - $timestamps=array_merge($timestamps, $this->generateTimestamps($path,$level+1)); + if (is_file($path)) { + if ($this->validateFile($path)) { + $timestamps[$path] = filemtime($path); + } + } else { + if (($this->recursiveLevel < 0 || $level < $this->recursiveLevel) && $this->validateDirectory($path)) { + $timestamps = array_merge($timestamps, $this->generateTimestamps($path, $level + 1)); + } } } closedir($dir); diff --git a/framework/caching/ExpressionDependency.php b/framework/caching/ExpressionDependency.php index 032ec32..18f6c54 100644 --- a/framework/caching/ExpressionDependency.php +++ b/framework/caching/ExpressionDependency.php @@ -45,7 +45,7 @@ class CExpressionDependency extends CCacheDependency * This method returns the result of the PHP expression. * @return mixed the data needed to determine if dependency has been changed. */ - protected function generateDependentData() + protected function generateDependencyData() { return $this->evaluateExpression($this->expression); } diff --git a/framework/caching/FileDependency.php b/framework/caching/FileDependency.php index 4293057..3e1b2b6 100644 --- a/framework/caching/FileDependency.php +++ b/framework/caching/FileDependency.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CFileCacheDependency extends CCacheDependency +class FileDependency extends Dependency { /** * @var string the name of the file whose last modification time is used to @@ -33,9 +30,9 @@ class CFileCacheDependency extends CCacheDependency * Constructor. * @param string $fileName name of the file whose change is to be checked. */ - public function __construct($fileName=null) + public function __construct($fileName = null) { - $this->fileName=$fileName; + $this->fileName = $fileName; } /** @@ -43,11 +40,8 @@ class CFileCacheDependency extends CCacheDependency * This method returns the file's last modification time. * @return mixed the data needed to determine if dependency has been changed. */ - protected function generateDependentData() + protected function generateDependencyData() { - if($this->fileName!==null) - return @filemtime($this->fileName); - else - throw new CException(Yii::t('yii','CFileCacheDependency.fileName cannot be empty.')); + return $this->fileName !== null ? @filemtime($this->fileName) : 0; } }