summaryrefslogtreecommitdiff
path: root/framework/caching/dependencies
diff options
context:
space:
mode:
authorPatrick Seeger <pseeger@ccwn.org>2012-04-13 23:11:05 +0200
committerPatrick Seeger <pseeger@ccwn.org>2012-04-13 23:11:05 +0200
commit341cc4dd9c53ffbfb863e026dd58549c1082c7a7 (patch)
tree1bbbed20313bafb9b063b6b4d894fe580d8b000f /framework/caching/dependencies
yii-framework 1.1.10 hinzugefügtHEADmaster
Diffstat (limited to 'framework/caching/dependencies')
-rw-r--r--framework/caching/dependencies/CCacheDependency.php66
-rw-r--r--framework/caching/dependencies/CChainedCacheDependency.php98
-rw-r--r--framework/caching/dependencies/CDbCacheDependency.php112
-rw-r--r--framework/caching/dependencies/CDirectoryCacheDependency.php134
-rw-r--r--framework/caching/dependencies/CExpressionDependency.php53
-rw-r--r--framework/caching/dependencies/CFileCacheDependency.php53
-rw-r--r--framework/caching/dependencies/CGlobalStateCacheDependency.php54
7 files changed, 570 insertions, 0 deletions
diff --git a/framework/caching/dependencies/CCacheDependency.php b/framework/caching/dependencies/CCacheDependency.php
new file mode 100644
index 0000000..ca0f318
--- /dev/null
+++ b/framework/caching/dependencies/CCacheDependency.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * CCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CCacheDependency is the base class for cache dependency classes.
+ *
+ * CCacheDependency implements the {@link ICacheDependency} interface.
+ * Child classes should override its {@link generateDependentData} for
+ * actual dependency checking.
+ *
+ * @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 <qiang.xue@gmail.com>
+ * @version $Id: CCacheDependency.php 3426 2011-10-25 00:01:09Z alexander.makarow $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CCacheDependency extends CComponent implements ICacheDependency
+{
+ private $_data;
+
+ /**
+ * Evaluates the dependency by generating and saving the data related with dependency.
+ * This method is invoked by cache before writing data into it.
+ */
+ public function evaluateDependency()
+ {
+ $this->_data=$this->generateDependentData();
+ }
+
+ /**
+ * @return boolean whether the dependency has changed.
+ */
+ public function getHasChanged()
+ {
+ 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;
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * Derived classes should override this method to generate actual dependent data.
+ * @return mixed the data needed to determine if dependency has been changed.
+ */
+ protected function generateDependentData()
+ {
+ return null;
+ }
+} \ No newline at end of file
diff --git a/framework/caching/dependencies/CChainedCacheDependency.php b/framework/caching/dependencies/CChainedCacheDependency.php
new file mode 100644
index 0000000..a7b985f
--- /dev/null
+++ b/framework/caching/dependencies/CChainedCacheDependency.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * CChainedCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CChainedCacheDependency represents a list of cache dependencies.
+ *
+ * If any of the dependencies reports a dependency change, CChainedCacheDependency
+ * will return true for the checking.
+ *
+ * To add dependencies to CChainedCacheDependency, use {@link getDependencies Dependencies}
+ * which gives a {@link CTypedList} instance and can be used like an array
+ * (see {@link CList} for more details}).
+ *
+ * @property CTypedList $dependencies List of dependency objects.
+ * @property boolean $hasChanged Whether the dependency is changed or not.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CChainedCacheDependency.php 3515 2011-12-28 12:29:24Z mdomba $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CChainedCacheDependency extends CComponent implements ICacheDependency
+{
+ private $_dependencies=null;
+
+ /**
+ * Constructor.
+ * @param array $dependencies the dependencies to be added to this chain.
+ * @since 1.1.4
+ */
+ public function __construct($dependencies=array())
+ {
+ if(!empty($dependencies))
+ $this->setDependencies($dependencies);
+ }
+
+ /**
+ * @return CTypedList list of dependency objects
+ */
+ public function getDependencies()
+ {
+ if($this->_dependencies===null)
+ $this->_dependencies=new CTypedList('ICacheDependency');
+ return $this->_dependencies;
+ }
+
+ /**
+ * @param array $values list of dependency objects or configurations to be added to this chain.
+ * If a depedency is specified as a configuration, it must be an array that can be recognized
+ * by {@link YiiBase::createComponent}.
+ */
+ public function setDependencies($values)
+ {
+ $dependencies=$this->getDependencies();
+ foreach($values as $value)
+ {
+ if(is_array($value))
+ $value=Yii::createComponent($value);
+ $dependencies->add($value);
+ }
+ }
+
+ /**
+ * Evaluates the dependency by generating and saving the data related with dependency.
+ */
+ public function evaluateDependency()
+ {
+ if($this->_dependencies!==null)
+ {
+ foreach($this->_dependencies as $dependency)
+ $dependency->evaluateDependency();
+ }
+ }
+
+ /**
+ * Performs the actual dependency checking.
+ * This method returns true if any of the dependency objects
+ * reports a dependency change.
+ * @return boolean whether the dependency is changed or not.
+ */
+ public function getHasChanged()
+ {
+ if($this->_dependencies!==null)
+ {
+ foreach($this->_dependencies as $dependency)
+ if($dependency->getHasChanged())
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/framework/caching/dependencies/CDbCacheDependency.php b/framework/caching/dependencies/CDbCacheDependency.php
new file mode 100644
index 0000000..98b8e8d
--- /dev/null
+++ b/framework/caching/dependencies/CDbCacheDependency.php
@@ -0,0 +1,112 @@
+<?php
+/**
+ * CDbCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CDbCacheDependency represents a dependency based on the query result of a SQL statement.
+ *
+ * If the query result (a scalar) changes, the dependency is considered as changed.
+ * To specify the SQL statement, set {@link sql} property.
+ * The {@link connectionID} property specifies the ID of a {@link CDbConnection} application
+ * component. It is this DB connection that is used to perform the query.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CDbCacheDependency.php 3204 2011-05-05 21:36:32Z alexander.makarow $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CDbCacheDependency extends CCacheDependency
+{
+ /**
+ * @var string the ID of a {@link CDbConnection} application component. Defaults to '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.
+ */
+ public $sql;
+ /**
+ * @var array parameters (name=>value) to be bound to the SQL statement specified by {@link sql}.
+ * @since 1.1.4
+ */
+ public $params;
+
+ private $_db;
+
+ /**
+ * Constructor.
+ * @param string $sql the SQL statement whose result is used to determine if the dependency has been changed.
+ */
+ public function __construct($sql=null)
+ {
+ $this->sql=$sql;
+ }
+
+ /**
+ * PHP sleep magic method.
+ * This method ensures that the database instance is set null because it contains resource handles.
+ * @return array
+ */
+ public function __sleep()
+ {
+ $this->_db=null;
+ return array_keys((array)$this);
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * This method returns the value of the global state.
+ * @return mixed the data needed to determine if dependency has been changed.
+ */
+ protected function generateDependentData()
+ {
+ 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)
+ {
+ // temporarily disable and re-enable query caching
+ $duration=$db->queryCachingDuration;
+ $db->queryCachingDuration=0;
+ $result=$command->queryRow();
+ $db->queryCachingDuration=$duration;
+ }
+ else
+ $result=$command->queryRow();
+ return $result;
+ }
+ else
+ throw new CException(Yii::t('yii','CDbCacheDependency.sql cannot be empty.'));
+ }
+
+ /**
+ * @return CDbConnection the DB connection instance
+ * @throws CException if {@link connectionID} does not point to a valid application component.
+ */
+ protected function getDbConnection()
+ {
+ if($this->_db!==null)
+ return $this->_db;
+ else
+ {
+ if(($this->_db=Yii::app()->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)));
+ }
+ }
+}
diff --git a/framework/caching/dependencies/CDirectoryCacheDependency.php b/framework/caching/dependencies/CDirectoryCacheDependency.php
new file mode 100644
index 0000000..2ebab9c
--- /dev/null
+++ b/framework/caching/dependencies/CDirectoryCacheDependency.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * CDirectoryCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CDirectoryCacheDependency represents a dependency based on change of a directory.
+ *
+ * CDirectoryCacheDependency performs dependency checking based on the
+ * modification time of the files contained in the specified directory.
+ * The directory being checked is specified via {@link directory}.
+ *
+ * By default, all files under the specified directory and subdirectories
+ * will be checked. If the last modification time of any of them is changed
+ * or if different number of files are contained in a directory, the dependency
+ * is reported as changed. By specifying {@link recursiveLevel},
+ * one can limit the checking to a certain depth of the directory.
+ *
+ * Note, dependency checking for a directory is expensive because it involves
+ * accessing modification time of multiple files under the directory.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CDirectoryCacheDependency.php 3500 2011-12-20 16:25:43Z mdomba $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CDirectoryCacheDependency extends CCacheDependency
+{
+ /**
+ * @var string the directory whose change is used to determine if the dependency has been changed.
+ * If any of the files under the directory is changed, the dependency is considered as changed.
+ */
+ public $directory;
+ /**
+ * @var integer the depth of the subdirectories to be recursively checked.
+ * 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;
+ /**
+ * @var string the regular expression matching valid file/directory names.
+ * Only the matching files or directories will be checked for changes.
+ * Defaults to null, meaning all files/directories will qualify.
+ */
+ public $namePattern;
+
+ /**
+ * Constructor.
+ * @param string $directory the directory to be checked
+ */
+ public function __construct($directory=null)
+ {
+ $this->directory=$directory;
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * 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()
+ {
+ if($this->directory!==null)
+ return $this->generateTimestamps($this->directory);
+ else
+ throw new CException(Yii::t('yii','CDirectoryCacheDependency.directory cannot be empty.'));
+ }
+
+ /**
+ * Determines the last modification time for files under the directory.
+ * This method may go recursively into subdirectories if {@link recursiveLevel} is not 0.
+ * @param string $directory the directory name
+ * @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)
+ {
+ 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))
+ 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));
+ }
+ }
+ closedir($dir);
+ return $timestamps;
+ }
+
+ /**
+ * Checks to see if the file should be checked for dependency.
+ * This method is invoked when dependency of the whole directory is being checked.
+ * By default, it always returns true, meaning the file should be checked.
+ * You may override this method to check only certain files.
+ * @param string $fileName the name of the file that may be checked for dependency.
+ * @return boolean whether this file should be checked.
+ */
+ protected function validateFile($fileName)
+ {
+ return true;
+ }
+
+ /**
+ * Checks to see if the specified subdirectory should be checked for dependency.
+ * This method is invoked when dependency of the whole directory is being checked.
+ * By default, it always returns true, meaning the subdirectory should be checked.
+ * You may override this method to check only certain subdirectories.
+ * @param string $directory the name of the subdirectory that may be checked for dependency.
+ * @return boolean whether this subdirectory should be checked.
+ */
+ protected function validateDirectory($directory)
+ {
+ return true;
+ }
+}
diff --git a/framework/caching/dependencies/CExpressionDependency.php b/framework/caching/dependencies/CExpressionDependency.php
new file mode 100644
index 0000000..393ac96
--- /dev/null
+++ b/framework/caching/dependencies/CExpressionDependency.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * CExpressionDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CExpressionDependency represents a dependency based on the result of a PHP expression.
+ *
+ * CExpressionDependency performs dependency checking based on the
+ * result of a PHP {@link expression}.
+ * The dependency is reported as unchanged if and only if the result is
+ * the same as the one evaluated when storing the data to cache.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CExpressionDependency.php 3515 2011-12-28 12:29:24Z mdomba $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CExpressionDependency extends CCacheDependency
+{
+ /**
+ * @var string the PHP expression whose result is used to determine the dependency.
+ * The expression can also be a valid PHP callback,
+ * including class method name (array(ClassName/Object, MethodName)),
+ * or anonymous function (PHP 5.3.0+). The function/method will be passed with a
+ * parameter which is the dependency object itself.
+ */
+ public $expression;
+
+ /**
+ * Constructor.
+ * @param string $expression the PHP expression whose result is used to determine the dependency.
+ */
+ public function __construct($expression='true')
+ {
+ $this->expression=$expression;
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * This method returns the result of the PHP expression.
+ * @return mixed the data needed to determine if dependency has been changed.
+ */
+ protected function generateDependentData()
+ {
+ return $this->evaluateExpression($this->expression);
+ }
+}
diff --git a/framework/caching/dependencies/CFileCacheDependency.php b/framework/caching/dependencies/CFileCacheDependency.php
new file mode 100644
index 0000000..44907cd
--- /dev/null
+++ b/framework/caching/dependencies/CFileCacheDependency.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * CFileCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CFileCacheDependency represents a dependency based on a file's last modification time.
+ *
+ * CFileCacheDependency performs dependency checking based on the
+ * last modification time of the file specified via {@link fileName}.
+ * The dependency is reported as unchanged if and only if the file's
+ * last modification time remains unchanged.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CFileCacheDependency.php 2799 2011-01-01 19:31:13Z qiang.xue $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CFileCacheDependency extends CCacheDependency
+{
+ /**
+ * @var string the name of the file whose last modification time is used to
+ * check if the dependency has been changed.
+ */
+ public $fileName;
+
+ /**
+ * Constructor.
+ * @param string $fileName name of the file whose change is to be checked.
+ */
+ public function __construct($fileName=null)
+ {
+ $this->fileName=$fileName;
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * This method returns the file's last modification time.
+ * @return mixed the data needed to determine if dependency has been changed.
+ */
+ protected function generateDependentData()
+ {
+ if($this->fileName!==null)
+ return @filemtime($this->fileName);
+ else
+ throw new CException(Yii::t('yii','CFileCacheDependency.fileName cannot be empty.'));
+ }
+}
diff --git a/framework/caching/dependencies/CGlobalStateCacheDependency.php b/framework/caching/dependencies/CGlobalStateCacheDependency.php
new file mode 100644
index 0000000..4775fc6
--- /dev/null
+++ b/framework/caching/dependencies/CGlobalStateCacheDependency.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * CGlobalStateCacheDependency class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CGlobalStateCacheDependency represents a dependency based on a global state value.
+ *
+ * CGlobalStateCacheDependency checks if a global state is changed or not.
+ * If the global state is changed, the dependency is reported as changed.
+ * To specify which global state this dependency should check with,
+ * set {@link stateName} to the name of the global state.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CGlobalStateCacheDependency.php 2799 2011-01-01 19:31:13Z qiang.xue $
+ * @package system.caching.dependencies
+ * @since 1.0
+ */
+class CGlobalStateCacheDependency extends CCacheDependency
+{
+ /**
+ * @var string the name of the global state whose value is to check
+ * if the dependency has changed.
+ * @see CApplication::setGlobalState
+ */
+ public $stateName;
+
+ /**
+ * Constructor.
+ * @param string $name the name of the global state
+ */
+ public function __construct($name=null)
+ {
+ $this->stateName=$name;
+ }
+
+ /**
+ * Generates the data needed to determine if dependency has been changed.
+ * This method returns the value of the global state.
+ * @return mixed the data needed to determine if dependency has been changed.
+ */
+ protected function generateDependentData()
+ {
+ if($this->stateName!==null)
+ return Yii::app()->getGlobalState($this->stateName);
+ else
+ throw new CException(Yii::t('yii','CGlobalStateCacheDependency.stateName cannot be empty.'));
+ }
+}