From 5cbd5722c0b8bf9d0222a6cd4d16f21d082a66b7 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 8 Nov 2013 16:44:24 -0500 Subject: [PATCH] Fixes #1162: removed constructors of cache dependency classes so that they can be easily created from configuration. --- framework/yii/caching/ChainedDependency.php | 14 +------------- framework/yii/caching/DbDependency.php | 18 ++++-------------- framework/yii/caching/ExpressionDependency.php | 15 +-------------- framework/yii/caching/FileDependency.php | 16 +++++----------- framework/yii/caching/GroupDependency.php | 22 ++++++++++------------ 5 files changed, 21 insertions(+), 64 deletions(-) diff --git a/framework/yii/caching/ChainedDependency.php b/framework/yii/caching/ChainedDependency.php index 20cb632..f8bfee3 100644 --- a/framework/yii/caching/ChainedDependency.php +++ b/framework/yii/caching/ChainedDependency.php @@ -23,7 +23,7 @@ class ChainedDependency extends Dependency * @var Dependency[] list of dependencies that this dependency is composed of. * Each array element must be a dependency object. */ - public $dependencies; + public $dependencies = []; /** * @var boolean whether this dependency is depending on every dependency in [[dependencies]]. * Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed. @@ -33,18 +33,6 @@ class ChainedDependency extends Dependency public $dependOnAll = true; /** - * Constructor. - * @param Dependency[] $dependencies list of dependencies that this dependency is composed of. - * Each array element should be a dependency object. - * @param array $config name-value pairs that will be used to initialize the object properties - */ - public function __construct($dependencies = [], $config = []) - { - $this->dependencies = $dependencies; - parent::__construct($config); - } - - /** * Evaluates the dependency by generating and saving the data related with dependency. * @param Cache $cache the cache component that is currently evaluating this dependency */ diff --git a/framework/yii/caching/DbDependency.php b/framework/yii/caching/DbDependency.php index e8e1e68..ac6f2e7 100644 --- a/framework/yii/caching/DbDependency.php +++ b/framework/yii/caching/DbDependency.php @@ -34,20 +34,7 @@ class DbDependency extends Dependency /** * @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]]. */ - public $params; - - /** - * Constructor. - * @param string $sql the SQL query whose result is used to determine if the dependency has been changed. - * @param array $params the parameters (name => value) to be bound to the SQL statement specified by [[sql]]. - * @param array $config name-value pairs that will be used to initialize the object properties - */ - public function __construct($sql, $params = [], $config = []) - { - $this->sql = $sql; - $this->params = $params; - parent::__construct($config); - } + public $params = []; /** * Generates the data needed to determine if dependency has been changed. @@ -62,6 +49,9 @@ class DbDependency extends Dependency if (!$db instanceof Connection) { throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection."); } + if ($this->sql === null) { + throw new InvalidConfigException("DbDependency::sql must be set."); + } if ($db->enableQueryCache) { // temporarily disable and re-enable query caching diff --git a/framework/yii/caching/ExpressionDependency.php b/framework/yii/caching/ExpressionDependency.php index 36250d2..f6642b4 100644 --- a/framework/yii/caching/ExpressionDependency.php +++ b/framework/yii/caching/ExpressionDependency.php @@ -27,7 +27,7 @@ class ExpressionDependency extends Dependency * A PHP expression can be any PHP code that evaluates to a value. To learn more about what an expression is, * please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php). */ - public $expression; + public $expression = 'true'; /** * @var mixed custom parameters associated with this dependency. You may get the value * of this property in [[expression]] using `$this->params`. @@ -35,19 +35,6 @@ class ExpressionDependency extends Dependency public $params; /** - * Constructor. - * @param string $expression the PHP expression whose result is used to determine the dependency. - * @param mixed $params the custom parameters associated with this dependency - * @param array $config name-value pairs that will be used to initialize the object properties - */ - public function __construct($expression = 'true', $params = null, $config = []) - { - $this->expression = $expression; - $this->params = $params; - parent::__construct($config); - } - - /** * Generates the data needed to determine if dependency has been changed. * This method returns the result of the PHP expression. * @param Cache $cache the cache component that is currently evaluating this dependency diff --git a/framework/yii/caching/FileDependency.php b/framework/yii/caching/FileDependency.php index fa8b147..11afde3 100644 --- a/framework/yii/caching/FileDependency.php +++ b/framework/yii/caching/FileDependency.php @@ -6,6 +6,7 @@ */ namespace yii\caching; +use yii\base\InvalidConfigException; /** * FileDependency represents a dependency based on a file's last modification time. @@ -25,24 +26,17 @@ class FileDependency extends Dependency public $fileName; /** - * Constructor. - * @param string $fileName name of the file whose change is to be checked. - * @param array $config name-value pairs that will be used to initialize the object properties - */ - public function __construct($fileName = null, $config = []) - { - $this->fileName = $fileName; - parent::__construct($config); - } - - /** * Generates the data needed to determine if dependency has been changed. * This method returns the file's last modification time. * @param Cache $cache the cache component that is currently evaluating this dependency * @return mixed the data needed to determine if dependency has been changed. + * @throws InvalidConfigException if [[fileName]] is not set */ protected function generateDependencyData($cache) { + if ($this->fileName === null) { + throw new InvalidConfigException('FileDependency::fileName must be set'); + } return @filemtime($this->fileName); } } diff --git a/framework/yii/caching/GroupDependency.php b/framework/yii/caching/GroupDependency.php index 48673f6..1cf7869 100644 --- a/framework/yii/caching/GroupDependency.php +++ b/framework/yii/caching/GroupDependency.php @@ -6,6 +6,7 @@ */ namespace yii\caching; +use yii\base\InvalidConfigException; /** * GroupDependency marks a cached data item with a group name. @@ -19,29 +20,22 @@ namespace yii\caching; class GroupDependency extends Dependency { /** - * @var string the group name + * @var string the group name. This property must be set. */ public $group; /** - * Constructor. - * @param string $group the group name - * @param array $config name-value pairs that will be used to initialize the object properties - */ - public function __construct($group, $config = []) - { - $this->group = $group; - parent::__construct($config); - } - - /** * Generates the data needed to determine if dependency has been changed. * This method does nothing in this class. * @param Cache $cache the cache component that is currently evaluating this dependency * @return mixed the data needed to determine if dependency has been changed. + * @throws InvalidConfigException if [[group]] is not set. */ protected function generateDependencyData($cache) { + if ($this->group === null) { + throw new InvalidConfigException('GroupDependency::group must be set'); + } $version = $cache->get([__CLASS__, $this->group]); if ($version === false) { $version = $this->invalidate($cache, $this->group); @@ -53,9 +47,13 @@ class GroupDependency extends Dependency * Performs the actual dependency checking. * @param Cache $cache the cache component that is currently evaluating this dependency * @return boolean whether the dependency is changed or not. + * @throws InvalidConfigException if [[group]] is not set. */ public function getHasChanged($cache) { + if ($this->group === null) { + throw new InvalidConfigException('GroupDependency::group must be set'); + } $version = $cache->get([__CLASS__, $this->group]); return $version === false || $version !== $this->data; }