Commit e1acc64b by Qiang Xue

refactoring cache and db references.

parent 5d6c9a4c
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\base; namespace yii\base;
use Yii; use Yii;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
use yii\helpers\FileHelper; use yii\helpers\FileHelper;
/** /**
* Module is the base class for module and application classes. * Module is the base class for module and application classes.
* *
* A module represents a sub-application which contains MVC elements by itself, such as * A module represents a sub-application which contains MVC elements by itself, such as
* models, views, controllers, etc. * models, views, controllers, etc.
* *
* A module may consist of [[modules|sub-modules]]. * A module may consist of [[modules|sub-modules]].
* *
* [[components|Components]] may be registered with the module so that they are globally * [[components|Components]] may be registered with the module so that they are globally
* accessible within the module. * accessible within the module.
* *
* @property string $uniqueId An ID that uniquely identifies this module among all modules within * @property string $uniqueId An ID that uniquely identifies this module among all modules within
* the current application. * the current application.
* @property string $basePath The root directory of the module. Defaults to the directory containing the module class. * @property string $basePath The root directory of the module. Defaults to the directory containing the module class.
* @property string $controllerPath The directory containing the controller classes. Defaults to "[[basePath]]/controllers". * @property string $controllerPath The directory containing the controller classes. Defaults to "[[basePath]]/controllers".
* @property string $viewPath The directory containing the view files within this module. Defaults to "[[basePath]]/views". * @property string $viewPath The directory containing the view files within this module. Defaults to "[[basePath]]/views".
* @property string $layoutPath The directory containing the layout view files within this module. Defaults to "[[viewPath]]/layouts". * @property string $layoutPath The directory containing the layout view files within this module. Defaults to "[[viewPath]]/layouts".
* @property array $modules The configuration of the currently installed modules (module ID => configuration). * @property array $modules The configuration of the currently installed modules (module ID => configuration).
* @property array $components The components (indexed by their IDs) registered within this module. * @property array $components The components (indexed by their IDs) registered within this module.
* @property array $import List of aliases to be imported. This property is write-only. * @property array $import List of aliases to be imported. This property is write-only.
* @property array $aliases List of aliases to be defined. This property is write-only. * @property array $aliases List of aliases to be defined. This property is write-only.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
abstract class Module extends Component abstract class Module extends Component
{ {
/** /**
* @var array custom module parameters (name => value). * @var array custom module parameters (name => value).
*/ */
public $params = array(); public $params = array();
/** /**
* @var array the IDs of the components that should be preloaded when this module is created. * @var array the IDs of the components that should be preloaded when this module is created.
*/ */
public $preload = array(); public $preload = array();
/** /**
* @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]]. * @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
*/ */
public $id; public $id;
/** /**
* @var Module the parent module of this module. Null if this module does not have a parent. * @var Module the parent module of this module. Null if this module does not have a parent.
*/ */
public $module; public $module;
/** /**
* @var string|boolean the layout that should be applied for views within this module. This refers to a view name * @var string|boolean the layout that should be applied for views within this module. This refers to a view name
* relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]] * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
* will be taken. If this is false, layout will be disabled within this module. * will be taken. If this is false, layout will be disabled within this module.
*/ */
public $layout; public $layout;
/** /**
* @var array mapping from controller ID to controller configurations. * @var array mapping from controller ID to controller configurations.
* Each name-value pair specifies the configuration of a single controller. * Each name-value pair specifies the configuration of a single controller.
* A controller configuration can be either a string or an array. * A controller configuration can be either a string or an array.
* If the former, the string should be the class name or path alias of the controller. * If the former, the string should be the class name or path alias of the controller.
* If the latter, the array must contain a 'class' element which specifies * If the latter, the array must contain a 'class' element which specifies
* the controller's class name or path alias, and the rest of the name-value pairs * the controller's class name or path alias, and the rest of the name-value pairs
* in the array are used to initialize the corresponding controller properties. For example, * in the array are used to initialize the corresponding controller properties. For example,
* *
* ~~~ * ~~~
* array( * array(
* 'account' => '@app/controllers/UserController', * 'account' => '@app/controllers/UserController',
* 'article' => array( * 'article' => array(
* 'class' => '@app/controllers/PostController', * 'class' => '@app/controllers/PostController',
* 'pageTitle' => 'something new', * 'pageTitle' => 'something new',
* ), * ),
* ) * )
* ~~~ * ~~~
*/ */
public $controllerMap = array(); public $controllerMap = array();
/** /**
* @var string the namespace that controller classes are in. Default is to use global namespace. * @var string the namespace that controller classes are in. Default is to use global namespace.
*/ */
public $controllerNamespace; public $controllerNamespace;
/** /**
* @return string the default route of this module. Defaults to 'default'. * @return string the default route of this module. Defaults to 'default'.
* The route may consist of child module ID, controller ID, and/or action ID. * The route may consist of child module ID, controller ID, and/or action ID.
* For example, `help`, `post/create`, `admin/post/create`. * For example, `help`, `post/create`, `admin/post/create`.
* If action ID is not given, it will take the default value as specified in * If action ID is not given, it will take the default value as specified in
* [[Controller::defaultAction]]. * [[Controller::defaultAction]].
*/ */
public $defaultRoute = 'default'; public $defaultRoute = 'default';
/** /**
* @var string the root directory of the module. * @var string the root directory of the module.
*/ */
private $_basePath; private $_basePath;
/** /**
* @var string the root directory that contains view files for this module * @var string the root directory that contains view files for this module
*/ */
private $_viewPath; private $_viewPath;
/** /**
* @var string the root directory that contains layout view files for this module. * @var string the root directory that contains layout view files for this module.
*/ */
private $_layoutPath; private $_layoutPath;
/** /**
* @var string the directory containing controller classes in the module. * @var string the directory containing controller classes in the module.
*/ */
private $_controllerPath; private $_controllerPath;
/** /**
* @var array child modules of this module * @var array child modules of this module
*/ */
private $_modules = array(); private $_modules = array();
/** /**
* @var array components registered under this module * @var array components registered under this module
*/ */
private $_components = array(); private $_components = array();
/** /**
* Constructor. * Constructor.
* @param string $id the ID of this module * @param string $id the ID of this module
* @param Module $parent the parent module (if any) * @param Module $parent the parent module (if any)
* @param array $config name-value pairs that will be used to initialize the object properties * @param array $config name-value pairs that will be used to initialize the object properties
*/ */
public function __construct($id, $parent = null, $config = array()) public function __construct($id, $parent = null, $config = array())
{ {
$this->id = $id; $this->id = $id;
$this->module = $parent; $this->module = $parent;
parent::__construct($config); parent::__construct($config);
} }
/** /**
* Getter magic method. * Getter magic method.
* This method is overridden to support accessing components * This method is overridden to support accessing components
* like reading module properties. * like reading module properties.
* @param string $name component or property name * @param string $name component or property name
* @return mixed the named property value * @return mixed the named property value
*/ */
public function __get($name) public function __get($name)
{ {
if ($this->hasComponent($name)) { if ($this->hasComponent($name)) {
return $this->getComponent($name); return $this->getComponent($name);
} else { } else {
return parent::__get($name); return parent::__get($name);
} }
} }
/** /**
* Checks if a property value is null. * Checks if a property value is null.
* This method overrides the parent implementation by checking * This method overrides the parent implementation by checking
* if the named component is loaded. * if the named component is loaded.
* @param string $name the property name or the event name * @param string $name the property name or the event name
* @return boolean whether the property value is null * @return boolean whether the property value is null
*/ */
public function __isset($name) public function __isset($name)
{ {
if ($this->hasComponent($name)) { if ($this->hasComponent($name)) {
return $this->getComponent($name) !== null; return $this->getComponent($name) !== null;
} else { } else {
return parent::__isset($name); return parent::__isset($name);
} }
} }
/** /**
* Initializes the module. * Initializes the module.
* This method is called after the module is created and initialized with property values * This method is called after the module is created and initialized with property values
* given in configuration. The default implement will create a path alias using the module [[id]] * given in configuration. The default implement will create a path alias using the module [[id]]
* and then call [[preloadComponents()]] to load components that are declared in [[preload]]. * and then call [[preloadComponents()]] to load components that are declared in [[preload]].
*/ */
public function init() public function init()
{ {
Yii::setAlias('@' . $this->id, $this->getBasePath()); Yii::setAlias('@' . $this->id, $this->getBasePath());
$this->preloadComponents(); $this->preloadComponents();
} }
/** /**
* Returns an ID that uniquely identifies this module among all modules within the current application. * Returns an ID that uniquely identifies this module among all modules within the current application.
* Note that if the module is an application, an empty string will be returned. * Note that if the module is an application, an empty string will be returned.
* @return string the unique ID of the module. * @return string the unique ID of the module.
*/ */
public function getUniqueId() public function getUniqueId()
{ {
if ($this instanceof Application) { if ($this instanceof Application) {
return ''; return '';
} elseif ($this->module) { } elseif ($this->module) {
return $this->module->getUniqueId() . '/' . $this->id; return $this->module->getUniqueId() . '/' . $this->id;
} else { } else {
return $this->id; return $this->id;
} }
} }
/** /**
* Returns the root directory of the module. * Returns the root directory of the module.
* It defaults to the directory containing the module class file. * It defaults to the directory containing the module class file.
* @return string the root directory of the module. * @return string the root directory of the module.
*/ */
public function getBasePath() public function getBasePath()
{ {
if ($this->_basePath === null) { if ($this->_basePath === null) {
$class = new \ReflectionClass($this); $class = new \ReflectionClass($this);
$this->_basePath = dirname($class->getFileName()); $this->_basePath = dirname($class->getFileName());
} }
return $this->_basePath; return $this->_basePath;
} }
/** /**
* Sets the root directory of the module. * Sets the root directory of the module.
* This method can only be invoked at the beginning of the constructor. * This method can only be invoked at the beginning of the constructor.
* @param string $path the root directory of the module. This can be either a directory name or a path alias. * @param string $path the root directory of the module. This can be either a directory name or a path alias.
* @throws Exception if the directory does not exist. * @throws Exception if the directory does not exist.
*/ */
public function setBasePath($path) public function setBasePath($path)
{ {
$this->_basePath = FileHelper::ensureDirectory($path); $this->_basePath = FileHelper::ensureDirectory($path);
} }
/** /**
* Returns the directory that contains the controller classes. * Returns the directory that contains the controller classes.
* Defaults to "[[basePath]]/controllers". * Defaults to "[[basePath]]/controllers".
* @return string the directory that contains the controller classes. * @return string the directory that contains the controller classes.
*/ */
public function getControllerPath() public function getControllerPath()
{ {
if ($this->_controllerPath !== null) { if ($this->_controllerPath !== null) {
return $this->_controllerPath; return $this->_controllerPath;
} else { } else {
return $this->_controllerPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'controllers'; return $this->_controllerPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'controllers';
} }
} }
/** /**
* Sets the directory that contains the controller classes. * Sets the directory that contains the controller classes.
* @param string $path the directory that contains the controller classes. * @param string $path the directory that contains the controller classes.
* This can be either a directory name or a path alias. * This can be either a directory name or a path alias.
* @throws Exception if the directory is invalid * @throws Exception if the directory is invalid
*/ */
public function setControllerPath($path) public function setControllerPath($path)
{ {
$this->_controllerPath = FileHelper::ensureDirectory($path); $this->_controllerPath = FileHelper::ensureDirectory($path);
} }
/** /**
* Returns the directory that contains the view files for this module. * Returns the directory that contains the view files for this module.
* @return string the root directory of view files. Defaults to "[[basePath]]/view". * @return string the root directory of view files. Defaults to "[[basePath]]/view".
*/ */
public function getViewPath() public function getViewPath()
{ {
if ($this->_viewPath !== null) { if ($this->_viewPath !== null) {
return $this->_viewPath; return $this->_viewPath;
} else { } else {
return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views'; return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
} }
} }
/** /**
* Sets the directory that contains the view files. * Sets the directory that contains the view files.
* @param string $path the root directory of view files. * @param string $path the root directory of view files.
* @throws Exception if the directory is invalid * @throws Exception if the directory is invalid
*/ */
public function setViewPath($path) public function setViewPath($path)
{ {
$this->_viewPath = FileHelper::ensureDirectory($path); $this->_viewPath = FileHelper::ensureDirectory($path);
} }
/** /**
* Returns the directory that contains layout view files for this module. * Returns the directory that contains layout view files for this module.
* @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts". * @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
*/ */
public function getLayoutPath() public function getLayoutPath()
{ {
if ($this->_layoutPath !== null) { if ($this->_layoutPath !== null) {
return $this->_layoutPath; return $this->_layoutPath;
} else { } else {
return $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts'; return $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
} }
} }
/** /**
* Sets the directory that contains the layout files. * Sets the directory that contains the layout files.
* @param string $path the root directory of layout files. * @param string $path the root directory of layout files.
* @throws Exception if the directory is invalid * @throws Exception if the directory is invalid
*/ */
public function setLayoutPath($path) public function setLayoutPath($path)
{ {
$this->_layoutPath = FileHelper::ensureDirectory($path); $this->_layoutPath = FileHelper::ensureDirectory($path);
} }
/** /**
* Imports the specified path aliases. * Imports the specified path aliases.
* This method is provided so that you can import a set of path aliases when configuring a module. * This method is provided so that you can import a set of path aliases when configuring a module.
* The path aliases will be imported by calling [[Yii::import()]]. * The path aliases will be imported by calling [[Yii::import()]].
* @param array $aliases list of path aliases to be imported * @param array $aliases list of path aliases to be imported
*/ */
public function setImport($aliases) public function setImport($aliases)
{ {
foreach ($aliases as $alias) { foreach ($aliases as $alias) {
Yii::import($alias); Yii::import($alias);
} }
} }
/** /**
* Defines path aliases. * Defines path aliases.
* This method calls [[Yii::setAlias()]] to register the path aliases. * This method calls [[Yii::setAlias()]] to register the path aliases.
* This method is provided so that you can define path aliases when configuring a module. * This method is provided so that you can define path aliases when configuring a module.
* @param array $aliases list of path aliases to be defined. The array keys are alias names * @param array $aliases list of path aliases to be defined. The array keys are alias names
* (must start with '@') and the array values are the corresponding paths or aliases. * (must start with '@') and the array values are the corresponding paths or aliases.
* For example, * For example,
* *
* ~~~ * ~~~
* array( * array(
* '@models' => '@app/models', // an existing alias * '@models' => '@app/models', // an existing alias
* '@backend' => __DIR__ . '/../backend', // a directory * '@backend' => __DIR__ . '/../backend', // a directory
* ) * )
* ~~~ * ~~~
*/ */
public function setAliases($aliases) public function setAliases($aliases)
{ {
foreach ($aliases as $name => $alias) { foreach ($aliases as $name => $alias) {
Yii::setAlias($name, $alias); Yii::setAlias($name, $alias);
} }
} }
/** /**
* Checks whether the named module exists. * Checks whether the named module exists.
* @param string $id module ID * @param string $id module ID
* @return boolean whether the named module exists. Both loaded and unloaded modules * @return boolean whether the named module exists. Both loaded and unloaded modules
* are considered. * are considered.
*/ */
public function hasModule($id) public function hasModule($id)
{ {
return isset($this->_modules[$id]); return isset($this->_modules[$id]);
} }
/** /**
* Retrieves the named module. * Retrieves the named module.
* @param string $id module ID (case-sensitive) * @param string $id module ID (case-sensitive)
* @param boolean $load whether to load the module if it is not yet loaded. * @param boolean $load whether to load the module if it is not yet loaded.
* @return Module|null the module instance, null if the module * @return Module|null the module instance, null if the module
* does not exist. * does not exist.
* @see hasModule() * @see hasModule()
*/ */
public function getModule($id, $load = true) public function getModule($id, $load = true)
{ {
if (isset($this->_modules[$id])) { if (isset($this->_modules[$id])) {
if ($this->_modules[$id] instanceof Module) { if ($this->_modules[$id] instanceof Module) {
return $this->_modules[$id]; return $this->_modules[$id];
} elseif ($load) { } elseif ($load) {
Yii::trace("Loading module: $id", __CLASS__); Yii::trace("Loading module: $id", __CLASS__);
return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this); return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
} }
} }
return null; return null;
} }
/** /**
* Adds a sub-module to this module. * Adds a sub-module to this module.
* @param string $id module ID * @param string $id module ID
* @param Module|array|null $module the sub-module to be added to this module. This can * @param Module|array|null $module the sub-module to be added to this module. This can
* be one of the followings: * be one of the followings:
* *
* - a [[Module]] object * - a [[Module]] object
* - a configuration array: when [[getModule()]] is called initially, the array * - a configuration array: when [[getModule()]] is called initially, the array
* will be used to instantiate the sub-module * will be used to instantiate the sub-module
* - null: the named sub-module will be removed from this module * - null: the named sub-module will be removed from this module
*/ */
public function setModule($id, $module) public function setModule($id, $module)
{ {
if ($module === null) { if ($module === null) {
unset($this->_modules[$id]); unset($this->_modules[$id]);
} else { } else {
$this->_modules[$id] = $module; $this->_modules[$id] = $module;
} }
} }
/** /**
* Returns the sub-modules in this module. * Returns the sub-modules in this module.
* @param boolean $loadedOnly whether to return the loaded sub-modules only. If this is set false, * @param boolean $loadedOnly whether to return the loaded sub-modules only. If this is set false,
* then all sub-modules registered in this module will be returned, whether they are loaded or not. * then all sub-modules registered in this module will be returned, whether they are loaded or not.
* Loaded modules will be returned as objects, while unloaded modules as configuration arrays. * Loaded modules will be returned as objects, while unloaded modules as configuration arrays.
* @return array the modules (indexed by their IDs) * @return array the modules (indexed by their IDs)
*/ */
public function getModules($loadedOnly = false) public function getModules($loadedOnly = false)
{ {
if ($loadedOnly) { if ($loadedOnly) {
$modules = array(); $modules = array();
foreach ($this->_modules as $module) { foreach ($this->_modules as $module) {
if ($module instanceof Module) { if ($module instanceof Module) {
$modules[] = $module; $modules[] = $module;
} }
} }
return $modules; return $modules;
} else { } else {
return $this->_modules; return $this->_modules;
} }
} }
/** /**
* Registers sub-modules in the current module. * Registers sub-modules in the current module.
* *
* Each sub-module should be specified as a name-value pair, where * Each sub-module should be specified as a name-value pair, where
* name refers to the ID of the module and value the module or a configuration * name refers to the ID of the module and value the module or a configuration
* array that can be used to create the module. In the latter case, [[Yii::createObject()]] * array that can be used to create the module. In the latter case, [[Yii::createObject()]]
* will be used to create the module. * will be used to create the module.
* *
* If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently. * If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.
* *
* The following is an example for registering two sub-modules: * The following is an example for registering two sub-modules:
* *
* ~~~ * ~~~
* array( * array(
* 'comment' => array( * 'comment' => array(
* 'class' => 'app\modules\CommentModule', * 'class' => 'app\modules\CommentModule',
* 'connectionID' => 'db', * 'db' => 'db',
* ), * ),
* 'booking' => array( * 'booking' => array(
* 'class' => 'app\modules\BookingModule', * 'class' => 'app\modules\BookingModule',
* ), * ),
* ) * )
* ~~~ * ~~~
* *
* @param array $modules modules (id => module configuration or instances) * @param array $modules modules (id => module configuration or instances)
*/ */
public function setModules($modules) public function setModules($modules)
{ {
foreach ($modules as $id => $module) { foreach ($modules as $id => $module) {
$this->_modules[$id] = $module; $this->_modules[$id] = $module;
} }
} }
/** /**
* Checks whether the named component exists. * Checks whether the named component exists.
* @param string $id component ID * @param string $id component ID
* @return boolean whether the named component exists. Both loaded and unloaded components * @return boolean whether the named component exists. Both loaded and unloaded components
* are considered. * are considered.
*/ */
public function hasComponent($id) public function hasComponent($id)
{ {
return isset($this->_components[$id]); return isset($this->_components[$id]);
} }
/** /**
* Retrieves the named component. * Retrieves the named component.
* @param string $id component ID (case-sensitive) * @param string $id component ID (case-sensitive)
* @param boolean $load whether to load the component if it is not yet loaded. * @param boolean $load whether to load the component if it is not yet loaded.
* @return Component|null the component instance, null if the component does not exist. * @return Component|null the component instance, null if the component does not exist.
* @see hasComponent() * @see hasComponent()
*/ */
public function getComponent($id, $load = true) public function getComponent($id, $load = true)
{ {
if (isset($this->_components[$id])) { if (isset($this->_components[$id])) {
if ($this->_components[$id] instanceof Component) { if ($this->_components[$id] instanceof Component) {
return $this->_components[$id]; return $this->_components[$id];
} elseif ($load) { } elseif ($load) {
Yii::trace("Loading component: $id", __CLASS__); Yii::trace("Loading component: $id", __CLASS__);
return $this->_components[$id] = Yii::createObject($this->_components[$id]); return $this->_components[$id] = Yii::createObject($this->_components[$id]);
} }
} }
return null; return null;
} }
/** /**
* Registers a component with this module. * Registers a component with this module.
* @param string $id component ID * @param string $id component ID
* @param Component|array|null $component the component to be registered with the module. This can * @param Component|array|null $component the component to be registered with the module. This can
* be one of the followings: * be one of the followings:
* *
* - a [[Component]] object * - a [[Component]] object
* - a configuration array: when [[getComponent()]] is called initially for this component, the array * - a configuration array: when [[getComponent()]] is called initially for this component, the array
* will be used to instantiate the component via [[Yii::createObject()]]. * will be used to instantiate the component via [[Yii::createObject()]].
* - null: the named component will be removed from the module * - null: the named component will be removed from the module
*/ */
public function setComponent($id, $component) public function setComponent($id, $component)
{ {
if ($component === null) { if ($component === null) {
unset($this->_components[$id]); unset($this->_components[$id]);
} else { } else {
$this->_components[$id] = $component; $this->_components[$id] = $component;
} }
} }
/** /**
* Returns the registered components. * Returns the registered components.
* @param boolean $loadedOnly whether to return the loaded components only. If this is set false, * @param boolean $loadedOnly whether to return the loaded components only. If this is set false,
* then all components specified in the configuration will be returned, whether they are loaded or not. * then all components specified in the configuration will be returned, whether they are loaded or not.
* Loaded components will be returned as objects, while unloaded components as configuration arrays. * Loaded components will be returned as objects, while unloaded components as configuration arrays.
* @return array the components (indexed by their IDs) * @return array the components (indexed by their IDs)
*/ */
public function getComponents($loadedOnly = false) public function getComponents($loadedOnly = false)
{ {
if ($loadedOnly) { if ($loadedOnly) {
$components = array(); $components = array();
foreach ($this->_components as $component) { foreach ($this->_components as $component) {
if ($component instanceof Component) { if ($component instanceof Component) {
$components[] = $component; $components[] = $component;
} }
} }
return $components; return $components;
} else { } else {
return $this->_components; return $this->_components;
} }
} }
/** /**
* Registers a set of components in this module. * Registers a set of components in this module.
* *
* Each component should be specified as a name-value pair, where * Each component should be specified as a name-value pair, where
* name refers to the ID of the component and value the component or a configuration * name refers to the ID of the component and value the component or a configuration
* array that can be used to create the component. In the latter case, [[Yii::createObject()]] * array that can be used to create the component. In the latter case, [[Yii::createObject()]]
* will be used to create the component. * will be used to create the component.
* *
* If a new component has the same ID as an existing one, the existing one will be overwritten silently. * If a new component has the same ID as an existing one, the existing one will be overwritten silently.
* *
* The following is an example for setting two components: * The following is an example for setting two components:
* *
* ~~~ * ~~~
* array( * array(
* 'db' => array( * 'db' => array(
* 'class' => 'yii\db\Connection', * 'class' => 'yii\db\Connection',
* 'dsn' => 'sqlite:path/to/file.db', * 'dsn' => 'sqlite:path/to/file.db',
* ), * ),
* 'cache' => array( * 'cache' => array(
* 'class' => 'yii\caching\DbCache', * 'class' => 'yii\caching\DbCache',
* 'connectionID' => 'db', * 'db' => 'db',
* ), * ),
* ) * )
* ~~~ * ~~~
* *
* @param array $components components (id => component configuration or instance) * @param array $components components (id => component configuration or instance)
*/ */
public function setComponents($components) public function setComponents($components)
{ {
foreach ($components as $id => $component) { foreach ($components as $id => $component) {
$this->_components[$id] = $component; $this->_components[$id] = $component;
} }
} }
/** /**
* Loads components that are declared in [[preload]]. * Loads components that are declared in [[preload]].
*/ */
public function preloadComponents() public function preloadComponents()
{ {
foreach ($this->preload as $id) { foreach ($this->preload as $id) {
$this->getComponent($id); $this->getComponent($id);
} }
} }
/** /**
* Runs a controller action specified by a route. * Runs a controller action specified by a route.
* This method parses the specified route and creates the corresponding child module(s), controller and action * This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters. * instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
* If the route is empty, the method will use [[defaultRoute]]. * If the route is empty, the method will use [[defaultRoute]].
* @param string $route the route that specifies the action. * @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action * @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal. * @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws InvalidRouteException if the requested route cannot be resolved into an action successfully * @throws InvalidRouteException if the requested route cannot be resolved into an action successfully
*/ */
public function runAction($route, $params = array()) public function runAction($route, $params = array())
{ {
$result = $this->createController($route); $result = $this->createController($route);
if (is_array($result)) { if (is_array($result)) {
/** @var $controller Controller */ /** @var $controller Controller */
list($controller, $actionID) = $result; list($controller, $actionID) = $result;
$oldController = Yii::$app->controller; $oldController = Yii::$app->controller;
Yii::$app->controller = $controller; Yii::$app->controller = $controller;
$status = $controller->runAction($actionID, $params); $status = $controller->runAction($actionID, $params);
Yii::$app->controller = $oldController; Yii::$app->controller = $oldController;
return $status; return $status;
} else { } else {
throw new InvalidRouteException('Unable to resolve the request "' . trim($this->getUniqueId() . '/' . $route, '/') . '".'); throw new InvalidRouteException('Unable to resolve the request "' . trim($this->getUniqueId() . '/' . $route, '/') . '".');
} }
} }
/** /**
* Creates a controller instance based on the controller ID. * Creates a controller instance based on the controller ID.
* *
* The controller is created within this module. The method first attempts to * The controller is created within this module. The method first attempts to
* create the controller based on the [[controllerMap]] of the module. If not available, * create the controller based on the [[controllerMap]] of the module. If not available,
* it will look for the controller class under the [[controllerPath]] and create an * it will look for the controller class under the [[controllerPath]] and create an
* instance of it. * instance of it.
* *
* @param string $route the route consisting of module, controller and action IDs. * @param string $route the route consisting of module, controller and action IDs.
* @return array|boolean if the controller is created successfully, it will be returned together * @return array|boolean if the controller is created successfully, it will be returned together
* with the remainder of the route which represents the action ID. Otherwise false will be returned. * with the remainder of the route which represents the action ID. Otherwise false will be returned.
*/ */
public function createController($route) public function createController($route)
{ {
if ($route === '') { if ($route === '') {
$route = $this->defaultRoute; $route = $this->defaultRoute;
} }
if (($pos = strpos($route, '/')) !== false) { if (($pos = strpos($route, '/')) !== false) {
$id = substr($route, 0, $pos); $id = substr($route, 0, $pos);
$route = substr($route, $pos + 1); $route = substr($route, $pos + 1);
} else { } else {
$id = $route; $id = $route;
$route = ''; $route = '';
} }
$module = $this->getModule($id); $module = $this->getModule($id);
if ($module !== null) { if ($module !== null) {
return $module->createController($route); return $module->createController($route);
} }
if (isset($this->controllerMap[$id])) { if (isset($this->controllerMap[$id])) {
$controller = Yii::createObject($this->controllerMap[$id], $id, $this); $controller = Yii::createObject($this->controllerMap[$id], $id, $this);
} elseif (preg_match('/^[a-z0-9\\-_]+$/', $id)) { } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id)) {
$className = StringHelper::id2camel($id) . 'Controller'; $className = StringHelper::id2camel($id) . 'Controller';
$classFile = $this->controllerPath . DIRECTORY_SEPARATOR . $className . '.php'; $classFile = $this->controllerPath . DIRECTORY_SEPARATOR . $className . '.php';
if (is_file($classFile)) { if (is_file($classFile)) {
$className = $this->controllerNamespace . '\\' . $className; $className = $this->controllerNamespace . '\\' . $className;
if (!class_exists($className, false)) { if (!class_exists($className, false)) {
require($classFile); require($classFile);
} }
if (class_exists($className, false) && is_subclass_of($className, '\yii\base\Controller')) { if (class_exists($className, false) && is_subclass_of($className, '\yii\base\Controller')) {
$controller = new $className($id, $this); $controller = new $className($id, $this);
} }
} }
} }
return isset($controller) ? array($controller, $route) : false; return isset($controller) ? array($controller, $route) : false;
} }
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\caching; namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
...@@ -14,30 +15,20 @@ 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 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]]. * By default, DbCache stores session data in a DB table named 'tbl_cache'. This table
* For MySQL database, the table should be created beforehand as follows : * must be pre-created. The table name can be changed by setting [[cacheTable]].
*
* ~~~
* 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.
* *
* Please refer to [[Cache]] for common cache operations that are supported by DbCache. * 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
...@@ -45,50 +36,56 @@ use yii\db\Query; ...@@ -45,50 +36,56 @@ use yii\db\Query;
class DbCache extends Cache 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'. * @var string name of the DB table to store cache content.
* The table must be created before using this cache component. * 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 * @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. * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
**/ **/
public $gcProbability = 100; 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. * Initializes the DbCache component.
* @param Connection $value the DB connection instance * 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 ...@@ -101,17 +98,16 @@ class DbCache extends Cache
{ {
$query = new Query; $query = new Query;
$query->select(array('data')) $query->select(array('data'))
->from($this->cacheTableName) ->from($this->cacheTable)
->where('id = :id AND (expire = 0 OR expire >' . time() . ')', array(':id' => $key)); ->where('id = :id AND (expire = 0 OR expire >' . time() . ')', array(':id' => $key));
$db = $this->getDb(); if ($this->db->enableQueryCache) {
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching // temporarily disable and re-enable query caching
$db->enableQueryCache = false; $this->db->enableQueryCache = false;
$result = $query->createCommand($db)->queryScalar(); $result = $query->createCommand($this->db)->queryScalar();
$db->enableQueryCache = true; $this->db->enableQueryCache = true;
return $result; return $result;
} else { } else {
return $query->createCommand($db)->queryScalar(); return $query->createCommand($this->db)->queryScalar();
} }
} }
...@@ -127,17 +123,16 @@ class DbCache extends Cache ...@@ -127,17 +123,16 @@ class DbCache extends Cache
} }
$query = new Query; $query = new Query;
$query->select(array('id', 'data')) $query->select(array('id', 'data'))
->from($this->cacheTableName) ->from($this->cacheTable)
->where(array('id' => $keys)) ->where(array('id' => $keys))
->andWhere('(expire = 0 OR expire > ' . time() . ')'); ->andWhere('(expire = 0 OR expire > ' . time() . ')');
$db = $this->getDb(); if ($this->db->enableQueryCache) {
if ($db->enableQueryCache) { $this->db->enableQueryCache = false;
$db->enableQueryCache = false; $rows = $query->createCommand($this->db)->queryAll();
$rows = $query->createCommand($db)->queryAll(); $this->db->enableQueryCache = true;
$db->enableQueryCache = true;
} else { } else {
$rows = $query->createCommand($db)->queryAll(); $rows = $query->createCommand($this->db)->queryAll();
} }
$results = array(); $results = array();
...@@ -161,13 +156,13 @@ class DbCache extends Cache ...@@ -161,13 +156,13 @@ class DbCache extends Cache
*/ */
protected function setValue($key, $value, $expire) protected function setValue($key, $value, $expire)
{ {
$command = $this->getDb()->createCommand(); $command = $this->db->createCommand()
$command->update($this->cacheTableName, array( ->update($this->cacheTable, array(
'expire' => $expire > 0 ? $expire + time() : 0, 'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB), 'data' => array($value, \PDO::PARAM_LOB),
), array( ), array(
'id' => $key, 'id' => $key,
));; ));
if ($command->execute()) { if ($command->execute()) {
$this->gc(); $this->gc();
...@@ -196,14 +191,13 @@ class DbCache extends Cache ...@@ -196,14 +191,13 @@ class DbCache extends Cache
$expire = 0; $expire = 0;
} }
$command = $this->getDb()->createCommand();
$command->insert($this->cacheTableName, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
));
try { try {
$command->execute(); $this->db->createCommand()
->insert($this->cacheTable, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
))->execute();
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
...@@ -218,8 +212,9 @@ class DbCache extends Cache ...@@ -218,8 +212,9 @@ class DbCache extends Cache
*/ */
protected function deleteValue($key) protected function deleteValue($key)
{ {
$command = $this->getDb()->createCommand(); $this->db->createCommand()
$command->delete($this->cacheTableName, array('id' => $key))->execute(); ->delete($this->cacheTable, array('id' => $key))
->execute();
return true; return true;
} }
...@@ -231,8 +226,9 @@ class DbCache extends Cache ...@@ -231,8 +226,9 @@ class DbCache extends Cache
public function gc($force = false) public function gc($force = false)
{ {
if ($force || mt_rand(0, 1000000) < $this->gcProbability) { if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$command = $this->getDb()->createCommand(); $this->db->createCommand()
$command->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())->execute(); ->delete($this->cacheTable, 'expire > 0 AND expire < ' . time())
->execute();
} }
} }
...@@ -243,8 +239,9 @@ class DbCache extends Cache ...@@ -243,8 +239,9 @@ class DbCache extends Cache
*/ */
protected function flushValues() protected function flushValues()
{ {
$command = $this->getDb()->createCommand(); $this->db->createCommand()
$command->delete($this->cacheTableName)->execute(); ->delete($this->cacheTable)
->execute();
return true; return true;
} }
} }
...@@ -23,9 +23,9 @@ use yii\db\Connection; ...@@ -23,9 +23,9 @@ use yii\db\Connection;
class DbDependency extends Dependency 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. * @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. * Only the first row of the query result will be used.
...@@ -50,24 +50,17 @@ class DbDependency extends Dependency ...@@ -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. * Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state. * This method returns the value of the global state.
* @return mixed the data needed to determine if dependency has been changed. * @return mixed the data needed to determine if dependency has been changed.
*/ */
protected function generateDependencyData() 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) { if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching // temporarily disable and re-enable query caching
$db->enableQueryCache = false; $db->enableQueryCache = false;
...@@ -78,36 +71,4 @@ class DbDependency extends Dependency ...@@ -78,36 +71,4 @@ class DbDependency extends Dependency
} }
return $result; 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;
}
} }
...@@ -135,9 +135,13 @@ class Controller extends \yii\base\Controller ...@@ -135,9 +135,13 @@ class Controller extends \yii\base\Controller
/** /**
* Returns the names of the global options for this command. * 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. * name is the option name.
* Child classes may override this method to specify possible global options. * 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. * @return array the names of the global options for this command.
*/ */
public function globalOptions() public function globalOptions()
......
<?php <?php
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\console\controllers; namespace yii\console\controllers;
use Yii; use Yii;
use yii\console\Exception; use yii\console\Exception;
use yii\console\Controller; use yii\console\Controller;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
/** /**
* This command manages application migrations. * This command manages application migrations.
* *
* A migration means a set of persistent changes to the application environment * A migration means a set of persistent changes to the application environment
* that is shared among different developers. For example, in an application * that is shared among different developers. For example, in an application
* backed by a database, a migration may refer to a set of changes to * backed by a database, a migration may refer to a set of changes to
* the database, such as creating a new table, adding a new table column. * the database, such as creating a new table, adding a new table column.
* *
* This command provides support for tracking the migration history, upgrading * This command provides support for tracking the migration history, upgrading
* or downloading with migrations, and creating new migration skeletons. * or downloading with migrations, and creating new migration skeletons.
* *
* The migration history is stored in a database table named as [[migrationTable]]. * The migration history is stored in a database table named
* The table will be automatically created the first this command is executed. * as [[migrationTable]]. The table will be automatically created the first time
* You may also manually create it with the following structure: * this command is executed, if it does not exist. You may also manually
* * create it as follows:
* ~~~ *
* CREATE TABLE tbl_migration ( * ~~~
* version varchar(255) PRIMARY KEY, * CREATE TABLE tbl_migration (
* apply_time integer * version varchar(255) PRIMARY KEY,
* ) * apply_time integer
* ~~~ * )
* * ~~~
* Below are some common usages of this command: *
* * Below are some common usages of this command:
* ~~~ *
* # creates a new migration named 'create_user_table' * ~~~
* yiic migrate/create create_user_table * # creates a new migration named 'create_user_table'
* * yiic migrate/create create_user_table
* # applies ALL new migrations *
* yiic migrate * # applies ALL new migrations
* * yiic migrate
* # reverts the last applied migration *
* yiic migrate/down * # reverts the last applied migration
* ~~~ * yiic migrate/down
* * ~~~
* @author Qiang Xue <qiang.xue@gmail.com> *
* @since 2.0 * @author Qiang Xue <qiang.xue@gmail.com>
*/ * @since 2.0
class MigrateController extends Controller */
{ class MigrateController extends Controller
/** {
* The name of the dummy migration that marks the beginning of the whole migration history. /**
*/ * The name of the dummy migration that marks the beginning of the whole migration history.
const BASE_MIGRATION = 'm000000_000000_base'; */
const BASE_MIGRATION = 'm000000_000000_base';
/**
* @var string the default command action. /**
*/ * @var string the default command action.
public $defaultAction = 'up'; */
/** public $defaultAction = 'up';
* @var string the directory storing the migration classes. This can be either /**
* a path alias or a directory. * @var string the directory storing the migration classes. This can be either
*/ * a path alias or a directory.
public $migrationPath = '@app/migrations'; */
/** public $migrationPath = '@app/migrations';
* @var string the name of the table for keeping applied migration information. /**
*/ * @var string the name of the table for keeping applied migration information.
public $migrationTable = 'tbl_migration'; */
/** public $migrationTable = 'tbl_migration';
* @var string the component ID that specifies the database connection for /**
* storing migration information. * @var string the template file for generating new migrations.
*/ * This can be either a path alias (e.g. "@app/migrations/template.php")
public $connectionID = 'db'; * or a file path.
/** */
* @var string the template file for generating new migrations. public $templateFile = '@yii/views/migration.php';
* This can be either a path alias (e.g. "@app/migrations/template.php") /**
* or a file path. * @var boolean whether to execute the migration in an interactive mode.
*/ */
public $templateFile = '@yii/views/migration.php'; public $interactive = true;
/** /**
* @var boolean whether to execute the migration in an interactive mode. * @var Connection|string the DB connection object or the application
*/ * component ID of the DB connection.
public $interactive = true; */
/** public $db = 'db';
* @var Connection the DB connection used for storing migration history.
* @see connectionID /**
*/ * Returns the names of the global options for this command.
public $db; * @return array the names of the global options for this command.
*/
/** public function globalOptions()
* Returns the names of the global options for this command. {
* @return array the names of the global options for this command. return array('migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive');
*/ }
public function globalOptions()
{ /**
return array('migrationPath', 'migrationTable', 'connectionID', 'templateFile', 'interactive'); * This method is invoked right before an action is to be executed (after all possible filters.)
} * It checks the existence of the [[migrationPath]].
* @param \yii\base\Action $action the action to be executed.
/** * @return boolean whether the action should continue to be executed.
* This method is invoked right before an action is to be executed (after all possible filters.) * @throws Exception if the migration directory does not exist.
* It checks the existence of the [[migrationPath]]. */
* @param \yii\base\Action $action the action to be executed. public function beforeAction($action)
* @return boolean whether the action should continue to be executed. {
* @throws Exception if the migration directory does not exist. if (parent::beforeAction($action)) {
*/ $path = Yii::getAlias($this->migrationPath);
public function beforeAction($action) if (!is_dir($path)) {
{ throw new Exception("The migration directory \"{$this->migrationPath}\" does not exist.");
if (parent::beforeAction($action)) { }
$path = Yii::getAlias($this->migrationPath); $this->migrationPath = $path;
if (!is_dir($path)) {
throw new Exception("The migration directory \"{$this->migrationPath}\" does not exist."); if (is_string($this->db)) {
} $this->db = Yii::$app->getComponent($this->db);
$this->migrationPath = $path; }
if (!$this->db instanceof Connection) {
$this->db = Yii::$app->getComponent($this->connectionID); throw new Exception("The 'db' option must refer to the application component ID of a DB connection.");
if (!$this->db instanceof Connection) { }
throw new Exception("Invalid DB connection \"{$this->connectionID}\".");
} $version = Yii::getVersion();
echo "Yii Migration Tool (based on Yii v{$version})\n\n";
$version = Yii::getVersion(); return true;
echo "Yii Migration Tool (based on Yii v{$version})\n\n"; } else {
return true; return false;
} else { }
return false; }
}
} /**
* Upgrades the application by applying new migrations.
/** * For example,
* Upgrades the application by applying new migrations. *
* For example, * ~~~
* * yiic migrate # apply all new migrations
* ~~~ * yiic migrate 3 # apply the first 3 new migrations
* yiic migrate # apply all new migrations * ~~~
* yiic migrate 3 # apply the first 3 new migrations *
* ~~~ * @param integer $limit the number of new migrations to be applied. If 0, it means
* * applying all available new migrations.
* @param integer $limit the number of new migrations to be applied. If 0, it means */
* applying all available new migrations. public function actionUp($limit = 0)
*/ {
public function actionUp($limit = 0) if (($migrations = $this->getNewMigrations()) === array()) {
{ echo "No new migration found. Your system is up-to-date.\n";
if (($migrations = $this->getNewMigrations()) === array()) { Yii::$app->end();
echo "No new migration found. Your system is up-to-date.\n"; }
Yii::$app->end();
} $total = count($migrations);
$limit = (int)$limit;
$total = count($migrations); if ($limit > 0) {
$limit = (int)$limit; $migrations = array_slice($migrations, 0, $limit);
if ($limit > 0) { }
$migrations = array_slice($migrations, 0, $limit);
} $n = count($migrations);
if ($n === $total) {
$n = count($migrations); echo "Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n";
if ($n === $total) { } else {
echo "Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n"; echo "Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n";
} else { }
echo "Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n";
} foreach ($migrations as $migration) {
echo " $migration\n";
foreach ($migrations as $migration) { }
echo " $migration\n"; echo "\n";
}
echo "\n"; if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if (!$this->migrateUp($migration)) {
foreach ($migrations as $migration) { echo "\nMigration failed. The rest of the migrations are canceled.\n";
if (!$this->migrateUp($migration)) { return;
echo "\nMigration failed. The rest of the migrations are canceled.\n"; }
return; }
} echo "\nMigrated up successfully.\n";
} }
echo "\nMigrated up successfully.\n"; }
}
} /**
* Downgrades the application by reverting old migrations.
/** * For example,
* Downgrades the application by reverting old migrations. *
* For example, * ~~~
* * yiic migrate/down # revert the last migration
* ~~~ * yiic migrate/down 3 # revert the last 3 migrations
* yiic migrate/down # revert the last migration * ~~~
* yiic migrate/down 3 # revert the last 3 migrations *
* ~~~ * @param integer $limit the number of migrations to be reverted. Defaults to 1,
* * meaning the last applied migration will be reverted.
* @param integer $limit the number of migrations to be reverted. Defaults to 1, * @throws Exception if the number of the steps specified is less than 1.
* meaning the last applied migration will be reverted. */
* @throws Exception if the number of the steps specified is less than 1. public function actionDown($limit = 1)
*/ {
public function actionDown($limit = 1) $limit = (int)$limit;
{ if ($limit < 1) {
$limit = (int)$limit; throw new Exception("The step argument must be greater than 0.");
if ($limit < 1) { }
throw new Exception("The step argument must be greater than 0.");
} if (($migrations = $this->getMigrationHistory($limit)) === array()) {
echo "No migration has been done before.\n";
if (($migrations = $this->getMigrationHistory($limit)) === array()) { return;
echo "No migration has been done before.\n"; }
return; $migrations = array_keys($migrations);
}
$migrations = array_keys($migrations); $n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n";
$n = count($migrations); foreach ($migrations as $migration) {
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n"; echo " $migration\n";
foreach ($migrations as $migration) { }
echo " $migration\n"; echo "\n";
}
echo "\n"; if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if (!$this->migrateDown($migration)) {
foreach ($migrations as $migration) { echo "\nMigration failed. The rest of the migrations are canceled.\n";
if (!$this->migrateDown($migration)) { return;
echo "\nMigration failed. The rest of the migrations are canceled.\n"; }
return; }
} echo "\nMigrated down successfully.\n";
} }
echo "\nMigrated down successfully.\n"; }
}
} /**
* Redoes the last few migrations.
/** *
* Redoes the last few migrations. * This command will first revert the specified migrations, and then apply
* * them again. For example,
* This command will first revert the specified migrations, and then apply *
* them again. For example, * ~~~
* * yiic migrate/redo # redo the last applied migration
* ~~~ * yiic migrate/redo 3 # redo the last 3 applied migrations
* yiic migrate/redo # redo the last applied migration * ~~~
* yiic migrate/redo 3 # redo the last 3 applied migrations *
* ~~~ * @param integer $limit the number of migrations to be redone. Defaults to 1,
* * meaning the last applied migration will be redone.
* @param integer $limit the number of migrations to be redone. Defaults to 1, * @throws Exception if the number of the steps specified is less than 1.
* meaning the last applied migration will be redone. */
* @throws Exception if the number of the steps specified is less than 1. public function actionRedo($limit = 1)
*/ {
public function actionRedo($limit = 1) $limit = (int)$limit;
{ if ($limit < 1) {
$limit = (int)$limit; throw new Exception("The step argument must be greater than 0.");
if ($limit < 1) { }
throw new Exception("The step argument must be greater than 0.");
} if (($migrations = $this->getMigrationHistory($limit)) === array()) {
echo "No migration has been done before.\n";
if (($migrations = $this->getMigrationHistory($limit)) === array()) { return;
echo "No migration has been done before.\n"; }
return; $migrations = array_keys($migrations);
}
$migrations = array_keys($migrations); $n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n";
$n = count($migrations); foreach ($migrations as $migration) {
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n"; echo " $migration\n";
foreach ($migrations as $migration) { }
echo " $migration\n"; echo "\n";
}
echo "\n"; if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) { if (!$this->migrateDown($migration)) {
foreach ($migrations as $migration) { echo "\nMigration failed. The rest of the migrations are canceled.\n";
if (!$this->migrateDown($migration)) { return;
echo "\nMigration failed. The rest of the migrations are canceled.\n"; }
return; }
} foreach (array_reverse($migrations) as $migration) {
} if (!$this->migrateUp($migration)) {
foreach (array_reverse($migrations) as $migration) { echo "\nMigration failed. The rest of the migrations migrations are canceled.\n";
if (!$this->migrateUp($migration)) { return;
echo "\nMigration failed. The rest of the migrations migrations are canceled.\n"; }
return; }
} echo "\nMigration redone successfully.\n";
} }
echo "\nMigration redone successfully.\n"; }
}
} /**
* Upgrades or downgrades till the specified version.
/** *
* Upgrades or downgrades till the specified version of migration. * This command will first revert the specified migrations, and then apply
* * them again. For example,
* This command will first revert the specified migrations, and then apply *
* them again. For example, * ~~~
* * yiic migrate/to 101129_185401 # using timestamp
* ~~~ * yiic migrate/to m101129_185401_create_user_table # using full name
* yiic migrate/to 101129_185401 # using timestamp * ~~~
* yiic migrate/to m101129_185401_create_user_table # using full name *
* ~~~ * @param string $version the version name that the application should be migrated to.
* * This can be either the timestamp or the full name of the migration.
* @param string $version the version name that the application should be migrated to. * @throws Exception if the version argument is invalid
* This can be either the timestamp or the full name of the migration. */
* @throws Exception if the version argument is invalid public function actionTo($version)
*/ {
public function actionTo($version) $originalVersion = $version;
{ if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$originalVersion = $version; $version = 'm' . $matches[1];
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) { } else {
$version = 'm' . $matches[1]; throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} else { }
throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} // try migrate up
$migrations = $this->getNewMigrations();
// try migrate up foreach ($migrations as $i => $migration) {
$migrations = $this->getNewMigrations(); if (strpos($migration, $version . '_') === 0) {
foreach ($migrations as $i => $migration) { $this->actionUp($i + 1);
if (strpos($migration, $version . '_') === 0) { return;
$this->actionUp($i + 1); }
return; }
}
} // try migrate down
$migrations = array_keys($this->getMigrationHistory(-1));
// try migrate down foreach ($migrations as $i => $migration) {
$migrations = array_keys($this->getMigrationHistory(-1)); if (strpos($migration, $version . '_') === 0) {
foreach ($migrations as $i => $migration) { if ($i === 0) {
if (strpos($migration, $version . '_') === 0) { echo "Already at '$originalVersion'. Nothing needs to be done.\n";
if ($i === 0) { } else {
echo "Already at '$originalVersion'. Nothing needs to be done.\n"; $this->actionDown($i);
} else { }
$this->actionDown($i); return;
} }
return; }
}
} throw new Exception("Unable to find the version '$originalVersion'.");
}
throw new Exception("Unable to find the version '$originalVersion'.");
} /**
* Modifies the migration history to the specified version.
/** *
* Modifies the migration history to the specified version. * No actual migration will be performed.
* *
* No actual migration will be performed. * ~~~
* * yiic migrate/mark 101129_185401 # using timestamp
* ~~~ * yiic migrate/mark m101129_185401_create_user_table # using full name
* yiic migrate/mark 101129_185401 # using timestamp * ~~~
* yiic migrate/mark m101129_185401_create_user_table # using full name *
* ~~~ * @param string $version the version at which the migration history should be marked.
* * This can be either the timestamp or the full name of the migration.
* @param string $version the version at which the migration history should be marked. * @throws Exception if the version argument is invalid or the version cannot be found.
* This can be either the timestamp or the full name of the migration. */
* @throws Exception if the version argument is invalid or the version cannot be found. public function actionMark($version)
*/ {
public function actionMark($version) $originalVersion = $version;
{ if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$originalVersion = $version; $version = 'm' . $matches[1];
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) { } else {
$version = 'm' . $matches[1]; throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} else { }
throw new Exception("The version argument must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).");
} // try mark up
$migrations = $this->getNewMigrations();
// try mark up foreach ($migrations as $i => $migration) {
$migrations = $this->getNewMigrations(); if (strpos($migration, $version . '_') === 0) {
foreach ($migrations as $i => $migration) { if ($this->confirm("Set migration history at $originalVersion?")) {
if (strpos($migration, $version . '_') === 0) { $command = $this->db->createCommand();
if ($this->confirm("Set migration history at $originalVersion?")) { for ($j = 0; $j <= $i; ++$j) {
$command = $this->db->createCommand(); $command->insert($this->migrationTable, array(
for ($j = 0; $j <= $i; ++$j) { 'version' => $migrations[$j],
$command->insert($this->migrationTable, array( 'apply_time' => time(),
'version' => $migrations[$j], ))->execute();
'apply_time' => time(), }
))->execute(); echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
} }
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n"; return;
} }
return; }
}
} // try mark down
$migrations = array_keys($this->getMigrationHistory(-1));
// try mark down foreach ($migrations as $i => $migration) {
$migrations = array_keys($this->getMigrationHistory(-1)); if (strpos($migration, $version . '_') === 0) {
foreach ($migrations as $i => $migration) { if ($i === 0) {
if (strpos($migration, $version . '_') === 0) { echo "Already at '$originalVersion'. Nothing needs to be done.\n";
if ($i === 0) { } else {
echo "Already at '$originalVersion'. Nothing needs to be done.\n"; if ($this->confirm("Set migration history at $originalVersion?")) {
} else { $command = $this->db->createCommand();
if ($this->confirm("Set migration history at $originalVersion?")) { for ($j = 0; $j < $i; ++$j) {
$command = $this->db->createCommand(); $command->delete($this->migrationTable, array(
for ($j = 0; $j < $i; ++$j) { 'version' => $migrations[$j],
$command->delete($this->migrationTable, array( ))->execute();
'version' => $migrations[$j], }
))->execute(); echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
} }
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n"; }
} return;
} }
return; }
}
} throw new Exception("Unable to find the version '$originalVersion'.");
}
throw new Exception("Unable to find the version '$originalVersion'.");
} /**
* Displays the migration history.
/** *
* Displays the migration history. * This command will show the list of migrations that have been applied
* * so far. For example,
* This command will show the list of migrations that have been applied *
* so far. For example, * ~~~
* * yiic migrate/history # showing the last 10 migrations
* ~~~ * yiic migrate/history 5 # showing the last 5 migrations
* yiic migrate/history # showing the last 10 migrations * yiic migrate/history 0 # showing the whole history
* yiic migrate/history 5 # showing the last 5 migrations * ~~~
* yiic migrate/history 0 # showing the whole history *
* ~~~ * @param integer $limit the maximum number of migrations to be displayed.
* * If it is 0, the whole migration history will be displayed.
* @param integer $limit the maximum number of migrations to be displayed. */
* If it is 0, the whole migration history will be displayed. public function actionHistory($limit = 10)
*/ {
public function actionHistory($limit = 10) $limit = (int)$limit;
{ $migrations = $this->getMigrationHistory($limit);
$limit = (int)$limit; if ($migrations === array()) {
$migrations = $this->getMigrationHistory($limit); echo "No migration has been done before.\n";
if ($migrations === array()) { } else {
echo "No migration has been done before.\n"; $n = count($migrations);
} else { if ($limit > 0) {
$n = count($migrations); echo "Showing the last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
if ($limit > 0) { } else {
echo "Showing the last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ":\n"; echo "Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . " been applied before:\n";
} else { }
echo "Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . " been applied before:\n"; foreach ($migrations as $version => $time) {
} echo " (" . date('Y-m-d H:i:s', $time) . ') ' . $version . "\n";
foreach ($migrations as $version => $time) { }
echo " (" . date('Y-m-d H:i:s', $time) . ') ' . $version . "\n"; }
} }
}
} /**
* Displays the un-applied new migrations.
/** *
* Displays the un-applied new migrations. * This command will show the new migrations that have not been applied.
* * For example,
* This command will show the new migrations that have not been applied. *
* For example, * ~~~
* * yiic migrate/new # showing the first 10 new migrations
* ~~~ * yiic migrate/new 5 # showing the first 5 new migrations
* yiic migrate/new # showing the first 10 new migrations * yiic migrate/new 0 # showing all new migrations
* yiic migrate/new 5 # showing the first 5 new migrations * ~~~
* yiic migrate/new 0 # showing all new migrations *
* ~~~ * @param integer $limit the maximum number of new migrations to be displayed.
* * If it is 0, all available new migrations will be displayed.
* @param integer $limit the maximum number of new migrations to be displayed. */
* If it is 0, all available new migrations will be displayed. public function actionNew($limit = 10)
*/ {
public function actionNew($limit = 10) $limit = (int)$limit;
{ $migrations = $this->getNewMigrations();
$limit = (int)$limit; if ($migrations === array()) {
$migrations = $this->getNewMigrations(); echo "No new migrations found. Your system is up-to-date.\n";
if ($migrations === array()) { } else {
echo "No new migrations found. Your system is up-to-date.\n"; $n = count($migrations);
} else { if ($limit > 0 && $n > $limit) {
$n = count($migrations); $migrations = array_slice($migrations, 0, $limit);
if ($limit > 0 && $n > $limit) { echo "Showing $limit out of $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
$migrations = array_slice($migrations, 0, $limit); } else {
echo "Showing $limit out of $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n"; echo "Found $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
} else { }
echo "Found $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
} foreach ($migrations as $migration) {
echo " " . $migration . "\n";
foreach ($migrations as $migration) { }
echo " " . $migration . "\n"; }
} }
}
} /**
* Creates a new migration.
/** *
* Creates a new migration. * This command creates a new migration using the available migration template.
* * After using this command, developers should modify the created migration
* This command creates a new migration using the available migration template. * skeleton by filling up the actual migration logic.
* After using this command, developers should modify the created migration *
* skeleton by filling up the actual migration logic. * ~~~
* * yiic migrate/create create_user_table
* ~~~ * ~~~
* yiic migrate/create create_user_table *
* ~~~ * @param string $name the name of the new migration. This should only contain
* * letters, digits and/or underscores.
* @param string $name the name of the new migration. This should only contain * @throws Exception if the name argument is invalid.
* letters, digits and/or underscores. */
* @throws Exception if the name argument is invalid. public function actionCreate($name)
*/ {
public function actionCreate($name) if (!preg_match('/^\w+$/', $name)) {
{ throw new Exception("The migration name should contain letters, digits and/or underscore characters only.");
if (!preg_match('/^\w+$/', $name)) { }
throw new Exception("The migration name should contain letters, digits and/or underscore characters only.");
} $name = 'm' . gmdate('ymd_His') . '_' . $name;
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $name . '.php';
$name = 'm' . gmdate('ymd_His') . '_' . $name;
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $name . '.php'; if ($this->confirm("Create new migration '$file'?")) {
$content = $this->renderFile(Yii::getAlias($this->templateFile), array(
if ($this->confirm("Create new migration '$file'?")) { 'className' => $name,
$content = $this->renderFile(Yii::getAlias($this->templateFile), array( ));
'className' => $name, file_put_contents($file, $content);
)); echo "New migration created successfully.\n";
file_put_contents($file, $content); }
echo "New migration created successfully.\n"; }
}
} /**
* Upgrades with the specified migration class.
/** * @param string $class the migration class name
* Upgrades with the specified migration class. * @return boolean whether the migration is successful
* @param string $class the migration class name */
* @return boolean whether the migration is successful protected function migrateUp($class)
*/ {
protected function migrateUp($class) if ($class === self::BASE_MIGRATION) {
{ return true;
if ($class === self::BASE_MIGRATION) { }
return true;
} echo "*** applying $class\n";
$start = microtime(true);
echo "*** applying $class\n"; $migration = $this->createMigration($class);
$start = microtime(true); if ($migration->up() !== false) {
$migration = $this->createMigration($class); $this->db->createCommand()->insert($this->migrationTable, array(
if ($migration->up() !== false) { 'version' => $class,
$this->db->createCommand()->insert($this->migrationTable, array( 'apply_time' => time(),
'version' => $class, ))->execute();
'apply_time' => time(), $time = microtime(true) - $start;
))->execute(); echo "*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$time = microtime(true) - $start; return true;
echo "*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; } else {
return true; $time = microtime(true) - $start;
} else { echo "*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$time = microtime(true) - $start; return false;
echo "*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; }
return false; }
}
} /**
* Downgrades with the specified migration class.
/** * @param string $class the migration class name
* Downgrades with the specified migration class. * @return boolean whether the migration is successful
* @param string $class the migration class name */
* @return boolean whether the migration is successful protected function migrateDown($class)
*/ {
protected function migrateDown($class) if ($class === self::BASE_MIGRATION) {
{ return true;
if ($class === self::BASE_MIGRATION) { }
return true;
} echo "*** reverting $class\n";
$start = microtime(true);
echo "*** reverting $class\n"; $migration = $this->createMigration($class);
$start = microtime(true); if ($migration->down() !== false) {
$migration = $this->createMigration($class); $this->db->createCommand()->delete($this->migrationTable, array(
if ($migration->down() !== false) { 'version' => $class,
$this->db->createCommand()->delete($this->migrationTable, array( ))->execute();
'version' => $class, $time = microtime(true) - $start;
))->execute(); echo "*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$time = microtime(true) - $start; return true;
echo "*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; } else {
return true; $time = microtime(true) - $start;
} else { echo "*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$time = microtime(true) - $start; return false;
echo "*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n"; }
return false; }
}
} /**
* Creates a new migration instance.
/** * @param string $class the migration class name
* Creates a new migration instance. * @return \yii\db\Migration the migration instance
* @param string $class the migration class name */
* @return \yii\db\Migration the migration instance protected function createMigration($class)
*/ {
protected function createMigration($class) $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php';
{ require_once($file);
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php'; return new $class(array(
require_once($file); 'db' => $this->db,
return new $class(array( ));
'db' => $this->db, }
));
} /**
* Returns the migration history.
* @param integer $limit the maximum number of records in the history to be returned
/** * @return array the migration history
* @return Connection the database connection that is used to store the migration history. */
* @throws Exception if the database connection ID is invalid. protected function getMigrationHistory($limit)
*/ {
protected function getDb() if ($this->db->schema->getTableSchema($this->migrationTable) === null) {
{ $this->createMigrationHistoryTable();
if ($this->db !== null) { }
return $this->db; $query = new Query;
} else { $rows = $query->select(array('version', 'apply_time'))
$this->db = Yii::$app->getComponent($this->connectionID); ->from($this->migrationTable)
if ($this->db instanceof Connection) { ->orderBy('version DESC')
return $this->db; ->limit($limit)
} else { ->createCommand()
throw new Exception("Invalid DB connection: {$this->connectionID}."); ->queryAll();
} $history = ArrayHelper::map($rows, 'version', 'apply_time');
} unset($history[self::BASE_MIGRATION]);
} return $history;
}
/**
* Returns the migration history. /**
* @param integer $limit the maximum number of records in the history to be returned * Creates the migration history table.
* @return array the migration history */
*/ protected function createMigrationHistoryTable()
protected function getMigrationHistory($limit) {
{ echo 'Creating migration history table "' . $this->migrationTable . '"...';
if ($this->db->schema->getTableSchema($this->migrationTable) === null) { $this->db->createCommand()->createTable($this->migrationTable, array(
$this->createMigrationHistoryTable(); 'version' => 'varchar(255) NOT NULL PRIMARY KEY',
} 'apply_time' => 'integer',
$query = new Query; ))->execute();
$rows = $query->select(array('version', 'apply_time')) $this->db->createCommand()->insert($this->migrationTable, array(
->from($this->migrationTable) 'version' => self::BASE_MIGRATION,
->orderBy('version DESC') 'apply_time' => time(),
->limit($limit) ))->execute();
->createCommand() echo "done.\n";
->queryAll(); }
$history = ArrayHelper::map($rows, 'version', 'apply_time');
unset($history[self::BASE_MIGRATION]); /**
return $history; * Returns the migrations that are not applied.
} * @return array list of new migrations
*/
/** protected function getNewMigrations()
* Creates the migration history table. {
*/ $applied = array();
protected function createMigrationHistoryTable() foreach ($this->getMigrationHistory(-1) as $version => $time) {
{ $applied[substr($version, 1, 13)] = true;
echo 'Creating migration history table "' . $this->migrationTable . '"...'; }
$this->db->createCommand()->createTable($this->migrationTable, array(
'version' => 'varchar(255) NOT NULL PRIMARY KEY', $migrations = array();
'apply_time' => 'integer', $handle = opendir($this->migrationPath);
))->execute(); while (($file = readdir($handle)) !== false) {
$this->db->createCommand()->insert($this->migrationTable, array( if ($file === '.' || $file === '..') {
'version' => self::BASE_MIGRATION, continue;
'apply_time' => time(), }
))->execute(); $path = $this->migrationPath . DIRECTORY_SEPARATOR . $file;
echo "done.\n"; if (preg_match('/^(m(\d{6}_\d{6})_.*?)\.php$/', $file, $matches) && is_file($path) && !isset($applied[$matches[2]])) {
} $migrations[] = $matches[1];
}
/** }
* Returns the migrations that are not applied. closedir($handle);
* @return array list of new migrations sort($migrations);
*/ return $migrations;
protected function getNewMigrations() }
{ }
$applied = array();
foreach ($this->getMigrationHistory(-1) as $version => $time) {
$applied[substr($version, 1, 13)] = true;
}
$migrations = array();
$handle = opendir($this->migrationPath);
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $this->migrationPath . DIRECTORY_SEPARATOR . $file;
if (preg_match('/^(m(\d{6}_\d{6})_.*?)\.php$/', $file, $matches) && is_file($path) && !isset($applied[$matches[2]])) {
$migrations[] = $matches[1];
}
}
closedir($handle);
sort($migrations);
return $migrations;
}
}
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
namespace yii\db; namespace yii\db;
use Yii;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\caching\Cache;
/** /**
* Command represents a SQL statement to be executed against a database. * Command represents a SQL statement to be executed against a database.
...@@ -132,7 +134,7 @@ class Command extends \yii\base\Component ...@@ -132,7 +134,7 @@ class Command extends \yii\base\Component
try { try {
$this->pdoStatement = $this->db->pdo->prepare($sql); $this->pdoStatement = $this->db->pdo->prepare($sql);
} catch (\Exception $e) { } 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; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode()); throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode());
} }
...@@ -264,7 +266,7 @@ class Command extends \yii\base\Component ...@@ -264,7 +266,7 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true); $paramLog = "\nParameters: " . var_export($this->_params, true);
} }
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__); Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
if ($sql == '') { if ($sql == '') {
return 0; return 0;
...@@ -272,7 +274,7 @@ class Command extends \yii\base\Component ...@@ -272,7 +274,7 @@ class Command extends \yii\base\Component
try { try {
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__); Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
} }
$this->prepare(); $this->prepare();
...@@ -280,16 +282,16 @@ class Command extends \yii\base\Component ...@@ -280,16 +282,16 @@ class Command extends \yii\base\Component
$n = $this->pdoStatement->rowCount(); $n = $this->pdoStatement->rowCount();
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
} }
return $n; return $n;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
} }
$message = $e->getMessage(); $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; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
...@@ -381,14 +383,14 @@ class Command extends \yii\base\Component ...@@ -381,14 +383,14 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true); $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 */ /** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') { 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( $cacheKey = $cache->buildKey(array(
__CLASS__, __CLASS__,
$db->dsn, $db->dsn,
...@@ -397,14 +399,14 @@ class Command extends \yii\base\Component ...@@ -397,14 +399,14 @@ class Command extends \yii\base\Component
$paramLog, $paramLog,
)); ));
if (($result = $cache->get($cacheKey)) !== false) { if (($result = $cache->get($cacheKey)) !== false) {
\Yii::trace('Query result found in cache', __CLASS__); Yii::trace('Query result served from cache', __CLASS__);
return $result; return $result;
} }
} }
try { try {
if ($db->enableProfiling) { if ($db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__); Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
} }
$this->prepare(); $this->prepare();
...@@ -421,21 +423,21 @@ class Command extends \yii\base\Component ...@@ -421,21 +423,21 @@ class Command extends \yii\base\Component
} }
if ($db->enableProfiling) { 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); $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; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($db->enableProfiling) { if ($db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
} }
$message = $e->getMessage(); $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; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
} }
......
...@@ -10,6 +10,7 @@ namespace yii\db; ...@@ -10,6 +10,7 @@ namespace yii\db;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\NotSupportedException; 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). * 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 ...@@ -136,10 +137,10 @@ class Connection extends Component
/** /**
* @var boolean whether to enable schema caching. * @var boolean whether to enable schema caching.
* Note that in order to enable truly schema caching, a valid cache component as specified * 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 schemaCacheDuration
* @see schemaCacheExclude * @see schemaCacheExclude
* @see schemaCacheID * @see schemaCache
*/ */
public $enableSchemaCache = false; public $enableSchemaCache = false;
/** /**
...@@ -155,20 +156,20 @@ class Connection extends Component ...@@ -155,20 +156,20 @@ class Connection extends Component
*/ */
public $schemaCacheExclude = array(); public $schemaCacheExclude = array();
/** /**
* @var string the ID of the cache application component that is used to cache the table metadata. * @var Cache|string the cache object or the ID of the cache application component that
* Defaults to 'cache'. * is used to cache the table metadata.
* @see enableSchemaCache * @see enableSchemaCache
*/ */
public $schemaCacheID = 'cache'; public $schemaCache = 'cache';
/** /**
* @var boolean whether to enable query caching. * @var boolean whether to enable query caching.
* Note that in order to enable query caching, a valid cache component as specified * 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 * Methods [[beginCache()]] and [[endCache()]] can be used as shortcuts to turn on
* and off query caching on the fly. * and off query caching on the fly.
* @see queryCacheDuration * @see queryCacheDuration
* @see queryCacheID * @see queryCache
* @see queryCacheDependency * @see queryCacheDependency
* @see beginCache() * @see beginCache()
* @see endCache() * @see endCache()
...@@ -176,7 +177,7 @@ class Connection extends Component ...@@ -176,7 +177,7 @@ class Connection extends Component
public $enableQueryCache = false; public $enableQueryCache = false;
/** /**
* @var integer number of seconds that query results can remain valid in cache. * @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. * Use 0 to indicate that the cached data will never expire.
* @see enableQueryCache * @see enableQueryCache
*/ */
...@@ -188,11 +189,11 @@ class Connection extends Component ...@@ -188,11 +189,11 @@ class Connection extends Component
*/ */
public $queryCacheDependency; public $queryCacheDependency;
/** /**
* @var string the ID of the cache application component that is used for query caching. * @var Cache|string the cache object or the ID of the cache application component
* Defaults to 'cache'. * that is used for query caching.
* @see enableQueryCache * @see enableQueryCache
*/ */
public $queryCacheID = 'cache'; public $queryCache = 'cache';
/** /**
* @var string the charset used for database connection. The property is only used * @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 * for MySQL and PostgreSQL databases. Defaults to null, meaning using default charset
...@@ -290,7 +291,7 @@ class Connection extends Component ...@@ -290,7 +291,7 @@ class Connection extends Component
* This method is provided as a shortcut to setting two properties that are related * This method is provided as a shortcut to setting two properties that are related
* with query caching: [[queryCacheDuration]] and [[queryCacheDependency]]. * with query caching: [[queryCacheDuration]] and [[queryCacheDependency]].
* @param integer $duration the number of seconds that query results may remain valid in cache. * @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. * @param \yii\caching\Dependency $dependency the dependency for the cached query result.
* See [[queryCacheDependency]] for more details. * See [[queryCacheDependency]] for more details.
*/ */
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\db; namespace yii\db;
use Yii;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\base\InvalidCallException; use yii\base\InvalidCallException;
use yii\caching\Cache; use yii\caching\Cache;
...@@ -84,21 +85,21 @@ abstract class Schema extends \yii\base\Object ...@@ -84,21 +85,21 @@ abstract class Schema extends \yii\base\Object
$db = $this->db; $db = $this->db;
$realName = $this->getRealTableName($name); $realName = $this->getRealTableName($name);
/** @var $cache Cache */ if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
if ($db->enableSchemaCache && ($cache = \Yii::$app->getComponent($db->schemaCacheID)) !== null && !in_array($name, $db->schemaCacheExclude, true)) { /** @var $cache Cache */
$key = $this->getCacheKey($cache, $name); $cache = is_string($db->schemaCache) ? Yii::$app->getComponent($db->schemaCache) : $db->schemaCache;
if ($refresh || ($table = $cache->get($key)) === false) { if ($cache instanceof Cache) {
$table = $this->loadTableSchema($realName); $key = $this->getCacheKey($cache, $name);
if ($table !== null) { if ($refresh || ($table = $cache->get($key)) === false) {
$cache->set($key, $table, $db->schemaCacheDuration); $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 $this->_tables[$name] = $table = $this->loadTableSchema($realName);
return $table;
} }
/** /**
...@@ -173,8 +174,9 @@ abstract class Schema extends \yii\base\Object ...@@ -173,8 +174,9 @@ abstract class Schema extends \yii\base\Object
*/ */
public function refresh() public function refresh()
{ {
/** @var $cache \yii\caching\Cache */ /** @var $cache Cache */
if ($this->db->enableSchemaCache && ($cache = \Yii::$app->getComponent($this->db->schemaCacheID)) !== null) { $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) { foreach ($this->_tables as $name => $table) {
$cache->delete($this->getCacheKey($cache, $name)); $cache->delete($this->getCacheKey($cache, $name));
} }
......
...@@ -7,16 +7,15 @@ ...@@ -7,16 +7,15 @@
namespace yii\logging; namespace yii\logging;
use Yii;
use yii\db\Connection; use yii\db\Connection;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
/** /**
* DbTarget stores log messages in a database table. * DbTarget stores log messages in a database table.
* *
* By default, DbTarget will use the database specified by [[connectionID]] and save * By default, DbTarget stores the log messages in a DB table named 'tbl_log'. This table
* messages into a table named by [[tableName]]. Please refer to [[tableName]] for the required * must be pre-created. The table name can be changed by setting [[logTable]].
* table structure. Note that this table must be created beforehand. Otherwise an exception
* will be thrown when DbTarget is saving messages into DB.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
...@@ -24,20 +23,18 @@ use yii\base\InvalidConfigException; ...@@ -24,20 +23,18 @@ use yii\base\InvalidConfigException;
class DbTarget extends Target class DbTarget extends Target
{ {
/** /**
* @var string the ID of [[Connection]] application component. * @var Connection|string the DB connection object or the application component ID of the DB connection.
* Defaults to 'db'. Please make sure that your database contains a table * After the DbTarget object is created, if you want to change this property, you should only assign it
* whose name is as specified in [[tableName]] and has the required table structure. * with a DB connection object.
* @see tableName
*/ */
public $connectionID = 'db'; public $db = 'db';
/** /**
* @var string the name of the DB table that stores log messages. Defaults to 'tbl_log'. * @var string name of the DB table to store cache content.
* * The table should be pre-created as follows:
* The DB table should have the following structure:
* *
* ~~~ * ~~~
* CREATE TABLE tbl_log ( * CREATE TABLE tbl_log (
* id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, * id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
* level INTEGER, * level INTEGER,
* category VARCHAR(255), * category VARCHAR(255),
* log_time INTEGER, * log_time INTEGER,
...@@ -48,42 +45,29 @@ class DbTarget extends Target ...@@ -48,42 +45,29 @@ class DbTarget extends Target
* ~~~ * ~~~
* *
* Note that the 'id' column must be created as an auto-incremental column. * 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`. * 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 * 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 * 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'; public $logTable = 'tbl_log';
private $_db;
/** /**
* Returns the DB connection used for saving log messages. * Initializes the DbTarget component.
* @return Connection the DB connection instance * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[connectionID]] does not point to a valid application component. * @throws InvalidConfigException if [[db]] is invalid.
*/ */
public function getDb() public function init()
{ {
if ($this->_db === null) { parent::init();
$db = \Yii::$app->getComponent($this->connectionID); if (is_string($this->db)) {
if ($db instanceof Connection) { $this->db = Yii::$app->getComponent($this->db);
$this->_db = $db; }
} else { if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbTarget::connectionID must refer to the ID of a DB application component."); 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 ...@@ -93,10 +77,9 @@ class DbTarget extends Target
*/ */
public function export($messages) public function export($messages)
{ {
$db = $this->getDb(); $tableName = $this->db->quoteTableName($this->logTable);
$tableName = $db->quoteTableName($this->tableName);
$sql = "INSERT INTO $tableName (level, category, log_time, message) VALUES (:level, :category, :log_time, :message)"; $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) { foreach ($messages as $message) {
$command->bindValues(array( $command->bindValues(array(
':level' => $message[1], ':level' => $message[1],
......
...@@ -15,7 +15,7 @@ use yii\base\InvalidConfigException; ...@@ -15,7 +15,7 @@ use yii\base\InvalidConfigException;
* CacheSession implements a session component using cache as storage medium. * CacheSession implements a session component using cache as storage medium.
* *
* The cache being used can be any cache application component. * 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 * 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 * 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; ...@@ -27,14 +27,27 @@ use yii\base\InvalidConfigException;
class CacheSession extends Session 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. * Returns a value indicating whether to use custom session storage.
...@@ -47,33 +60,6 @@ class CacheSession extends Session ...@@ -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. * Session read handler.
* Do not call this method directly. * Do not call this method directly.
* @param string $id session ID * @param string $id session ID
...@@ -81,7 +67,7 @@ class CacheSession extends Session ...@@ -81,7 +67,7 @@ class CacheSession extends Session
*/ */
public function readSession($id) public function readSession($id)
{ {
$data = $this->getCache()->get($this->calculateKey($id)); $data = $this->cache->get($this->calculateKey($id));
return $data === false ? '' : $data; return $data === false ? '' : $data;
} }
...@@ -94,7 +80,7 @@ class CacheSession extends Session ...@@ -94,7 +80,7 @@ class CacheSession extends Session
*/ */
public function writeSession($id, $data) 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 ...@@ -105,7 +91,7 @@ class CacheSession extends Session
*/ */
public function destroySession($id) 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 ...@@ -115,6 +101,6 @@ class CacheSession extends Session
*/ */
protected function calculateKey($id) protected function calculateKey($id)
{ {
return $this->getCache()->buildKey(array(__CLASS__, $id)); return $this->cache->buildKey(array(__CLASS__, $id));
} }
} }
...@@ -15,58 +15,70 @@ use yii\base\InvalidConfigException; ...@@ -15,58 +15,70 @@ use yii\base\InvalidConfigException;
/** /**
* DbSession extends [[Session]] by using database as session data storage. * 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 * 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]]. * must be pre-created. The table name can be changed by setting [[sessionTable]].
* The table should have the following structure: *
* * The following example shows how you can configure the application to use DbSession:
*
* ~~~ * ~~~
* CREATE TABLE tbl_session * 'session' => array(
* ( * 'class' => 'yii\web\DbSession',
* id CHAR(32) PRIMARY KEY, * // 'db' => 'mydb',
* expire INTEGER, * // 'sessionTable' => 'my_session',
* data BLOB
* ) * )
* ~~~ * ~~~
* *
* 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class DbSession extends Session class DbSession extends Session
{ {
/** /**
* @var string the ID of a {@link CDbConnection} application component. If not set, a SQLite database * @var Connection|string the DB connection object or the application component ID of the DB connection.
* will be automatically created and used. The SQLite database file is * After the DbSession object is created, if you want to change this property, you should only assign it
* is <code>protected/runtime/session-YiiVersion.db</code>. * with a DB connection object.
*/ */
public $connectionID; public $db = 'db';
/** /**
* @var string the name of the DB table to store session content. * @var string the name of the DB table that stores the session data.
* Note, if {@link autoCreateSessionTable} is false and you want to create the DB table manually by yourself, * The table should be pre-created as follows:
* you need to make sure the DB table is of the following structure: *
* <pre> * ~~~
* (id CHAR(32) PRIMARY KEY, expire INTEGER, data BLOB) * CREATE TABLE tbl_session
* </pre> * (
* @see autoCreateSessionTable * 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. * Returns a value indicating whether to use custom session storage.
...@@ -94,56 +106,31 @@ class DbSession extends Session ...@@ -94,56 +106,31 @@ class DbSession extends Session
parent::regenerateID(false); parent::regenerateID(false);
$newID = session_id(); $newID = session_id();
$db = $this->getDb();
$query = new Query; $query = new Query;
$row = $query->from($this->sessionTableName) $row = $query->from($this->sessionTable)
->where(array('id' => $oldID)) ->where(array('id' => $oldID))
->createCommand($db) ->createCommand($this->db)
->queryRow(); ->queryRow();
if ($row !== false) { if ($row !== false) {
if ($deleteOldSession) { if ($deleteOldSession) {
$db->createCommand()->update($this->sessionTableName, array( $this->db->createCommand()
'id' => $newID ->update($this->sessionTable, array('id' => $newID), array('id' => $oldID))
), array('id' => $oldID))->execute(); ->execute();
} else { } else {
$row['id'] = $newID; $row['id'] = $newID;
$db->createCommand()->insert($this->sessionTableName, $row)->execute(); $this->db->createCommand()
->insert($this->sessionTable, $row)
->execute();
} }
} else { } else {
// shouldn't reach here normally // shouldn't reach here normally
$db->createCommand()->insert($this->sessionTableName, array( $this->db->createCommand()
'id' => $newID, ->insert($this->sessionTable, array(
'expire' => time() + $this->getTimeout(), 'id' => $newID,
))->execute(); '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.");
}
} }
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 ...@@ -156,9 +143,9 @@ class DbSession extends Session
{ {
$query = new Query; $query = new Query;
$data = $query->select(array('data')) $data = $query->select(array('data'))
->from($this->sessionTableName) ->from($this->sessionTable)
->where('expire>:expire AND id=:id', array(':expire' => time(), ':id' => $id)) ->where('expire>:expire AND id=:id', array(':expire' => time(), ':id' => $id))
->createCommand($this->getDb()) ->createCommand($this->db)
->queryScalar(); ->queryScalar();
return $data === false ? '' : $data; return $data === false ? '' : $data;
} }
...@@ -176,24 +163,23 @@ class DbSession extends Session ...@@ -176,24 +163,23 @@ class DbSession extends Session
// http://us.php.net/manual/en/function.session-set-save-handler.php // http://us.php.net/manual/en/function.session-set-save-handler.php
try { try {
$expire = time() + $this->getTimeout(); $expire = time() + $this->getTimeout();
$db = $this->getDb();
$query = new Query; $query = new Query;
$exists = $query->select(array('id')) $exists = $query->select(array('id'))
->from($this->sessionTableName) ->from($this->sessionTable)
->where(array('id' => $id)) ->where(array('id' => $id))
->createCommand($db) ->createCommand($this->db)
->queryScalar(); ->queryScalar();
if ($exists === false) { if ($exists === false) {
$db->createCommand()->insert($this->sessionTableName, array( $this->db->createCommand()
'id' => $id, ->insert($this->sessionTable, array(
'data' => $data, 'id' => $id,
'expire' => $expire, 'data' => $data,
))->execute(); 'expire' => $expire,
))->execute();
} else { } else {
$db->createCommand()->update($this->sessionTableName, array( $this->db->createCommand()
'data' => $data, ->update($this->sessionTable, array('data' => $data, 'expire' => $expire), array('id' => $id))
'expire' => $expire ->execute();
), array('id' => $id))->execute();
} }
} catch (\Exception $e) { } catch (\Exception $e) {
if (YII_DEBUG) { if (YII_DEBUG) {
...@@ -213,8 +199,8 @@ class DbSession extends Session ...@@ -213,8 +199,8 @@ class DbSession extends Session
*/ */
public function destroySession($id) public function destroySession($id)
{ {
$this->getDb()->createCommand() $this->db->createCommand()
->delete($this->sessionTableName, array('id' => $id)) ->delete($this->sessionTable, array('id' => $id))
->execute(); ->execute();
return true; return true;
} }
...@@ -227,8 +213,8 @@ class DbSession extends Session ...@@ -227,8 +213,8 @@ class DbSession extends Session
*/ */
public function gcSession($maxLifetime) public function gcSession($maxLifetime)
{ {
$this->getDb()->createCommand() $this->db->createCommand()
->delete($this->sessionTableName, 'expire<:expire', array(':expire' => time())) ->delete($this->sessionTable, 'expire<:expire', array(':expire' => time()))
->execute(); ->execute();
return true; return true;
} }
......
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\web; namespace yii\web;
use Yii; use Yii;
use yii\base\ActionFilter; use yii\base\ActionFilter;
use yii\base\Action; use yii\base\Action;
use yii\base\View; use yii\base\View;
use yii\caching\Dependency; use yii\caching\Dependency;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class PageCache extends ActionFilter class PageCache extends ActionFilter
{ {
/** /**
* @var boolean whether the content being cached should be differentiated according to the route. * @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. * A route consists of the requested controller ID and action ID. Defaults to true.
*/ */
public $varyByRoute = true; public $varyByRoute = true;
/** /**
* @var View the view object that is used to create the fragment cache widget to implement page caching. * @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. * If not set, the view registered with the application will be used.
*/ */
public $view; public $view;
/**
/** * @var string the application component ID of the [[\yii\caching\Cache|cache]] object.
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) */
*/ public $cache = 'cache';
public $cacheID = 'cache'; /**
/** * @var integer number of seconds that the data can remain valid in 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.
* Use 0 to indicate that the cached data will never expire. */
*/ public $duration = 60;
public $duration = 60; /**
/** * @var array|Dependency the dependency that the cached content depends on.
* @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.
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. * For example,
* For example, *
* * ~~~
* ~~~ * array(
* array( * 'class' => 'yii\caching\DbDependency',
* 'class' => 'yii\caching\DbDependency', * 'sql' => 'SELECT MAX(lastModified) FROM Post',
* 'sql' => 'SELECT MAX(lastModified) FROM Post', * )
* ) * ~~~
* ~~~ *
* * would make the output cache depends on the last modified time of all posts.
* 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.
* If any post has its modification time changed, the cached content would be invalidated. */
*/ public $dependency;
public $dependency; /**
/** * @var array list of factors that would cause the variation of the content being cached.
* @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).
* 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
* The following variation setting will cause the content to be cached in different versions * according to the current application language:
* according to the current application language: *
* * ~~~
* ~~~ * array(
* array( * Yii::$app->language,
* Yii::$app->language, * )
* ) */
*/ public $variations;
public $variations; /**
/** * @var boolean whether to enable the fragment cache. You may use this property to turn on and off
* @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).
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). */
*/ public $enabled = true;
public $enabled = true;
public function init()
public function init() {
{ parent::init();
parent::init(); if ($this->view === null) {
if ($this->view === null) { $this->view = Yii::$app->getView();
$this->view = Yii::$app->getView(); }
} }
}
/**
/** * This method is invoked right before an action is to be executed (after all possible filters.)
* 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.
* You may override this method to do last-minute preparation for the action. * @param Action $action the action to be executed.
* @param Action $action the action to be executed. * @return boolean whether the action should continue to be executed.
* @return boolean whether the action should continue to be executed. */
*/ public function beforeAction($action)
public function beforeAction($action) {
{ $properties = array();
$properties = array(); foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) {
foreach (array('cacheID', 'duration', 'dependency', 'variations', 'enabled') as $name) { $properties[$name] = $this->$name;
$properties[$name] = $this->$name; }
} $id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__;
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__; return $this->view->beginCache($id, $properties);
return $this->view->beginCache($id, $properties); }
}
/**
/** * This method is invoked right after an action is executed.
* This method is invoked right after an action is executed. * You may override this method to do some postprocessing for the action.
* You may override this method to do some postprocessing for the action. * @param Action $action the action just executed.
* @param Action $action the action just executed. */
*/ public function afterAction($action)
public function afterAction($action) {
{ $this->view->endCache();
$this->view->endCache(); }
}
} }
\ No newline at end of file
...@@ -9,6 +9,7 @@ namespace yii\web; ...@@ -9,6 +9,7 @@ namespace yii\web;
use Yii; use Yii;
use yii\base\Component; use yii\base\Component;
use yii\caching\Cache;
/** /**
* UrlManager handles HTTP request parsing and creation of URLs based on a set of rules. * UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
...@@ -49,11 +50,14 @@ class UrlManager extends Component ...@@ -49,11 +50,14 @@ class UrlManager extends Component
*/ */
public $routeVar = 'r'; public $routeVar = 'r';
/** /**
* @var string the ID of the cache component that is used to cache the parsed URL rules. * @var Cache|string the cache object or the application component ID of the cache object.
* Defaults to 'cache' which refers to the primary cache component registered with the application. * Compiled URL rules will be cached through this cache object, if it is available.
* Set this property to false if you do not want to cache the URL rules. *
* 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 * @var string the default class name for creating URL rule instances
* when it is not specified in [[rules]]. * when it is not specified in [[rules]].
...@@ -65,11 +69,14 @@ class UrlManager extends Component ...@@ -65,11 +69,14 @@ class UrlManager extends Component
/** /**
* Initializes the application component. * Initializes UrlManager.
*/ */
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
}
$this->compileRules(); $this->compileRules();
} }
...@@ -81,13 +88,10 @@ class UrlManager extends Component ...@@ -81,13 +88,10 @@ class UrlManager extends Component
if (!$this->enablePrettyUrl || $this->rules === array()) { if (!$this->enablePrettyUrl || $this->rules === array()) {
return; return;
} }
/** if ($this->cache instanceof Cache) {
* @var $cache \yii\caching\Cache $key = $this->cache->buildKey(__CLASS__);
*/
if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
$key = $cache->buildKey(__CLASS__);
$hash = md5(json_encode($this->rules)); $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]; $this->rules = $data[0];
return; return;
} }
...@@ -100,8 +104,8 @@ class UrlManager extends Component ...@@ -100,8 +104,8 @@ class UrlManager extends Component
$this->rules[$i] = Yii::createObject($rule); $this->rules[$i] = Yii::createObject($rule);
} }
if (isset($cache)) { if ($this->cache instanceof Cache) {
$cache->set($key, array($this->rules, $hash)); $this->cache->set($key, array($this->rules, $hash));
} }
} }
......
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\widgets; namespace yii\widgets;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\Widget; use yii\base\Widget;
use yii\caching\Cache; use yii\caching\Cache;
use yii\caching\Dependency; use yii\caching\Dependency;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class FragmentCache extends Widget class FragmentCache extends Widget
{ {
/** /**
* @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.
*/ * After the FragmentCache object is created, if you want to change this property,
public $cacheID = 'cache'; * you should only assign it with a cache object.
/** */
* @var integer number of seconds that the data can remain valid in cache. public $cache = 'cache';
* Use 0 to indicate that the cached data will never expire. /**
*/ * @var integer number of seconds that the data can remain valid in cache.
public $duration = 60; * Use 0 to indicate that the cached data will never expire.
/** */
* @var array|Dependency the dependency that the cached content depends on. public $duration = 60;
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. /**
* For example, * @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', * 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. *
*/ * would make the output cache depends on the last modified time of all posts.
public $dependency; * If any post has its modification time changed, the cached content would be invalidated.
/** */
* @var array list of factors that would cause the variation of the content being cached. public $dependency;
* 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 * @var array list of factors that would cause the variation of the content being cached.
* according to the current application language: * 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, * ~~~
* ) * array(
*/ * Yii::$app->language,
public $variations; * )
/** */
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off public $variations;
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). /**
*/ * @var boolean whether to enable the fragment cache. You may use this property to turn on and off
public $enabled = true; * the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
/** */
* @var \yii\base\View the view object within which this widget is used. If not set, public $enabled = true;
* the view registered with the application will be used. This is mainly used by dynamic content feature. /**
*/ * @var \yii\base\View the view object within which this widget is used. If not set,
public $view; * the view registered with the application will be used. This is mainly used by dynamic content feature.
/** */
* @var array a list of placeholders for embedding dynamic contents. This property public $view;
* is used internally to implement the content caching feature. Do not modify it. /**
*/ * @var array a list of placeholders for embedding dynamic contents. This property
public $dynamicPlaceholders; * 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()} * Initializes the FragmentCache object.
* will be captured and saved in cache. */
* This method does nothing if valid content is already found in cache. public function init()
*/ {
public function init() parent::init();
{
if ($this->view === null) { if ($this->view === null) {
$this->view = Yii::$app->getView(); $this->view = Yii::$app->getView();
} }
if ($this->getCache() !== null && $this->getCachedContent() === false) {
$this->view->cacheStack[] = $this; if (!$this->enabled) {
ob_start(); $this->cache = null;
ob_implicit_flush(false); } elseif (is_string($this->cache)) {
} $this->cache = Yii::$app->getComponent($this->cache);
} }
/** if ($this->getCachedContent() === false) {
* Marks the end of content to be cached. $this->view->cacheStack[] = $this;
* Content displayed before this method call and after {@link init()} ob_start();
* will be captured and saved in cache. ob_implicit_flush(false);
* This method does nothing if valid content is already found in cache. }
*/ }
public function run()
{ /**
if (($content = $this->getCachedContent()) !== false) { * Marks the end of content to be cached.
echo $content; * Content displayed before this method call and after {@link init()}
} elseif (($cache = $this->getCache()) !== null) { * will be captured and saved in cache.
$content = ob_get_clean(); * This method does nothing if valid content is already found in cache.
array_pop($this->view->cacheStack); */
if (is_array($this->dependency)) { public function run()
$this->dependency = Yii::createObject($this->dependency); {
} if (($content = $this->getCachedContent()) !== false) {
$data = array($content, $this->dynamicPlaceholders); echo $content;
$cache->set($this->calculateKey(), $data, $this->duration, $this->dependency); } elseif ($this->cache instanceof Cache) {
$content = ob_get_clean();
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) { array_pop($this->view->cacheStack);
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); if (is_array($this->dependency)) {
} $this->dependency = Yii::createObject($this->dependency);
echo $content; }
} $data = array($content, $this->dynamicPlaceholders);
} $this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
/** if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) {
* @var string|boolean the cached content. False if the content is not cached. $content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
*/ }
private $_content; echo $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. /**
*/ * @var string|boolean the cached content. False if the content is not cached.
public function getCachedContent() */
{ private $_content;
if ($this->_content === null) {
$this->_content = false; /**
if (($cache = $this->getCache()) !== null) { * Returns the cached content if available.
$key = $this->calculateKey(); * @return string|boolean the cached content. False is returned if valid content is not found in the cache.
$data = $cache->get($key); */
if (is_array($data) && count($data) === 2) { public function getCachedContent()
list ($content, $placeholders) = $data; {
if (is_array($placeholders) && count($placeholders) > 0) { if ($this->_content === null) {
if ($this->view->cacheStack === array()) { $this->_content = false;
// outermost cache: replace placeholder with dynamic content if ($this->cache instanceof Cache) {
$content = $this->updateDynamicContent($content, $placeholders); $key = $this->calculateKey();
} $data = $this->cache->get($key);
foreach ($placeholders as $name => $statements) { if (is_array($data) && count($data) === 2) {
$this->view->addDynamicPlaceholder($name, $statements); list ($content, $placeholders) = $data;
} if (is_array($placeholders) && count($placeholders) > 0) {
} if ($this->view->cacheStack === array()) {
$this->_content = $content; // outermost cache: replace placeholder with dynamic content
} $content = $this->updateDynamicContent($content, $placeholders);
} }
} foreach ($placeholders as $name => $statements) {
return $this->_content; $this->view->addDynamicPlaceholder($name, $statements);
} }
}
protected function updateDynamicContent($content, $placeholders) $this->_content = $content;
{ }
foreach ($placeholders as $name => $statements) { }
$placeholders[$name] = $this->view->evaluateDynamicContent($statements); }
} return $this->_content;
return strtr($content, $placeholders); }
}
protected function updateDynamicContent($content, $placeholders)
/** {
* Generates a unique key used for storing the content in cache. foreach ($placeholders as $name => $statements) {
* The key generated depends on both [[id]] and [[variations]]. $placeholders[$name] = $this->view->evaluateDynamicContent($statements);
* @return string a valid cache key }
*/ return strtr($content, $placeholders);
protected function calculateKey() }
{
$factors = array(__CLASS__, $this->getId()); /**
if (is_array($this->variations)) { * Generates a unique key used for storing the content in cache.
foreach ($this->variations as $factor) { * The key generated depends on both [[id]] and [[variations]].
$factors[] = $factor; * @return string a valid cache key
} */
} protected function calculateKey()
return $this->getCache()->buildKey($factors); {
} $factors = array(__CLASS__, $this->getId());
if (is_array($this->variations)) {
/** foreach ($this->variations as $factor) {
* @var Cache $factors[] = $factor;
*/ }
private $_cache; }
return $this->cache->buildKey($factors);
/** }
* 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;
}
} }
\ No newline at end of file
<?php <?php
namespace yiiunit\framework\util; namespace yiiunit\framework\util;
use Yii; use Yii;
use yii\helpers\Html; use yii\helpers\Html;
use yii\web\Application; use yii\web\Application;
class HtmlTest extends \yii\test\TestCase class HtmlTest extends \yii\test\TestCase
{ {
public function setUp() public function setUp()
{ {
new Application('test', '@yiiunit/runtime', array( new Application('test', '@yiiunit/runtime', array(
'components' => array( 'components' => array(
'request' => array( 'request' => array(
'class' => 'yii\web\Request', 'class' => 'yii\web\Request',
'url' => '/test', 'url' => '/test',
), ),
), ),
)); ));
} }
public function tearDown() public function tearDown()
{ {
Yii::$app = null; Yii::$app = null;
} }
public function testEncode() public function testEncode()
{ {
$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'")); $this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
} }
public function testDecode() public function testDecode()
{ {
$this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;")); $this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;"));
} }
public function testTag() public function testTag()
{ {
$this->assertEquals('<br />', Html::tag('br')); $this->assertEquals('<br />', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span')); $this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); $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' => '<>'))); $this->assertEquals('<input type="text" name="test" value="&lt;&gt;" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = false; Html::$closeVoidElements = false;
$this->assertEquals('<br>', Html::tag('br')); $this->assertEquals('<br>', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span')); $this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); $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' => '<>'))); $this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = true; Html::$closeVoidElements = true;
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true))); $this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = false; Html::$showBooleanAttributeValues = false;
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true))); $this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = true; Html::$showBooleanAttributeValues = true;
} }
public function testBeginTag() public function testBeginTag()
{ {
$this->assertEquals('<br>', Html::beginTag('br')); $this->assertEquals('<br>', Html::beginTag('br'));
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title'))); $this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title')));
} }
public function testEndTag() public function testEndTag()
{ {
$this->assertEquals('</br>', Html::endTag('br')); $this->assertEquals('</br>', Html::endTag('br'));
$this->assertEquals('</span>', Html::endTag('span')); $this->assertEquals('</span>', Html::endTag('span'));
} }
public function testCdata() public function testCdata()
{ {
$data = 'test<>'; $data = 'test<>';
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data)); $this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data));
} }
public function testStyle() public function testStyle()
{ {
$content = 'a <>'; $content = 'a <>';
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content)); $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'))); $this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less')));
} }
public function testScript() public function testScript()
{ {
$content = 'a <>'; $content = 'a <>';
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content)); $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'))); $this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js')));
} }
public function testCssFile() 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="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com'));
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile('')); $this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile(''));
} }
public function testJsFile() 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="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile('')); $this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile(''));
} }
public function testBeginForm() public function testBeginForm()
{ {
$this->assertEquals('<form action="/test" method="post">', Html::beginForm()); $this->assertEquals('<form action="/test" method="post">', Html::beginForm());
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get')); $this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
$hiddens = array( $hiddens = array(
'<input type="hidden" name="id" value="1" />', '<input type="hidden" name="id" value="1" />',
'<input type="hidden" name="title" value="&lt;" />', '<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')); $this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get'));
} }
public function testEndForm() public function testEndForm()
{ {
$this->assertEquals('</form>', Html::endForm()); $this->assertEquals('</form>', Html::endForm());
} }
public function testA() public function testA()
{ {
$this->assertEquals('<a>something<></a>', Html::a('something<>')); $this->assertEquals('<a>something<></a>', Html::a('something<>'));
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example')); $this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example'));
$this->assertEquals('<a href="/test">something</a>', Html::a('something', '')); $this->assertEquals('<a href="/test">something</a>', Html::a('something', ''));
} }
public function testMailto() public function testMailto()
{ {
$this->assertEquals('<a href="mailto:test&lt;&gt;">test<></a>', Html::mailto('test<>')); $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>')); $this->assertEquals('<a href="mailto:test&gt;">test<></a>', Html::mailto('test<>', 'test>'));
} }
public function testImg() public function testImg()
{ {
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example')); $this->assertEquals('<img src="/example" alt="" />', Html::img('/example'));
$this->assertEquals('<img src="/test" alt="" />', Html::img('')); $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))); $this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10)));
} }
public function testLabel() public function testLabel()
{ {
$this->assertEquals('<label>something<></label>', Html::label('something<>')); $this->assertEquals('<label>something<></label>', Html::label('something<>'));
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a')); $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'))); $this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test')));
} }
public function testButton() public function testButton()
{ {
$this->assertEquals('<button type="button">Button</button>', Html::button()); $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="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"))); $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() public function testSubmitButton()
{ {
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton()); $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'))); $this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't')));
} }
public function testResetButton() public function testResetButton()
{ {
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton()); $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'))); $this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't')));
} }
public function testInput() public function testInput()
{ {
$this->assertEquals('<input type="text" />', Html::input('text')); $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'))); $this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't')));
} }
public function testButtonInput() public function testButtonInput()
{ {
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test')); $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'))); $this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a')));
} }
public function testSubmitInput() public function testSubmitInput()
{ {
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput()); $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'))); $this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a')));
} }
public function testResetInput() public function testResetInput()
{ {
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput()); $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'))); $this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a')));
} }
public function testTextInput() public function testTextInput()
{ {
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test')); $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'))); $this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't')));
} }
public function testHiddenInput() public function testHiddenInput()
{ {
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test')); $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'))); $this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't')));
} }
public function testPasswordInput() public function testPasswordInput()
{ {
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test')); $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'))); $this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't')));
} }
public function testFileInput() public function testFileInput()
{ {
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test')); $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'))); $this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't')));
} }
public function testTextarea() public function testTextarea()
{ {
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test')); $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'))); $this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', array('class' => 't')));
} }
public function testRadio() public function testRadio()
{ {
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); $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="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'))); $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() public function testCheckbox()
{ {
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); $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="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'))); $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() public function testDropDownList()
{ {
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test')); $this->assertEquals($expected, Html::dropDownList('test'));
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2">text2</option> <option value="value2">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems())); $this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); $this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
} }
public function testListBox() public function testListBox()
{ {
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test')); $this->assertEquals($expected, Html::listBox('test'));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="5"> <select name="test" size="5">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2">text2</option> <option value="value2">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5))); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option> <option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value 2">text&nbsp;&nbsp;2</option> <option value="value 2">text&nbsp;&nbsp;2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2())); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems())); $this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1" selected="selected">text1</option> <option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems())); $this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test[]" multiple="multiple" size="4"> <select name="test[]" multiple="multiple" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true))); $this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4"> <input type="hidden" name="test" value="0" /><select name="test" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0'))); $this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
} }
public function testCheckboxList() public function testCheckboxList()
{ {
$this->assertEquals('', Html::checkboxList('test')); $this->assertEquals('', Html::checkboxList('test'));
$expected = <<<EOD $expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1" /> text1</label> <label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems())); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label> <label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label> <label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2())); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br /> <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> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label> 0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label> 1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
} }
))); )));
} }
public function testRadioList() public function testRadioList()
{ {
$this->assertEquals('', Html::radioList('test')); $this->assertEquals('', Html::radioList('test'));
$expected = <<<EOD $expected = <<<EOD
<label><input type="radio" name="test" value="value1" /> text1</label> <label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems())); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label> <label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label> <label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2())); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br /> <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> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
0<label>text1 <input type="radio" name="test" value="value1" /></label> 0<label>text1 <input type="radio" name="test" value="value1" /></label>
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label> 1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
} }
))); )));
} }
public function testRenderOptions() public function testRenderOptions()
{ {
$data = array( $data = array(
'value1' => 'label1', 'value1' => 'label1',
'group1' => array( 'group1' => array(
'value11' => 'label11', 'value11' => 'label11',
'group11' => array( 'group11' => array(
'value111' => 'label111', 'value111' => 'label111',
), ),
'group12' => array(), 'group12' => array(),
), ),
'value2' => 'label2', 'value2' => 'label2',
'group2' => array(), 'group2' => array(),
); );
$expected = <<<EOD $expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option> <option value="">please&nbsp;select&lt;&gt;</option>
<option value="value1" selected="selected">label1</option> <option value="value1" selected="selected">label1</option>
<optgroup label="group1"> <optgroup label="group1">
<option value="value11">label11</option> <option value="value11">label11</option>
<optgroup label="group11"> <optgroup label="group11">
<option class="option" value="value111" selected="selected">label111</option> <option class="option" value="value111" selected="selected">label111</option>
</optgroup> </optgroup>
<optgroup class="group" label="group12"> <optgroup class="group" label="group12">
</optgroup> </optgroup>
</optgroup> </optgroup>
<option value="value2">label2</option> <option value="value2">label2</option>
<optgroup label="group2"> <optgroup label="group2">
</optgroup> </optgroup>
EOD; EOD;
$attributes = array( $attributes = array(
'prompt' => 'please select<>', 'prompt' => 'please select<>',
'options' => array( 'options' => array(
'value111' => array('class' => 'option'), 'value111' => array('class' => 'option'),
), ),
'groups' => array( 'groups' => array(
'group12' => array('class' => 'group'), 'group12' => array('class' => 'group'),
), ),
); );
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes)); $this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
} }
public function testRenderAttributes() public function testRenderAttributes()
{ {
$this->assertEquals('', Html::renderTagAttributes(array())); $this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); $this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Html::$showBooleanAttributeValues = false; Html::$showBooleanAttributeValues = false;
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); $this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Html::$showBooleanAttributeValues = true; Html::$showBooleanAttributeValues = true;
} }
protected function getDataItems() protected function getDataItems()
{ {
return array( return array(
'value1' => 'text1', 'value1' => 'text1',
'value2' => 'text2', 'value2' => 'text2',
); );
} }
protected function getDataItems2() protected function getDataItems2()
{ {
return array( return array(
'value1<>' => 'text1<>', 'value1<>' => 'text1<>',
'value 2' => 'text 2', 'value 2' => 'text 2',
); );
} }
} }
...@@ -11,6 +11,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -11,6 +11,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// default setting with '/' as base url // default setting with '/' as base url
$manager = new UrlManager(array( $manager = new UrlManager(array(
'baseUrl' => '/', 'baseUrl' => '/',
'cache' => null,
)); ));
$url = $manager->createUrl('post/view'); $url = $manager->createUrl('post/view');
$this->assertEquals('/?r=post/view', $url); $this->assertEquals('/?r=post/view', $url);
...@@ -20,6 +21,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -20,6 +21,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// default setting with '/test/' as base url // default setting with '/test/' as base url
$manager = new UrlManager(array( $manager = new UrlManager(array(
'baseUrl' => '/test/', 'baseUrl' => '/test/',
'cache' => null,
)); ));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post')); $url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/?r=post/view&id=1&title=sample+post', $url); $this->assertEquals('/test/?r=post/view&id=1&title=sample+post', $url);
...@@ -28,18 +30,21 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -28,18 +30,21 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/', 'baseUrl' => '/',
'cache' => null,
)); ));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post')); $url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view?id=1&title=sample+post', $url); $this->assertEquals('/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/test/', 'baseUrl' => '/test/',
'cache' => null,
)); ));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post')); $url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/post/view?id=1&title=sample+post', $url); $this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/test/index.php', 'baseUrl' => '/test/index.php',
'cache' => null,
)); ));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post')); $url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url); $this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url);
...@@ -49,7 +54,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -49,7 +54,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules // pretty URL with rules
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cacheID' => false, 'cache' => null,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
...@@ -66,7 +71,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -66,7 +71,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules and suffix // pretty URL with rules and suffix
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cacheID' => false, 'cache' => null,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
...@@ -87,6 +92,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -87,6 +92,7 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array( $manager = new UrlManager(array(
'baseUrl' => '/', 'baseUrl' => '/',
'hostInfo' => 'http://www.example.com', 'hostInfo' => 'http://www.example.com',
'cache' => null,
)); ));
$url = $manager->createAbsoluteUrl('post/view', array('id' => 1, 'title' => 'sample post')); $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); $this->assertEquals('http://www.example.com/?r=post/view&id=1&title=sample+post', $url);
...@@ -94,7 +100,9 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -94,7 +100,9 @@ class UrlManagerTest extends \yiiunit\TestCase
public function testParseRequest() public function testParseRequest()
{ {
$manager = new UrlManager; $manager = new UrlManager(array(
'cache' => null,
));
$request = new Request; $request = new Request;
// default setting without 'r' param // default setting without 'r' param
...@@ -115,6 +123,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -115,6 +123,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL without rules // pretty URL without rules
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cache' => null,
)); ));
// empty pathinfo // empty pathinfo
$request->pathInfo = ''; $request->pathInfo = '';
...@@ -136,7 +145,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -136,7 +145,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL rules // pretty URL rules
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cacheID' => false, 'cache' => null,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
...@@ -169,7 +178,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -169,7 +178,7 @@ class UrlManagerTest extends \yiiunit\TestCase
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'suffix' => '.html', 'suffix' => '.html',
'cacheID' => false, 'cache' => null,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
......
...@@ -10,7 +10,7 @@ class UrlRuleTest extends \yiiunit\TestCase ...@@ -10,7 +10,7 @@ class UrlRuleTest extends \yiiunit\TestCase
{ {
public function testCreateUrl() public function testCreateUrl()
{ {
$manager = new UrlManager; $manager = new UrlManager(array('cache' => null));
$suites = $this->getTestsForCreateUrl(); $suites = $this->getTestsForCreateUrl();
foreach ($suites as $i => $suite) { foreach ($suites as $i => $suite) {
list ($name, $config, $tests) = $suite; list ($name, $config, $tests) = $suite;
...@@ -25,7 +25,7 @@ class UrlRuleTest extends \yiiunit\TestCase ...@@ -25,7 +25,7 @@ class UrlRuleTest extends \yiiunit\TestCase
public function testParseRequest() public function testParseRequest()
{ {
$manager = new UrlManager; $manager = new UrlManager(array('cache' => null));
$request = new Request; $request = new Request;
$suites = $this->getTestsForParseRequest(); $suites = $this->getTestsForParseRequest();
foreach ($suites as $i => $suite) { foreach ($suites as $i => $suite) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment