Browse Source

refactoring cache and db references.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
e1acc64b2b
  1. 1246
      framework/base/Module.php
  2. 169
      framework/caching/DbCache.php
  3. 53
      framework/caching/DbDependency.php
  4. 6
      framework/console/Controller.php
  5. 1281
      framework/console/controllers/MigrateController.php
  6. 34
      framework/db/Command.php
  7. 25
      framework/db/Connection.php
  8. 30
      framework/db/Schema.php
  9. 67
      framework/logging/DbTarget.php
  10. 58
      framework/web/CacheSession.php
  11. 170
      framework/web/DbSession.php
  12. 217
      framework/web/PageCache.php
  13. 30
      framework/web/UrlManager.php
  14. 395
      framework/widgets/FragmentCache.php
  15. 896
      tests/unit/framework/util/HtmlTest.php
  16. 19
      tests/unit/framework/web/UrlManagerTest.php
  17. 4
      tests/unit/framework/web/UrlRuleTest.php

1246
framework/base/Module.php

File diff suppressed because it is too large Load Diff

169
framework/caching/DbCache.php

@ -7,6 +7,7 @@
namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Query;
@ -14,30 +15,20 @@ use yii\db\Query;
/**
* DbCache implements a cache application component by storing cached data in a database.
*
* DbCache stores cache data in a DB table whose name is specified via [[cacheTableName]].
* For MySQL database, the table should be created beforehand as follows :
*
* ~~~
* CREATE TABLE tbl_cache (
* id char(128) NOT NULL,
* expire int(11) DEFAULT NULL,
* data LONGBLOB,
* PRIMARY KEY (id),
* KEY expire (expire)
* );
* ~~~
*
* You should replace `LONGBLOB` as follows if you are using a different DBMS:
*
* - PostgreSQL: `BYTEA`
* - SQLite, SQL server, Oracle: `BLOB`
*
* DbCache connects to the database via the DB connection specified in [[connectionID]]
* which must refer to a valid DB application component.
* By default, DbCache stores session data in a DB table named 'tbl_cache'. This table
* must be pre-created. The table name can be changed by setting [[cacheTable]].
*
* Please refer to [[Cache]] for common cache operations that are supported by DbCache.
*
* @property Connection $db The DB connection instance.
* The following example shows how you can configure the application to use DbCache:
*
* ~~~
* 'cache' => array(
* 'class' => 'yii\caching\DbCache',
* // 'db' => 'mydb',
* // 'cacheTable' => 'my_cache',
* )
* ~~~
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -45,50 +36,56 @@ use yii\db\Query;
class DbCache extends Cache
{
/**
* @var string the ID of the [[Connection|DB connection]] application component. Defaults to 'db'.
* @var Connection|string the DB connection object or the application component ID of the DB connection.
* After the DbCache object is created, if you want to change this property, you should only assign it
* with a DB connection object.
*/
public $connectionID = 'db';
public $db = 'db';
/**
* @var string name of the DB table to store cache content. Defaults to 'tbl_cache'.
* The table must be created before using this cache component.
* @var string name of the DB table to store cache content.
* The table should be pre-created as follows:
*
* ~~~
* CREATE TABLE tbl_cache (
* id char(128) NOT NULL PRIMARY KEY,
* expire int(11),
* data BLOB
* );
* ~~~
*
* where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
* that can be used for some popular DBMS:
*
* - MySQL: LONGBLOB
* - PostgreSQL: BYTEA
* - MSSQL: BLOB
*
* When using DbCache in a production server, we recommend you create a DB index for the 'expire'
* column in the cache table to improve the performance.
*/
public $cacheTableName = 'tbl_cache';
public $cacheTable = 'tbl_cache';
/**
* @var integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
**/
public $gcProbability = 100;
/**
* @var Connection the DB connection instance
*/
private $_db;
/**
* Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance
* @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/
public function getDb()
{
if ($this->_db === null) {
$db = \Yii::$app->getComponent($this->connectionID);
if ($db instanceof Connection) {
$this->_db = $db;
} else {
throw new InvalidConfigException("DbCache::connectionID must refer to the ID of a DB application component.");
}
}
return $this->_db;
}
/**
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
* Initializes the DbCache component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function setDb($value)
public function init()
{
$this->_db = $value;
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbCache::db must be either a DB connection instance or the application component ID of a DB connection.");
}
}
/**
@ -101,17 +98,16 @@ class DbCache extends Cache
{
$query = new Query;
$query->select(array('data'))
->from($this->cacheTableName)
->from($this->cacheTable)
->where('id = :id AND (expire = 0 OR expire >' . time() . ')', array(':id' => $key));
$db = $this->getDb();
if ($db->enableQueryCache) {
if ($this->db->enableQueryCache) {
// temporarily disable and re-enable query caching
$db->enableQueryCache = false;
$result = $query->createCommand($db)->queryScalar();
$db->enableQueryCache = true;
$this->db->enableQueryCache = false;
$result = $query->createCommand($this->db)->queryScalar();
$this->db->enableQueryCache = true;
return $result;
} else {
return $query->createCommand($db)->queryScalar();
return $query->createCommand($this->db)->queryScalar();
}
}
@ -127,17 +123,16 @@ class DbCache extends Cache
}
$query = new Query;
$query->select(array('id', 'data'))
->from($this->cacheTableName)
->from($this->cacheTable)
->where(array('id' => $keys))
->andWhere('(expire = 0 OR expire > ' . time() . ')');
$db = $this->getDb();
if ($db->enableQueryCache) {
$db->enableQueryCache = false;
$rows = $query->createCommand($db)->queryAll();
$db->enableQueryCache = true;
if ($this->db->enableQueryCache) {
$this->db->enableQueryCache = false;
$rows = $query->createCommand($this->db)->queryAll();
$this->db->enableQueryCache = true;
} else {
$rows = $query->createCommand($db)->queryAll();
$rows = $query->createCommand($this->db)->queryAll();
}
$results = array();
@ -161,13 +156,13 @@ class DbCache extends Cache
*/
protected function setValue($key, $value, $expire)
{
$command = $this->getDb()->createCommand();
$command->update($this->cacheTableName, array(
'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB),
), array(
'id' => $key,
));;
$command = $this->db->createCommand()
->update($this->cacheTable, array(
'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB),
), array(
'id' => $key,
));
if ($command->execute()) {
$this->gc();
@ -196,14 +191,13 @@ class DbCache extends Cache
$expire = 0;
}
$command = $this->getDb()->createCommand();
$command->insert($this->cacheTableName, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
));
try {
$command->execute();
$this->db->createCommand()
->insert($this->cacheTable, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
))->execute();
return true;
} catch (\Exception $e) {
return false;
@ -218,8 +212,9 @@ class DbCache extends Cache
*/
protected function deleteValue($key)
{
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName, array('id' => $key))->execute();
$this->db->createCommand()
->delete($this->cacheTable, array('id' => $key))
->execute();
return true;
}
@ -231,8 +226,9 @@ class DbCache extends Cache
public function gc($force = false)
{
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())->execute();
$this->db->createCommand()
->delete($this->cacheTable, 'expire > 0 AND expire < ' . time())
->execute();
}
}
@ -243,8 +239,9 @@ class DbCache extends Cache
*/
protected function flushValues()
{
$command = $this->getDb()->createCommand();
$command->delete($this->cacheTableName)->execute();
$this->db->createCommand()
->delete($this->cacheTable)
->execute();
return true;
}
}

