Commit 5cbd5722 by Qiang Xue

Fixes #1162: removed constructors of cache dependency classes so that they can…

Fixes #1162: removed constructors of cache dependency classes so that they can be easily created from configuration.
parent b9fb97d6
......@@ -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
*/
......
......@@ -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
......
......@@ -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
......
......@@ -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);
}
}
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment