Commit 9165a159 by Qiang Xue

cleanup.

parent 0d21ee67
......@@ -9,8 +9,6 @@
namespace yii\base;
use yii\util\ReflectionHelper;
/**
* Action is the base class for all controller action classes.
*
......@@ -21,6 +19,14 @@ use yii\util\ReflectionHelper;
* will be invoked by the controller when the action is requested.
* The `run()` method can have parameters which will be filled up
* with user input values automatically according to their names.
* For example, if the `run()` method is declared as follows:
*
* ~~~
* public function run($id, $type = 'book') { ... }
* ~~~
*
* And the parameters provided for the action are: `array('id' => 1)`.
* Then the `run()` method will be invoked as `run(1)` automatically.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -37,6 +43,7 @@ class Action extends Component
public $controller;
/**
* Constructor.
* @param string $id the ID of this action
* @param Controller $controller the controller that owns this action
* @param array $config name-value pairs that will be used to initialize the object properties
......@@ -51,7 +58,7 @@ class Action extends Component
/**
* Runs this action with the specified parameters.
* This method is mainly invoked by the controller.
* @param array $params action parameters
* @param array $params the parameters to be bound to the action's run() method.
* @return integer the exit status (0 means normal, non-zero means abnormal).
* @throws InvalidConfigException if the action class does not have a run() method
*/
......
......@@ -12,8 +12,7 @@ namespace yii\base;
/**
* ActionEvent represents the event parameter used for an action event.
*
* By setting the [[isValid]] property, one may control whether to continue the life cycle of
* the action currently being executed.
* By setting the [[isValid]] property, one may control whether to continue running the action.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -25,7 +24,7 @@ class ActionEvent extends Event
*/
public $action;
/**
* @var boolean whether the action is in valid state and its life cycle should proceed.
* @var boolean whether to continue running the action.
*/
public $isValid = true;
......@@ -34,7 +33,7 @@ class ActionEvent extends Event
* @param Action $action the action associated with this action event.
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct(Action $action, $config = array())
public function __construct($action, $config = array())
{
$this->action = $action;
parent::__construct($config);
......
<?php
/**
* ActionFilter class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ActionFilter is the base class for all action filters.
*
* A filter can be applied to a controller action at different stages of its life cycle. In particular,
* it responds to the following events that are raised when an action is being executed:
*
* 1. authorize
* 2. beforeAction
* 3. beforeRender
* 4. afterRender
* 5. afterAction
*
* Derived classes may respond to these events by overriding the corresponding methods in this class.
* For example, to create an access control filter, one may override the [[authorize()]] method.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActionFilter extends Behavior
{
/**
* @var Controller the owner of this behavior. For action filters, this should be a controller object.
*/
public $owner;
/**
* @var array IDs of actions that this filter applies to.
* If this property is empty or not set, it means this filter applies to all actions.
* Note that if an action appears in [[except]], the filter will not apply to this action, even
* if the action also appears in [[only]].
* @see exception
*/
public $only;
/**
* @var array IDs of actions that this filter does NOT apply to.
*/
public $except;
public function init()
{
$this->owner->on('authorize', array($this, 'handleEvent'));
$this->owner->on('beforeAction', array($this, 'handleEvent'));
$this->owner->on('beforeRender', array($this, 'handleEvent'));
$this->owner->getEventHandlers('afterRender')->insertAt(0, array($this, 'handleEvent'));
$this->owner->getEventHandlers('afterAction')->insertAt(0, array($this, 'handleEvent'));
}
public function authorize($event)
{
}
public function beforeAction($event)
{
}
public function beforeRender($event)
{
}
public function afterRender($event)
{
}
public function afterAction($event)
{
}
public function handleEvent($event)
{
if ($this->applyTo($event->action)) {
$this->{$event->name}($event);
}
}
public function applyTo(Action $action)
{
return (empty($this->only) || in_array($action->id, $this->only, false) !== false)
&& (empty($this->except) || in_array($action->id, $this->except, false) === false);
}
}
\ No newline at end of file
......@@ -15,13 +15,6 @@ use yii\util\StringHelper;
/**
* Controller is the base class for classes containing controller logic.
*
* Controller implements the action life cycles, which consist of the following steps:
*
* 1. [[authorize]]
* 2. [[beforeAction]]
* 3. [[afterAction]]
*
* @property array $actionParams the request parameters (name-value pairs) to be used for action parameter binding
* @property string $route the route (module ID, controller ID and action ID) of the current request.
* @property string $uniqueId the controller ID that is prefixed with the module ID (if any).
*
......@@ -30,7 +23,6 @@ use yii\util\StringHelper;
*/
class Controller extends Component
{
const EVENT_AUTHORIZE = 'authorize';
const EVENT_BEFORE_ACTION = 'beforeAction';
const EVENT_AFTER_ACTION = 'afterAction';
......@@ -117,7 +109,7 @@ class Controller extends Component
$oldAction = $this->action;
$this->action = $action;
if ($this->authorize($action) && $this->beforeAction($action)) {
if ($this->beforeAction($action)) {
$status = $action->runWithParams($params);
$this->afterAction($action);
} else {
......@@ -198,18 +190,6 @@ class Controller extends Component
}
/**
* This method is invoked when checking the access for the action to be executed.
* @param Action $action the action to be executed.
* @return boolean whether the action is allowed to be executed.
*/
public function authorize($action)
{
$event = new ActionEvent($action);
$this->trigger(self::EVENT_AUTHORIZE, $event);
return $event->isValid;
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param Action $action the action to be executed.
......@@ -273,6 +253,13 @@ class Controller extends Component
return $this->action !== null ? $this->getUniqueId() . '/' . $this->action->id : $this->getUniqueId();
}
/**
* Renders a view and applies layout if available.
*
* @param $view
* @param array $params
* @return string
*/
public function render($view, $params = array())
{
return $this->createView()->render($view, $params);
......
......@@ -52,11 +52,7 @@ class ErrorHandler extends Component
* @var \Exception the exception that is being handled currently
*/
public $exception;
/**
* @var boolean whether to log errors also using error_log(). Defaults to true.
* Note that errors captured by the error handler are always logged by [[\Yii::error()]].
*/
public $logErrors = true;
public function init()
{
......@@ -123,7 +119,7 @@ class ErrorHandler extends Component
protected function render($exception)
{
if ($this->errorAction !== null) {
\Yii::$application->runController($this->errorAction);
\Yii::$application->runAction($this->errorAction);
} elseif (\Yii::$application instanceof \yii\web\Application) {
if (!headers_sent()) {
$errorCode = $exception instanceof HttpException ? $exception->statusCode : 500;
......@@ -297,9 +293,6 @@ class ErrorHandler extends Component
$category .= '\\' . $exception->getSeverity();
}
\Yii::error((string)$exception, $category);
if ($this->logErrors) {
error_log($exception);
}
}
public function clearOutput()
......
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class Exception extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Exception';
}
......@@ -9,13 +9,11 @@
namespace yii\base;
use yii\util\ReflectionHelper;
/**
* InlineAction represents an action that is defined as a controller method.
*
* The name of the controller method should be in the format of `actionXyz`
* where `Xyz` stands for the action ID (e.g. `actionIndex`).
* The name of the controller method is available via [[actionMethod]] which
* is set by the [[controller]] who creates this action.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class InvalidCallException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Invalid Call Exception';
}
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class InvalidConfigException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Invalid Configuration Exception';
}
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class InvalidRequestException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Invalid Request Exception';
}
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class InvalidRouteException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Invalid Route Exception';
}
......@@ -27,6 +27,9 @@ use yii\util\FileHelper;
* @property string $uniqueId An ID that uniquely identifies this module among all modules within
* the current application.
* @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 $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 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 $import List of aliases to be imported. This property is write-only.
......@@ -240,8 +243,8 @@ abstract class Module extends Component
}
/**
* @return string the root directory of view files. Defaults to 'moduleDir/views' where
* moduleDir is the directory containing the module class.
* Returns the directory that contains the view files for this module.
* @return string the root directory of view files. Defaults to "[[basePath]]/view".
*/
public function getViewPath()
{
......@@ -263,8 +266,8 @@ abstract class Module extends Component
}
/**
* @return string the root directory of layout files. Defaults to 'moduleDir/views/layouts' where
* moduleDir is the directory containing the module class.
* Returns the directory that contains layout view files for this module.
* @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
*/
public function getLayoutPath()
{
......
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class NotSupportedException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Not Supported Exception';
}
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class UnknownMethodException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Unknown Method Exception';
}
......@@ -17,5 +17,9 @@ namespace yii\base;
*/
class UnknownPropertyException extends \Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Unknown Property Exception';
}
......@@ -18,6 +18,11 @@ namespace yii\db;
class Exception extends \yii\base\Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Database Exception';
/**
* @var mixed the error info provided by a PDO exception. This is the same as returned
* by [PDO::errorInfo](http://www.php.net/manual/en/pdo.errorinfo.php).
*/
......
<?php
/**
* ReflectionHelper class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\util;
use yii\base\Exception;
/**
* ReflectionHelper
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ReflectionHelper
{
/**
* Prepares parameters so that they can be bound to the specified method.
* This method converts the input parameters into an array that can later be
* passed to `call_user_func_array()` when calling the specified method.
* The conversion is based on the matching of method parameter names
* and the input array keys. For example,
*
* ~~~
* class Foo {
* function bar($a, $b) { ... }
* }
* $object = new Foo;
* $params = array('b' => 2, 'c' => 3, 'a' => 1);
* var_export(ReflectionHelper::extractMethodParams($object, 'bar', $params));
* // output: array('a' => 1, 'b' => 2);
* ~~~
*
* @param object|string $object the object or class name that owns the specified method
* @param string $method the method name
* @param array $params the parameters in terms of name-value pairs
* @return array parameters that are needed by the method only and
* can be passed to the method via `call_user_func_array()`.
* @throws Exception if any required method parameter is not found in the given parameters
*/
public static function extractMethodParams($object, $method, $params)
{
$m = new \ReflectionMethod($object, $method);
$ps = array();
foreach ($m->getParameters() as $param) {
$name = $param->getName();
if (array_key_exists($name, $params)) {
$ps[$name] = $params[$name];
} elseif ($param->isDefaultValueAvailable()) {
$ps[$name] = $param->getDefaultValue();
} else {
throw new Exception(\Yii::t('yii', 'Missing required parameter "{name}".', array('{name}' => $name)));
}
}
return $ps;
}
/**
* Initializes an object with the given parameters.
* Only the public non-static properties of the object will be initialized, and their names must
* match the given parameter names. For example,
*
* ~~~
* class Foo {
* public $a;
* protected $b;
* }
* $object = new Foo;
* $params = array('b' => 2, 'c' => 3, 'a' => 1);
* $remaining = ReflectionHelper::bindObjectParams($object, $params);
* var_export($object); // output: $object->a = 1; $object->b = null;
* var_export($remaining); // output: array('b' => 2, 'c' => 3);
* ~~~
*
* @param object $object the object whose properties are to be initialized
* @param array $params the input parameters to be used to initialize the object
* @return array the remaining unused input parameters
*/
public static function initObjectWithParams($object, $params)
{
if (empty($params)) {
return array();
}
$class = new \ReflectionClass(get_class($object));
foreach ($params as $name => $value) {
if ($class->hasProperty($name)) {
$property = $class->getProperty($name);
if ($property->isPublic() && !$property->isStatic()) {
$object->$name = $value;
unset($params[$name]);
}
}
}
return $params;
}
}
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