53
framework/caching/DbDependency.php

@ -23,9 +23,9 @@ use yii\db\Connection;
class DbDependency extends Dependency
{
/**
* @var string the ID of the [[Connection|DB connection]] application component. Defaults to 'db'.
* @var string the application component ID of the DB connection.
*/
public $connectionID = 'db';
public $db = 'db';
/**
* @var string the SQL query whose result is used to determine if the dependency has been changed.
* Only the first row of the query result will be used.
@ -50,24 +50,17 @@ class DbDependency extends Dependency
}
/**
* 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 generateDependencyData()
{
$db = $this->getDb();
$db = Yii::$app->getComponent($this->db);
if (!$db instanceof Connection) {
throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection.");
}
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching
$db->enableQueryCache = false;
@ -78,36 +71,4 @@ class DbDependency extends Dependency
}
return $result;
}
/**
* @var Connection the DB connection instance
*/
private $_db;
/**
* Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance
* @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/
public function getDb()
{
if ($this->_db === null) {
$db = Yii::$app->getComponent($this->connectionID);
if ($db instanceof Connection) {
$this->_db = $db;
} else {
throw new InvalidConfigException("DbCacheDependency::connectionID must refer to the ID of a DB application component.");
}
}
return $this->_db;
}
/**
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
*/
public function setDb($value)
{
$this->_db = $value;
}
}

6
framework/console/Controller.php

@ -135,9 +135,13 @@ class Controller extends \yii\base\Controller
/**
* Returns the names of the global options for this command.
* A global option requires the existence of a global member variable whose
* A global option requires the existence of a public member variable whose
* name is the option name.
* Child classes may override this method to specify possible global options.
*
* Note that the values setting via global options are not available
* until [[beforeAction()]] is being called.
*
* @return array the names of the global options for this command.
*/
public function globalOptions()

1281
framework/console/controllers/MigrateController.php

File diff suppressed because it is too large Load Diff

34
framework/db/Command.php

@ -7,7 +7,9 @@
namespace yii\db;
use Yii;
use yii\base\NotSupportedException;
use yii\caching\Cache;
/**
* Command represents a SQL statement to be executed against a database.
@ -132,7 +134,7 @@ class Command extends \yii\base\Component
try {
$this->pdoStatement = $this->db->pdo->prepare($sql);
} catch (\Exception $e) {
\Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__);
Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode());
}
@ -264,7 +266,7 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
if ($sql == '') {
return 0;
@ -272,7 +274,7 @@ class Command extends \yii\base\Component
try {
if ($this->db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
}
$this->prepare();
@ -280,16 +282,16 @@ class Command extends \yii\base\Component
$n = $this->pdoStatement->rowCount();
if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
return $n;
} catch (\Exception $e) {
if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
$message = $e->getMessage();
\Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __CLASS__);
Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode());
@ -381,14 +383,14 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
/** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') {
$cache = \Yii::$app->getComponent($db->queryCacheID);
$cache = is_string($db->queryCache) ? Yii::$app->getComponent($db->queryCache) : $db->queryCache;
}
if (isset($cache)) {
if (isset($cache) && $cache instanceof Cache) {
$cacheKey = $cache->buildKey(array(
__CLASS__,
$db->dsn,
@ -397,14 +399,14 @@ class Command extends \yii\base\Component
$paramLog,
));
if (($result = $cache->get($cacheKey)) !== false) {
\Yii::trace('Query result found in cache', __CLASS__);
Yii::trace('Query result served from cache', __CLASS__);
return $result;
}
}
try {
if ($db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
}
$this->prepare();
@ -421,21 +423,21 @@ class Command extends \yii\base\Component
}
if ($db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
if (isset($cache, $cacheKey)) {
if (isset($cache, $cacheKey) && $cache instanceof Cache) {
$cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency);
\Yii::trace('Saved query result in cache', __CLASS__);
Yii::trace('Saved query result in cache', __CLASS__);
}
return $result;
} catch (\Exception $e) {
if ($db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
$message = $e->getMessage();
\Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __CLASS__);
Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode());
}

25
framework/db/Connection.php

@ -10,6 +10,7 @@ namespace yii\db;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\caching\Cache;
/**
* Connection represents a connection to a database via [PDO](http://www.php.net/manual/en/ref.pdo.php).
@ -136,10 +137,10 @@ class Connection extends Component
/**
* @var boolean whether to enable schema caching.
* Note that in order to enable truly schema caching, a valid cache component as specified
* by [[schemaCacheID]] must be enabled and [[enableSchemaCache]] must be set true.
* by [[schemaCache]] must be enabled and [[enableSchemaCache]] must be set true.
* @see schemaCacheDuration
* @see schemaCacheExclude
* @see schemaCacheID
* @see schemaCache
*/
public $enableSchemaCache = false;
/**
@ -155,20 +156,20 @@ class Connection extends Component
*/
public $schemaCacheExclude = array();
/**
* @var string the ID of the cache application component that is used to cache the table metadata.
* Defaults to 'cache'.
* @var Cache|string the cache object or the ID of the cache application component that
* is used to cache the table metadata.
* @see enableSchemaCache
*/
public $schemaCacheID = 'cache';
public $schemaCache = 'cache';
/**
* @var boolean whether to enable query caching.
* Note that in order to enable query caching, a valid cache component as specified
* by [[queryCacheID]] must be enabled and [[enableQueryCache]] must be set true.
* by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true.
*
* Methods [[beginCache()]] and [[endCache()]] can be used as shortcuts to turn on
* and off query caching on the fly.
* @see queryCacheDuration
* @see queryCacheID
* @see queryCache
* @see queryCacheDependency
* @see beginCache()
* @see endCache()
@ -176,7 +177,7 @@ class Connection extends Component
public $enableQueryCache = false;
/**
* @var integer number of seconds that query results can remain valid in cache.
* Defaults to 3600, meaning one hour.
* Defaults to 3600, meaning 3600 seconds, or one hour.
* Use 0 to indicate that the cached data will never expire.
* @see enableQueryCache
*/
@ -188,11 +189,11 @@ class Connection extends Component
*/
public $queryCacheDependency;
/**
* @var string the ID of the cache application component that is used for query caching.
* Defaults to 'cache'.
* @var Cache|string the cache object or the ID of the cache application component
* that is used for query caching.
* @see enableQueryCache
*/
public $queryCacheID = 'cache';
public $queryCache = 'cache';
/**
* @var string the charset used for database connection. The property is only used
* for MySQL and PostgreSQL databases. Defaults to null, meaning using default charset
@ -290,7 +291,7 @@ class Connection extends Component
* This method is provided as a shortcut to setting two properties that are related
* with query caching: [[queryCacheDuration]] and [[queryCacheDependency]].
* @param integer $duration the number of seconds that query results may remain valid in cache.
* See [[queryCacheDuration]] for more details.
* If not set, it will use the value of [[queryCacheDuration]]. See [[queryCacheDuration]] for more details.
* @param \yii\caching\Dependency $dependency the dependency for the cached query result.
* See [[queryCacheDependency]] for more details.
*/

