From 9165a1592a499c6a67610ee90815ecb0d1c83a11 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 29 Jan 2013 09:01:38 -0500 Subject: [PATCH] cleanup. --- framework/base/Action.php | 13 +++- framework/base/ActionEvent.php | 7 +- framework/base/ActionFilter.php | 90 ------------------------ framework/base/Controller.php | 29 +++----- framework/base/ErrorHandler.php | 11 +-- framework/base/Exception.php | 4 ++ framework/base/InlineAction.php | 6 +- framework/base/InvalidCallException.php | 4 ++ framework/base/InvalidConfigException.php | 4 ++ framework/base/InvalidRequestException.php | 4 ++ framework/base/InvalidRouteException.php | 4 ++ framework/base/Module.php | 11 +-- framework/base/NotSupportedException.php | 4 ++ framework/base/UnknownMethodException.php | 4 ++ framework/base/UnknownPropertyException.php | 4 ++ framework/db/Exception.php | 5 ++ framework/util/ReflectionHelper.php | 103 ---------------------------- 17 files changed, 69 insertions(+), 238 deletions(-) delete mode 100644 framework/base/ActionFilter.php delete mode 100644 framework/util/ReflectionHelper.php diff --git a/framework/base/Action.php b/framework/base/Action.php index 6925500..8d4ec5a 100644 --- a/framework/base/Action.php +++ b/framework/base/Action.php @@ -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 * @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 */ diff --git a/framework/base/ActionEvent.php b/framework/base/ActionEvent.php index 1b3e07d..ee945a8 100644 --- a/framework/base/ActionEvent.php +++ b/framework/base/ActionEvent.php @@ -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 * @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); diff --git a/framework/base/ActionFilter.php b/framework/base/ActionFilter.php deleted file mode 100644 index 9cb5331..0000000 --- a/framework/base/ActionFilter.php +++ /dev/null @@ -1,90 +0,0 @@ - - * @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 diff --git a/framework/base/Controller.php b/framework/base/Controller.php index 39c829d..5b7b460 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -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); diff --git a/framework/base/ErrorHandler.php b/framework/base/ErrorHandler.php index 9f1621a..c17755e 100644 --- a/framework/base/ErrorHandler.php +++ b/framework/base/ErrorHandler.php @@ -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() diff --git a/framework/base/Exception.php b/framework/base/Exception.php index a740a35..89fa30a 100644 --- a/framework/base/Exception.php +++ b/framework/base/Exception.php @@ -17,5 +17,9 @@ namespace yii\base; */ class Exception extends \Exception { + /** + * @var string the user-friend name of this exception + */ + public $name = 'Exception'; } diff --git a/framework/base/InlineAction.php b/framework/base/InlineAction.php index 4c509a3..00ecb8f 100644 --- a/framework/base/InlineAction.php +++ b/framework/base/InlineAction.php @@ -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 * @since 2.0 diff --git a/framework/base/InvalidCallException.php b/framework/base/InvalidCallException.php index 24e7b6e..e9d81b4 100644 --- a/framework/base/InvalidCallException.php +++ b/framework/base/InvalidCallException.php @@ -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'; } diff --git a/framework/base/InvalidConfigException.php b/framework/base/InvalidConfigException.php index 5256d7e..8f292df 100644 --- a/framework/base/InvalidConfigException.php +++ b/framework/base/InvalidConfigException.php @@ -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'; } diff --git a/framework/base/InvalidRequestException.php b/framework/base/InvalidRequestException.php index 2e2a04a..38c4115 100644 --- a/framework/base/InvalidRequestException.php +++ b/framework/base/InvalidRequestException.php @@ -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'; } diff --git a/framework/base/InvalidRouteException.php b/framework/base/InvalidRouteException.php index 8e82b40..6549c07 100644 --- a/framework/base/InvalidRouteException.php +++ b/framework/base/InvalidRouteException.php @@ -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'; } diff --git a/framework/base/Module.php b/framework/base/Module.php index 23bd577..966cc64 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -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() { diff --git a/framework/base/NotSupportedException.php b/framework/base/NotSupportedException.php index aa2badb..2da008c 100644 --- a/framework/base/NotSupportedException.php +++ b/framework/base/NotSupportedException.php @@ -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'; } diff --git a/framework/base/UnknownMethodException.php b/framework/base/UnknownMethodException.php index 8667d24..b88d97f 100644 --- a/framework/base/UnknownMethodException.php +++ b/framework/base/UnknownMethodException.php @@ -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'; } diff --git a/framework/base/UnknownPropertyException.php b/framework/base/UnknownPropertyException.php index 69581f9..601ae28 100644 --- a/framework/base/UnknownPropertyException.php +++ b/framework/base/UnknownPropertyException.php @@ -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'; } diff --git a/framework/db/Exception.php b/framework/db/Exception.php index bdc1277..61a8e58 100644 --- a/framework/db/Exception.php +++ b/framework/db/Exception.php @@ -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). */ diff --git a/framework/util/ReflectionHelper.php b/framework/util/ReflectionHelper.php deleted file mode 100644 index cc13f94..0000000 --- a/framework/util/ReflectionHelper.php +++ /dev/null @@ -1,103 +0,0 @@ - - * @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; - } -}