30
framework/db/Schema.php

@ -7,6 +7,7 @@
namespace yii\db;
use Yii;
use yii\base\NotSupportedException;
use yii\base\InvalidCallException;
use yii\caching\Cache;
@ -84,21 +85,21 @@ abstract class Schema extends \yii\base\Object
$db = $this->db;
$realName = $this->getRealTableName($name);
/** @var $cache Cache */
if ($db->enableSchemaCache && ($cache = \Yii::$app->getComponent($db->schemaCacheID)) !== null && !in_array($name, $db->schemaCacheExclude, true)) {
$key = $this->getCacheKey($cache, $name);
if ($refresh || ($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName);
if ($table !== null) {
$cache->set($key, $table, $db->schemaCacheDuration);
if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
/** @var $cache Cache */
$cache = is_string($db->schemaCache) ? Yii::$app->getComponent($db->schemaCache) : $db->schemaCache;
if ($cache instanceof Cache) {
$key = $this->getCacheKey($cache, $name);
if ($refresh || ($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName);
if ($table !== null) {
$cache->set($key, $table, $db->schemaCacheDuration);
}
}
return $this->_tables[$name] = $table;
}
$this->_tables[$name] = $table;
} else {
$this->_tables[$name] = $table = $this->loadTableSchema($realName);
}
return $table;
return $this->_tables[$name] = $table = $this->loadTableSchema($realName);
}
/**
@ -173,8 +174,9 @@ abstract class Schema extends \yii\base\Object
*/
public function refresh()
{
/** @var $cache \yii\caching\Cache */
if ($this->db->enableSchemaCache && ($cache = \Yii::$app->getComponent($this->db->schemaCacheID)) !== null) {
/** @var $cache Cache */
$cache = is_string($this->db->schemaCache) ? Yii::$app->getComponent($this->db->schemaCache) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof Cache) {
foreach ($this->_tables as $name => $table) {
$cache->delete($this->getCacheKey($cache, $name));
}

67
framework/logging/DbTarget.php

@ -7,16 +7,15 @@
namespace yii\logging;
use Yii;
use yii\db\Connection;
use yii\base\InvalidConfigException;
/**
* DbTarget stores log messages in a database table.
*
* By default, DbTarget will use the database specified by [[connectionID]] and save
* messages into a table named by [[tableName]]. Please refer to [[tableName]] for the required
* table structure. Note that this table must be created beforehand. Otherwise an exception
* will be thrown when DbTarget is saving messages into DB.
* By default, DbTarget stores the log messages in a DB table named 'tbl_log'. This table
* must be pre-created. The table name can be changed by setting [[logTable]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -24,20 +23,18 @@ use yii\base\InvalidConfigException;
class DbTarget extends Target
{
/**
* @var string the ID of [[Connection]] application component.
* Defaults to 'db'. Please make sure that your database contains a table
* whose name is as specified in [[tableName]] and has the required table structure.
* @see tableName
* @var Connection|string the DB connection object or the application component ID of the DB connection.
* After the DbTarget object is created, if you want to change this property, you should only assign it
* with a DB connection object.
*/
public $connectionID = 'db';
public $db = 'db';
/**
* @var string the name of the DB table that stores log messages. Defaults to 'tbl_log'.
*
* The DB table should have the following structure:
* @var string name of the DB table to store cache content.
* The table should be pre-created as follows:
*
* ~~~
* CREATE TABLE tbl_log (
* id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
* id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
* level INTEGER,
* category VARCHAR(255),
* log_time INTEGER,
@ -48,42 +45,29 @@ class DbTarget extends Target
* ~~~
*
* Note that the 'id' column must be created as an auto-incremental column.
* The above SQL shows the syntax of MySQL. If you are using other DBMS, you need
* The above SQL uses the MySQL syntax. If you are using other DBMS, you need
* to adjust it accordingly. For example, in PostgreSQL, it should be `id SERIAL PRIMARY KEY`.
*
* The indexes declared above are not required. They are mainly used to improve the performance
* of some queries about message levels and categories. Depending on your actual needs, you may
* want to create additional indexes (e.g. index on log_time).
* want to create additional indexes (e.g. index on `log_time`).
*/
public $tableName = 'tbl_log';
private $_db;
public $logTable = 'tbl_log';
/**
* Returns the DB connection used for saving log messages.
* @return Connection the DB connection instance
* @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
* Initializes the DbTarget component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function getDb()
public function init()
{
if ($this->_db === null) {
$db = \Yii::$app->getComponent($this->connectionID);
if ($db instanceof Connection) {
$this->_db = $db;
} else {
throw new InvalidConfigException("DbTarget::connectionID must refer to the ID of a DB application component.");
}
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbTarget::db must be either a DB connection instance or the application component ID of a DB connection.");
}
return $this->_db;
}
/**
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
*/
public function setDb($value)
{
$this->_db = $value;
}
/**
@ -93,10 +77,9 @@ class DbTarget extends Target
*/
public function export($messages)
{
$db = $this->getDb();
$tableName = $db->quoteTableName($this->tableName);
$tableName = $this->db->quoteTableName($this->logTable);
$sql = "INSERT INTO $tableName (level, category, log_time, message) VALUES (:level, :category, :log_time, :message)";
$command = $db->createCommand($sql);
$command = $this->db->createCommand($sql);
foreach ($messages as $message) {
$command->bindValues(array(
':level' => $message[1],

58
framework/web/CacheSession.php

@ -15,7 +15,7 @@ use yii\base\InvalidConfigException;
* CacheSession implements a session component using cache as storage medium.
*
* The cache being used can be any cache application component.
* The ID of the cache application component is specified via [[cacheID]], which defaults to 'cache'.
* The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
*
* Beware, by definition cache storage are volatile, which means the data stored on them
* may be swapped out and get lost. Therefore, you must make sure the cache used by this component
@ -27,14 +27,27 @@ use yii\base\InvalidConfigException;
class CacheSession extends Session
{
/**
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
* @var Cache|string the cache object or the application component ID of the cache object.
* The session data will be stored using this cache object.
*
* After the CacheSession object is created, if you want to change this property,
* you should only assign it with a cache object.
*/
public $cacheID = 'cache';
public $cache = 'cache';
/**
* @var Cache the cache component
* Initializes the application component.
*/
private $_cache;
public function init()
{
parent::init();
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
}
if (!$this->cache instanceof Cache) {
throw new InvalidConfigException('CacheSession::cache must refer to the application component ID of a cache object.');
}
}
/**
* Returns a value indicating whether to use custom session storage.
@ -47,33 +60,6 @@ class CacheSession extends Session
}
/**
* Returns the cache instance used for storing session data.
* @return Cache the cache instance
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component.
*/
public function getCache()
{
if ($this->_cache === null) {
$cache = Yii::$app->getComponent($this->cacheID);
if ($cache instanceof Cache) {
$this->_cache = $cache;
} else {
throw new InvalidConfigException('CacheSession::cacheID must refer to the ID of a cache application component.');
}
}
return $this->_cache;
}
/**
* Sets the cache instance used by the session component.
* @param Cache $value the cache instance
*/
public function setCache($value)
{
$this->_cache = $value;
}
/**
* Session read handler.
* Do not call this method directly.
* @param string $id session ID
@ -81,7 +67,7 @@ class CacheSession extends Session
*/
public function readSession($id)
{
$data = $this->getCache()->get($this->calculateKey($id));
$data = $this->cache->get($this->calculateKey($id));
return $data === false ? '' : $data;
}
@ -94,7 +80,7 @@ class CacheSession extends Session
*/
public function writeSession($id, $data)
{
return $this->getCache()->set($this->calculateKey($id), $data, $this->getTimeout());
return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
}
/**
@ -105,7 +91,7 @@ class CacheSession extends Session
*/
public function destroySession($id)
{
return $this->getCache()->delete($this->calculateKey($id));
return $this->cache->delete($this->calculateKey($id));
}
/**
@ -115,6 +101,6 @@ class CacheSession extends Session
*/
protected function calculateKey($id)
{
return $this->getCache()->buildKey(array(__CLASS__, $id));
return $this->cache->buildKey(array(__CLASS__, $id));
}
}

170
framework/web/DbSession.php

@ -15,58 +15,70 @@ use yii\base\InvalidConfigException;
/**
* DbSession extends [[Session]] by using database as session data storage.
*
* DbSession uses a DB application component to perform DB operations. The ID of the DB application
* component is specified via [[connectionID]] which defaults to 'db'.
*
* By default, DbSession stores session data in a DB table named 'tbl_session'. This table
* must be pre-created. The table name can be changed by setting [[sessionTableName]].
* The table should have the following structure:
*
* must be pre-created. The table name can be changed by setting [[sessionTable]].
*
* The following example shows how you can configure the application to use DbSession:
*
* ~~~
* CREATE TABLE tbl_session
* (
* id CHAR(32) PRIMARY KEY,
* expire INTEGER,
* data BLOB
* 'session' => array(
* 'class' => 'yii\web\DbSession',
* // 'db' => 'mydb',
* // 'sessionTable' => 'my_session',
* )
* ~~~
*
* where 'BLOB' refers to the BLOB-type of your preferred database. Below are the BLOB type
* that can be used for some popular databases:
*
* - MySQL: LONGBLOB
* - PostgreSQL: BYTEA
* - MSSQL: BLOB
*
* When using DbSession in a production server, we recommend you create a DB index for the 'expire'
* column in the session table to improve the performance.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DbSession extends Session
{
/**
* @var string the ID of a {@link CDbConnection} application component. If not set, a SQLite database
* will be automatically created and used. The SQLite database file is
* is <code>protected/runtime/session-YiiVersion.db</code>.
* @var Connection|string the DB connection object or the application component ID of the DB connection.
* After the DbSession object is created, if you want to change this property, you should only assign it
* with a DB connection object.
*/
public $connectionID;
public $db = 'db';
/**
* @var string the name of the DB table to store session content.
* Note, if {@link autoCreateSessionTable} is false and you want to create the DB table manually by yourself,
* you need to make sure the DB table is of the following structure:
* <pre>
* (id CHAR(32) PRIMARY KEY, expire INTEGER, data BLOB)
* </pre>
* @see autoCreateSessionTable
* @var string the name of the DB table that stores the session data.
* The table should be pre-created as follows:
*
* ~~~
* CREATE TABLE tbl_session
* (
* id CHAR(40) NOT NULL PRIMARY KEY,
* expire INTEGER,
* data BLOB
* )
* ~~~
*
* where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
* that can be used for some popular DBMS:
*
* - MySQL: LONGBLOB
* - PostgreSQL: BYTEA
* - MSSQL: BLOB
*
* When using DbSession in a production server, we recommend you create a DB index for the 'expire'
* column in the session table to improve the performance.
*/
public $sessionTableName = 'tbl_session';
public $sessionTable = 'tbl_session';
/**
* @var Connection the DB connection instance
* Initializes the DbSession component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
private $_db;
public function init()
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbSession::db must be either a DB connection instance or the application component ID of a DB connection.");
}
}
/**
* Returns a value indicating whether to use custom session storage.
@ -94,56 +106,31 @@ class DbSession extends Session
parent::regenerateID(false);
$newID = session_id();
$db = $this->getDb();
$query = new Query;
$row = $query->from($this->sessionTableName)
$row = $query->from($this->sessionTable)
->where(array('id' => $oldID))
->createCommand($db)
->createCommand($this->db)
->queryRow();
if ($row !== false) {
if ($deleteOldSession) {
$db->createCommand()->update($this->sessionTableName, array(
'id' => $newID
), array('id' => $oldID))->execute();
$this->db->createCommand()
->update($this->sessionTable, array('id' => $newID), array('id' => $oldID))
->execute();
} else {
$row['id'] = $newID;
$db->createCommand()->insert($this->sessionTableName, $row)->execute();
$this->db->createCommand()
->insert($this->sessionTable, $row)
->execute();
}
} else {
// shouldn't reach here normally
$db->createCommand()->insert($this->sessionTableName, array(
'id' => $newID,
'expire' => time() + $this->getTimeout(),
))->execute();
}
}
/**
* Returns the DB connection instance used for storing session data.
* @return Connection the DB connection instance
* @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/
public function getDb()
{
if ($this->_db === null) {
$db = Yii::$app->getComponent($this->connectionID);
if ($db instanceof Connection) {
$this->_db = $db;
} else {
throw new InvalidConfigException("DbSession::connectionID must refer to the ID of a DB application component.");
}
$this->db->createCommand()
->insert($this->sessionTable, array(
'id' => $newID,
'expire' => time() + $this->getTimeout(),
))->execute();
}
return $this->_db;
}
/**
* Sets the DB connection used by the session component.
* @param Connection $value the DB connection instance
*/
public function setDb($value)
{
$this->_db = $value;
}
/**
@ -156,9 +143,9 @@ class DbSession extends Session
{
$query = new Query;
$data = $query->select(array('data'))
->from($this->sessionTableName)
->from($this->sessionTable)
->where('expire>:expire AND id=:id', array(':expire' => time(), ':id' => $id))
->createCommand($this->getDb())
->createCommand($this->db)
->queryScalar();
return $data === false ? '' : $data;
}
@ -176,24 +163,23 @@ class DbSession extends Session
// http://us.php.net/manual/en/function.session-set-save-handler.php
try {
$expire = time() + $this->getTimeout();
$db = $this->getDb();
$query = new Query;
$exists = $query->select(array('id'))
->from($this->sessionTableName)
->from($this->sessionTable)
->where(array('id' => $id))
->createCommand($db)
->createCommand($this->db)
->queryScalar();
if ($exists === false) {
$db->createCommand()->insert($this->sessionTableName, array(
'id' => $id,
'data' => $data,
'expire' => $expire,
))->execute();
$this->db->createCommand()
->insert($this->sessionTable, array(
'id' => $id,
'data' => $data,
'expire' => $expire,
))->execute();
} else {
$db->createCommand()->update($this->sessionTableName, array(
'data' => $data,
'expire' => $expire
), array('id' => $id))->execute();
$this->db->createCommand()
->update($this->sessionTable, array('data' => $data, 'expire' => $expire), array('id' => $id))
->execute();
}
} catch (\Exception $e) {
if (YII_DEBUG) {
@ -213,8 +199,8 @@ class DbSession extends Session
*/
public function destroySession($id)
{
$this->getDb()->createCommand()
->delete($this->sessionTableName, array('id' => $id))
$this->db->createCommand()
->delete($this->sessionTable, array('id' => $id))
->execute();
return true;
}
@ -227,8 +213,8 @@ class DbSession extends Session
*/
public function gcSession($maxLifetime)
{
$this->getDb()->createCommand()
->delete($this->sessionTableName, 'expire<:expire', array(':expire' => time()))
$this->db->createCommand()
->delete($this->sessionTable, 'expire<:expire', array(':expire' => time()))
->execute();
return true;
}

217
framework/web/PageCache.php

@ -1,110 +1,109 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use Yii;
use yii\base\ActionFilter;
use yii\base\Action;
use yii\base\View;
use yii\caching\Dependency;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class PageCache extends ActionFilter
{
/**
* @var boolean whether the content being cached should be differentiated according to the route.
* A route consists of the requested controller ID and action ID. Defaults to true.
*/
public $varyByRoute = true;
/**
* @var View the view object that is used to create the fragment cache widget to implement page caching.
* If not set, the view registered with the application will be used.
*/
public $view;
/**
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
*/
public $cacheID = 'cache';
/**
* @var integer number of seconds that the data can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
*/
public $duration = 60;
/**
* @var array|Dependency the dependency that the cached content depends on.
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
* For example,
*
* ~~~
* array(
* 'class' => 'yii\caching\DbDependency',
* 'sql' => 'SELECT MAX(lastModified) FROM Post',
* )
* ~~~
*
* would make the output cache depends on the last modified time of all posts.
* If any post has its modification time changed, the cached content would be invalidated.
*/
public $dependency;
/**
* @var array list of factors that would cause the variation of the content being cached.
* Each factor is a string representing a variation (e.g. the language, a GET parameter).
* The following variation setting will cause the content to be cached in different versions
* according to the current application language:
*
* ~~~
* array(
* Yii::$app->language,
* )
*/
public $variations;
/**
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/
public $enabled = true;
public function init()
{
parent::init();
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param Action $action the action to be executed.
* @return boolean whether the action should continue to be executed.
*/
public function beforeAction($action)
{
$properties = array();
foreach (array('cacheID', 'duration', 'dependency', 'variations', 'enabled') as $name) {
$properties[$name] = $this->$name;
}
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__;
return $this->view->beginCache($id, $properties);
}
/**
* This method is invoked right after an action is executed.
* You may override this method to do some postprocessing for the action.
* @param Action $action the action just executed.
*/
public function afterAction($action)
{
$this->view->endCache();
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use Yii;
use yii\base\ActionFilter;
use yii\base\Action;
use yii\base\View;
use yii\caching\Dependency;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class PageCache extends ActionFilter
{
/**
* @var boolean whether the content being cached should be differentiated according to the route.
* A route consists of the requested controller ID and action ID. Defaults to true.
*/
public $varyByRoute = true;
/**
* @var View the view object that is used to create the fragment cache widget to implement page caching.
* If not set, the view registered with the application will be used.
*/
public $view;
/**
* @var string the application component ID of the [[\yii\caching\Cache|cache]] object.
*/
public $cache = 'cache';
/**
* @var integer number of seconds that the data can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
*/
public $duration = 60;
/**
* @var array|Dependency the dependency that the cached content depends on.
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
* For example,
*
* ~~~
* array(
* 'class' => 'yii\caching\DbDependency',
* 'sql' => 'SELECT MAX(lastModified) FROM Post',
* )
* ~~~
*
* would make the output cache depends on the last modified time of all posts.
* If any post has its modification time changed, the cached content would be invalidated.
*/
public $dependency;
/**
* @var array list of factors that would cause the variation of the content being cached.
* Each factor is a string representing a variation (e.g. the language, a GET parameter).
* The following variation setting will cause the content to be cached in different versions
* according to the current application language:
*
* ~~~
* array(
* Yii::$app->language,
* )
*/
public $variations;
/**
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/
public $enabled = true;
public function init()
{
parent::init();
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param Action $action the action to be executed.
* @return boolean whether the action should continue to be executed.
*/
public function beforeAction($action)
{
$properties = array();
foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) {
$properties[$name] = $this->$name;
}
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__;
return $this->view->beginCache($id, $properties);
}
/**
* This method is invoked right after an action is executed.
* You may override this method to do some postprocessing for the action.
* @param Action $action the action just executed.
*/
public function afterAction($action)
{
$this->view->endCache();
}
}

30
framework/web/UrlManager.php

@ -9,6 +9,7 @@ namespace yii\web;
use Yii;
use yii\base\Component;
use yii\caching\Cache;
/**
* UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
@ -49,11 +50,14 @@ class UrlManager extends Component
*/
public $routeVar = 'r';
/**
* @var string the ID of the cache component that is used to cache the parsed URL rules.
* Defaults to 'cache' which refers to the primary cache component registered with the application.
* Set this property to false if you do not want to cache the URL rules.
* @var Cache|string the cache object or the application component ID of the cache object.
* Compiled URL rules will be cached through this cache object, if it is available.
*
* After the UrlManager object is created, if you want to change this property,
* you should only assign it with a cache object.
* Set this property to null if you do not want to cache the URL rules.
*/
public $cacheID = 'cache';
public $cache = 'cache';
/**
* @var string the default class name for creating URL rule instances
* when it is not specified in [[rules]].
@ -65,11 +69,14 @@ class UrlManager extends Component
/**
* Initializes the application component.
* Initializes UrlManager.
*/
public function init()
{
parent::init();
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
}
$this->compileRules();
}
@ -81,13 +88,10 @@ class UrlManager extends Component
if (!$this->enablePrettyUrl || $this->rules === array()) {
return;
}
/**
* @var $cache \yii\caching\Cache
*/
if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
$key = $cache->buildKey(__CLASS__);
if ($this->cache instanceof Cache) {
$key = $this->cache->buildKey(__CLASS__);
$hash = md5(json_encode($this->rules));
if (($data = $cache->get($key)) !== false && isset($data[1]) && $data[1] === $hash) {
if (($data = $this->cache->get($key)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
return;
}
@ -100,8 +104,8 @@ class UrlManager extends Component
$this->rules[$i] = Yii::createObject($rule);
}
if (isset($cache)) {
$cache->set($key, array($this->rules, $hash));
if ($this->cache instanceof Cache) {
$this->cache->set($key, array($this->rules, $hash));
}
}

395
framework/widgets/FragmentCache.php

@ -1,213 +1,184 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\caching\Cache;
use yii\caching\Dependency;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FragmentCache extends Widget
{
/**
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
*/
public $cacheID = 'cache';
/**
* @var integer number of seconds that the data can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
*/
public $duration = 60;
/**
* @var array|Dependency the dependency that the cached content depends on.
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
* For example,
*
* ~~~
* array(
* 'class' => 'yii\caching\DbDependency',
* 'sql' => 'SELECT MAX(lastModified) FROM Post',
* )
* ~~~
*
* would make the output cache depends on the last modified time of all posts.
* If any post has its modification time changed, the cached content would be invalidated.
*/
public $dependency;
/**
* @var array list of factors that would cause the variation of the content being cached.
* Each factor is a string representing a variation (e.g. the language, a GET parameter).
* The following variation setting will cause the content to be cached in different versions
* according to the current application language:
*
* ~~~
* array(
* Yii::$app->language,
* )
*/
public $variations;
/**
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/
public $enabled = true;
/**
* @var \yii\base\View the view object within which this widget is used. If not set,
* the view registered with the application will be used. This is mainly used by dynamic content feature.
*/
public $view;
/**
* @var array a list of placeholders for embedding dynamic contents. This property
* is used internally to implement the content caching feature. Do not modify it.
*/
public $dynamicPlaceholders;
/**
* Marks the start of content to be cached.
* Content displayed after this method call and before {@link endCache()}
* will be captured and saved in cache.
* This method does nothing if valid content is already found in cache.
*/
public function init()
{
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
if ($this->getCache() !== null && $this->getCachedContent() === false) {
$this->view->cacheStack[] = $this;
ob_start();
ob_implicit_flush(false);
}
}
/**
* Marks the end of content to be cached.
* Content displayed before this method call and after {@link init()}
* will be captured and saved in cache.
* This method does nothing if valid content is already found in cache.
*/
public function run()
{
if (($content = $this->getCachedContent()) !== false) {
echo $content;
} elseif (($cache = $this->getCache()) !== null) {
$content = ob_get_clean();
array_pop($this->view->cacheStack);
if (is_array($this->dependency)) {
$this->dependency = Yii::createObject($this->dependency);
}
$data = array($content, $this->dynamicPlaceholders);
$cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) {
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
}
echo $content;
}
}
/**
* @var string|boolean the cached content. False if the content is not cached.
*/
private $_content;
/**
* Returns the cached content if available.
* @return string|boolean the cached content. False is returned if valid content is not found in the cache.
*/
public function getCachedContent()
{
if ($this->_content === null) {
$this->_content = false;
if (($cache = $this->getCache()) !== null) {
$key = $this->calculateKey();
$data = $cache->get($key);
if (is_array($data) && count($data) === 2) {
list ($content, $placeholders) = $data;
if (is_array($placeholders) && count($placeholders) > 0) {
if ($this->view->cacheStack === array()) {
// outermost cache: replace placeholder with dynamic content
$content = $this->updateDynamicContent($content, $placeholders);
}
foreach ($placeholders as $name => $statements) {
$this->view->addDynamicPlaceholder($name, $statements);
}
}
$this->_content = $content;
}
}
}
return $this->_content;
}
protected function updateDynamicContent($content, $placeholders)
{
foreach ($placeholders as $name => $statements) {
$placeholders[$name] = $this->view->evaluateDynamicContent($statements);
}
return strtr($content, $placeholders);
}
/**
* Generates a unique key used for storing the content in cache.
* The key generated depends on both [[id]] and [[variations]].
* @return string a valid cache key
*/
protected function calculateKey()
{
$factors = array(__CLASS__, $this->getId());
if (is_array($this->variations)) {
foreach ($this->variations as $factor) {
$factors[] = $factor;
}
}
return $this->getCache()->buildKey($factors);
}
/**
* @var Cache
*/
private $_cache;
/**
* Returns the cache instance used for storing content.
* @return Cache the cache instance. Null is returned if the cache component is not available
* or [[enabled]] is false.
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component.
*/
public function getCache()
{
if (!$this->enabled) {
return null;
}
if ($this->_cache === null) {
$cache = Yii::$app->getComponent($this->cacheID);
if ($cache instanceof Cache) {
$this->_cache = $cache;
} else {
throw new InvalidConfigException('FragmentCache::cacheID must refer to the ID of a cache application component.');
}
}
return $this->_cache;
}
/**
* Sets the cache instance used by the session component.
* @param Cache $value the cache instance
*/
public function setCache($value)
{
$this->_cache = $value;
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\caching\Cache;
use yii\caching\Dependency;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FragmentCache extends Widget
{
/**
* @var Cache|string the cache object or the application component ID of the cache object.
* After the FragmentCache object is created, if you want to change this property,
* you should only assign it with a cache object.
*/
public $cache = 'cache';
/**
* @var integer number of seconds that the data can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
*/
public $duration = 60;
/**
* @var array|Dependency the dependency that the cached content depends on.
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
* For example,
*
* ~~~
* array(
* 'class' => 'yii\caching\DbDependency',
* 'sql' => 'SELECT MAX(lastModified) FROM Post',
* )
* ~~~
*
* would make the output cache depends on the last modified time of all posts.
* If any post has its modification time changed, the cached content would be invalidated.
*/
public $dependency;
/**
* @var array list of factors that would cause the variation of the content being cached.
* Each factor is a string representing a variation (e.g. the language, a GET parameter).
* The following variation setting will cause the content to be cached in different versions
* according to the current application language:
*
* ~~~
* array(
* Yii::$app->language,
* )
*/
public $variations;
/**
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/
public $enabled = true;
/**
* @var \yii\base\View the view object within which this widget is used. If not set,
* the view registered with the application will be used. This is mainly used by dynamic content feature.
*/
public $view;
/**
* @var array a list of placeholders for embedding dynamic contents. This property
* is used internally to implement the content caching feature. Do not modify it.
*/
public $dynamicPlaceholders;
/**
* Initializes the FragmentCache object.
*/
public function init()
{
parent::init();
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
if (!$this->enabled) {
$this->cache = null;
} elseif (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
}
if ($this->getCachedContent() === false) {
$this->view->cacheStack[] = $this;
ob_start();
ob_implicit_flush(false);
}
}
/**
* Marks the end of content to be cached.
* Content displayed before this method call and after {@link init()}
* will be captured and saved in cache.
* This method does nothing if valid content is already found in cache.
*/
public function run()
{
if (($content = $this->getCachedContent()) !== false) {
echo $content;
} elseif ($this->cache instanceof Cache) {
$content = ob_get_clean();
array_pop($this->view->cacheStack);
if (is_array($this->dependency)) {
$this->dependency = Yii::createObject($this->dependency);
}
$data = array($content, $this->dynamicPlaceholders);
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) {
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
}
echo $content;
}
}
/**
* @var string|boolean the cached content. False if the content is not cached.
*/
private $_content;
/**
* Returns the cached content if available.
* @return string|boolean the cached content. False is returned if valid content is not found in the cache.
*/
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 ($this->view->cacheStack === array()) {
// outermost cache: replace placeholder with dynamic content
$content = $this->updateDynamicContent($content, $placeholders);
}
foreach ($placeholders as $name => $statements) {
$this->view->addDynamicPlaceholder($name, $statements);
}
}
$this->_content = $content;
}
}
}
return $this->_content;
}
protected function updateDynamicContent($content, $placeholders)
{
foreach ($placeholders as $name => $statements) {
$placeholders[$name] = $this->view->evaluateDynamicContent($statements);
}
return strtr($content, $placeholders);
}
/**
* Generates a unique key used for storing the content in cache.
* The key generated depends on both [[id]] and [[variations]].
* @return string a valid cache key
*/
protected function calculateKey()
{
$factors = array(__CLASS__, $this->getId());
if (is_array($this->variations)) {
foreach ($this->variations as $factor) {
$factors[] = $factor;
}
}
return $this->cache->buildKey($factors);
}
}

896
tests/unit/framework/util/HtmlTest.php

@ -1,448 +1,448 @@
<?php
namespace yiiunit\framework\util;
use Yii;
use yii\helpers\Html;
use yii\web\Application;
class HtmlTest extends \yii\test\TestCase
{
public function setUp()
{
new Application('test', '@yiiunit/runtime', array(
'components' => array(
'request' => array(
'class' => 'yii\web\Request',
'url' => '/test',
),
),
));
}
public function tearDown()
{
Yii::$app = null;
}
public function testEncode()
{
$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
}
public function testDecode()
{
$this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;"));
}
public function testTag()
{
$this->assertEquals('<br />', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = false;
$this->assertEquals('<br>', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = true;
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = false;
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = true;
}
public function testBeginTag()
{
$this->assertEquals('<br>', Html::beginTag('br'));
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title')));
}
public function testEndTag()
{
$this->assertEquals('</br>', Html::endTag('br'));
$this->assertEquals('</span>', Html::endTag('span'));
}
public function testCdata()
{
$data = 'test<>';
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data));
}
public function testStyle()
{
$content = 'a <>';
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content));
$this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less')));
}
public function testScript()
{
$content = 'a <>';
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content));
$this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js')));
}
public function testCssFile()
{
$this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com'));
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile(''));
}
public function testJsFile()
{
$this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile(''));
}
public function testBeginForm()
{
$this->assertEquals('<form action="/test" method="post">', Html::beginForm());
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
$hiddens = array(
'<input type="hidden" name="id" value="1" />',
'<input type="hidden" name="title" value="&lt;" />',
);
$this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get'));
}
public function testEndForm()
{
$this->assertEquals('</form>', Html::endForm());
}
public function testA()
{
$this->assertEquals('<a>something<></a>', Html::a('something<>'));
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example'));
$this->assertEquals('<a href="/test">something</a>', Html::a('something', ''));
}
public function testMailto()
{
$this->assertEquals('<a href="mailto:test&lt;&gt;">test<></a>', Html::mailto('test<>'));
$this->assertEquals('<a href="mailto:test&gt;">test<></a>', Html::mailto('test<>', 'test>'));
}
public function testImg()
{
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example'));
$this->assertEquals('<img src="/test" alt="" />', Html::img(''));
$this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10)));
}
public function testLabel()
{
$this->assertEquals('<label>something<></label>', Html::label('something<>'));
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a'));
$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test')));
}
public function testButton()
{
$this->assertEquals('<button type="button">Button</button>', Html::button());
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>'));
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t")));
}
public function testSubmitButton()
{
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton());
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't')));
}
public function testResetButton()
{
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton());
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't')));
}
public function testInput()
{
$this->assertEquals('<input type="text" />', Html::input('text'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't')));
}
public function testButtonInput()
{
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test'));
$this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a')));
}
public function testSubmitInput()
{
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput());
$this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a')));
}
public function testResetInput()
{
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput());
$this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a')));
}
public function testTextInput()
{
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't')));
}
public function testHiddenInput()
{
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test'));
$this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't')));
}
public function testPasswordInput()
{
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test'));
$this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't')));
}
public function testFileInput()
{
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test'));
$this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't')));
}
public function testTextarea()
{
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test'));
$this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', array('class' => 't')));
}
public function testRadio()
{
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0')));
}
public function testCheckbox()
{
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0')));
}
public function testDropDownList()
{
$expected = <<<EOD
<select name="test">
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test'));
$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems()));
$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
}
public function testListBox()
{
$expected = <<<EOD
<select name="test" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test'));
$expected = <<<EOD
<select name="test" size="5">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value 2">text&nbsp;&nbsp;2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
$expected = <<<EOD
<select name="test[]" multiple="multiple" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
}
public function testCheckboxList()
{
$this->assertEquals('', Html::checkboxList('test'));
$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br />
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n",
'unselect' => '0',
)));
$expected = <<<EOD
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
}
)));
}
public function testRadioList()
{
$this->assertEquals('', Html::radioList('test'));
$expected = <<<EOD
<label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br />
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n",
'unselect' => '0',
)));
$expected = <<<EOD
0<label>text1 <input type="radio" name="test" value="value1" /></label>
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
}
)));
}
public function testRenderOptions()
{
$data = array(
'value1' => 'label1',
'group1' => array(
'value11' => 'label11',
'group11' => array(
'value111' => 'label111',
),
'group12' => array(),
),
'value2' => 'label2',
'group2' => array(),
);
$expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option>
<option value="value1" selected="selected">label1</option>
<optgroup label="group1">
<option value="value11">label11</option>
<optgroup label="group11">
<option class="option" value="value111" selected="selected">label111</option>
</optgroup>
<optgroup class="group" label="group12">
</optgroup>
</optgroup>
<option value="value2">label2</option>
<optgroup label="group2">
</optgroup>
EOD;
$attributes = array(
'prompt' => 'please select<>',
'options' => array(
'value111' => array('class' => 'option'),
),
'groups' => array(
'group12' => array('class' => 'group'),
),
);
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
}
public function testRenderAttributes()
{
$this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Html::$showBooleanAttributeValues = false;
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Html::$showBooleanAttributeValues = true;
}
protected function getDataItems()
{
return array(
'value1' => 'text1',
'value2' => 'text2',
);
}
protected function getDataItems2()
{
return array(
'value1<>' => 'text1<>',
'value 2' => 'text 2',
);
}
}
<?php
namespace yiiunit\framework\util;
use Yii;
use yii\helpers\Html;
use yii\web\Application;
class HtmlTest extends \yii\test\TestCase
{
public function setUp()
{
new Application('test', '@yiiunit/runtime', array(
'components' => array(
'request' => array(
'class' => 'yii\web\Request',
'url' => '/test',
),
),
));
}
public function tearDown()
{
Yii::$app = null;
}
public function testEncode()
{
$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
}
public function testDecode()
{
$this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;"));
}
public function testTag()
{
$this->assertEquals('<br />', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = false;
$this->assertEquals('<br>', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = true;
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = false;
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = true;
}
public function testBeginTag()
{
$this->assertEquals('<br>', Html::beginTag('br'));
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title')));
}
public function testEndTag()
{
$this->assertEquals('</br>', Html::endTag('br'));
$this->assertEquals('</span>', Html::endTag('span'));
}
public function testCdata()
{
$data = 'test<>';
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data));
}
public function testStyle()
{
$content = 'a <>';
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content));
$this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less')));
}
public function testScript()
{
$content = 'a <>';
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content));
$this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js')));
}
public function testCssFile()
{
$this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com'));
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile(''));
}
public function testJsFile()
{
$this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile(''));
}
public function testBeginForm()
{
$this->assertEquals('<form action="/test" method="post">', Html::beginForm());
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
$hiddens = array(
'<input type="hidden" name="id" value="1" />',
'<input type="hidden" name="title" value="&lt;" />',
);
$this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get'));
}
public function testEndForm()
{
$this->assertEquals('</form>', Html::endForm());
}
public function testA()
{
$this->assertEquals('<a>something<></a>', Html::a('something<>'));
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example'));
$this->assertEquals('<a href="/test">something</a>', Html::a('something', ''));
}
public function testMailto()
{
$this->assertEquals('<a href="mailto:test&lt;&gt;">test<></a>', Html::mailto('test<>'));
$this->assertEquals('<a href="mailto:test&gt;">test<></a>', Html::mailto('test<>', 'test>'));
}
public function testImg()
{
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example'));
$this->assertEquals('<img src="/test" alt="" />', Html::img(''));
$this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10)));
}
public function testLabel()
{
$this->assertEquals('<label>something<></label>', Html::label('something<>'));
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a'));
$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test')));
}
public function testButton()
{
$this->assertEquals('<button type="button">Button</button>', Html::button());
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>'));
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t")));
}
public function testSubmitButton()
{
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton());
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't')));
}
public function testResetButton()
{
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton());
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't')));
}
public function testInput()
{
$this->assertEquals('<input type="text" />', Html::input('text'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't')));
}
public function testButtonInput()
{
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test'));
$this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a')));
}
public function testSubmitInput()
{
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput());
$this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a')));
}
public function testResetInput()
{
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput());
$this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a')));
}
public function testTextInput()
{
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't')));
}
public function testHiddenInput()
{
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test'));
$this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't')));
}
public function testPasswordInput()
{
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test'));
$this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't')));
}
public function testFileInput()
{
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test'));
$this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't')));
}
public function testTextarea()
{
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test'));
$this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', array('class' => 't')));
}
public function testRadio()
{
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0')));
}
public function testCheckbox()
{
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0')));
}
public function testDropDownList()
{
$expected = <<<EOD
<select name="test">
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test'));
$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems()));
$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
}
public function testListBox()
{
$expected = <<<EOD
<select name="test" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test'));
$expected = <<<EOD
<select name="test" size="5">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value 2">text&nbsp;&nbsp;2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
$expected = <<<EOD
<select name="test" size="4">
<option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
$expected = <<<EOD
<select name="test[]" multiple="multiple" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4">
</select>
EOD;
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
}
public function testCheckboxList()
{
$this->assertEquals('', Html::checkboxList('test'));
$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br />
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n",
'unselect' => '0',
)));
$expected = <<<EOD
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
}
)));
}
public function testRadioList()
{
$this->assertEquals('', Html::radioList('test'));
$expected = <<<EOD
<label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br />
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n",
'unselect' => '0',
)));
$expected = <<<EOD
0<label>text1 <input type="radio" name="test" value="value1" /></label>
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
}
)));
}
public function testRenderOptions()
{
$data = array(
'value1' => 'label1',
'group1' => array(
'value11' => 'label11',
'group11' => array(
'value111' => 'label111',
),
'group12' => array(),
),
'value2' => 'label2',
'group2' => array(),
);
$expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option>
<option value="value1" selected="selected">label1</option>
<optgroup label="group1">
<option value="value11">label11</option>
<optgroup label="group11">
<option class="option" value="value111" selected="selected">label111</option>
</optgroup>
<optgroup class="group" label="group12">
</optgroup>
</optgroup>
<option value="value2">label2</option>
<optgroup label="group2">
</optgroup>
EOD;
$attributes = array(
'prompt' => 'please select<>',
'options' => array(
'value111' => array('class' => 'option'),
),
'groups' => array(
'group12' => array('class' => 'group'),
),
);
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
}
public function testRenderAttributes()
{
$this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Html::$showBooleanAttributeValues = false;
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Html::$showBooleanAttributeValues = true;
}
protected function getDataItems()
{
return array(
'value1' => 'text1',
'value2' => 'text2',
);
}
protected function getDataItems2()
{
return array(
'value1<>' => 'text1<>',
'value 2' => 'text 2',
);
}
}

19
tests/unit/framework/web/UrlManagerTest.php

@ -11,6 +11,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// default setting with '/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/',
'cache' => null,
));
$url = $manager->createUrl('post/view');
$this->assertEquals('/?r=post/view', $url);
@ -20,6 +21,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// default setting with '/test/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/test/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/?r=post/view&id=1&title=sample+post', $url);
@ -28,18 +30,21 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/index.php',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url);
@ -49,7 +54,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cacheID' => false,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
@ -66,7 +71,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules and suffix
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cacheID' => false,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
@ -87,6 +92,7 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array(
'baseUrl' => '/',
'hostInfo' => 'http://www.example.com',
'cache' => null,
));
$url = $manager->createAbsoluteUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('http://www.example.com/?r=post/view&id=1&title=sample+post', $url);
@ -94,7 +100,9 @@ class UrlManagerTest extends \yiiunit\TestCase
public function testParseRequest()
{
$manager = new UrlManager;
$manager = new UrlManager(array(
'cache' => null,
));
$request = new Request;
// default setting without 'r' param
@ -115,6 +123,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL without rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cache' => null,
));
// empty pathinfo
$request->pathInfo = '';
@ -136,7 +145,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cacheID' => false,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
@ -169,7 +178,7 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'suffix' => '.html',
'cacheID' => false,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',

4
tests/unit/framework/web/UrlRuleTest.php

@ -10,7 +10,7 @@ class UrlRuleTest extends \yiiunit\TestCase
{
public function testCreateUrl()
{
$manager = new UrlManager;
$manager = new UrlManager(array('cache' => null));
$suites = $this->getTestsForCreateUrl();
foreach ($suites as $i => $suite) {
list ($name, $config, $tests) = $suite;
@ -25,7 +25,7 @@ class UrlRuleTest extends \yiiunit\TestCase
public function testParseRequest()
{
$manager = new UrlManager;
$manager = new UrlManager(array('cache' => null));
$request = new Request;
$suites = $this->getTestsForParseRequest();
foreach ($suites as $i => $suite) {

Loading…
Cancel
Save