Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #343 from Yiivgeny/CConsoleCommandEnhancements

Adding full-featured events for CConsoleCommand
  • Loading branch information...
commit c73abcff94d2648b0c2efd849bf1b68bb0342ae5 2 parents e176528 + 1a92495
@qiangxue qiangxue authored
View
1  CHANGELOG
@@ -4,6 +4,7 @@
Version 1.1.11 work in progress
-------------------------------
+- Enh: Added full-featured behaviors and events CConsoleCommand::onBeforeAction & CConsoleCommand::onAfterAction (Yiivgeny)
- Enh #171: Added support for PUT and DELETE request tunneled through POST via parameter named _method in POST body (musterknabe)
- Enh #266: Add support for HTML5 url, email, number, range and date fields to CHtml (gregmolnar)
- Bug #193: Changed datetime column type for postgresql from 'time' to 'timestamp' (cebe)
View
2  framework/YiiBase.php
@@ -677,6 +677,8 @@ public static function registerAutoloader($callback, $append=false)
'CConsoleApplication' => '/console/CConsoleApplication.php',
'CConsoleCommand' => '/console/CConsoleCommand.php',
'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
+ 'CConsoleCommandEvent' => '/console/CConsoleCommandEvent.php',
+ 'CConsoleCommandBehavior' => '/console/CConsoleCommandBehavior.php',
'CHelpCommand' => '/console/CHelpCommand.php',
'CDbCommand' => '/db/CDbCommand.php',
'CDbConnection' => '/db/CDbConnection.php',
View
62 framework/console/CConsoleCommand.php
@@ -65,6 +65,7 @@ public function __construct($name,$runner)
{
$this->_name=$name;
$this->_runner=$runner;
+ $this->attachBehaviors($this->behaviors());
}
/**
@@ -76,6 +77,32 @@ public function __construct($name,$runner)
public function init()
{
}
+
+ /**
+ * Returns a list of behaviors that this command should behave as.
+ * The return value should be an array of behavior configurations indexed by
+ * behavior names. Each behavior configuration can be either a string specifying
+ * the behavior class or an array of the following structure:
+ * <pre>
+ * 'behaviorName'=>array(
+ * 'class'=>'path.to.BehaviorClass',
+ * 'property1'=>'value1',
+ * 'property2'=>'value2',
+ * )
+ * </pre>
+ *
+ * Note, the behavior classes must implement {@link IBehavior} or extend from
+ * {@link CBehavior}. Behaviors declared in this method will be attached
+ * to the controller when it is instantiated.
+ *
+ * For more details about behaviors, see {@link CComponent}.
+ * @return array the behavior configurations (behavior name=>behavior configuration)
+ * @since 1.1.11
+ */
+ public function behaviors()
+ {
+ return array();
+ }
/**
* Executes the command.
@@ -152,7 +179,16 @@ public function run($args)
*/
protected function beforeAction($action,$params)
{
- return true;
+ if($this->hasEventHandler('onBeforeAction'))
+ {
+ $event = new CConsoleCommandEvent($this, $params, $action);
+ $this->onBeforeAction($event);
+ return !$event->stopCommand;
+ }
+ else
+ {
+ return true;
+ }
}
/**
@@ -163,6 +199,8 @@ protected function beforeAction($action,$params)
*/
protected function afterAction($action,$params)
{
+ if($this->hasEventHandler('onAfterAction'))
+ $this->onAfterAction(new CConsoleCommandEvent($this, $params, $action));
}
/**
@@ -499,4 +537,24 @@ public function confirm($message)
echo $message.' [yes|no] ';
return !strncasecmp(trim(fgets(STDIN)),'y',1);
}
-}
+
+ /**
+ * This event is raised before an action is to be executed.
+ * @param CConsoleCommandEvent $event the event parameter
+ * @since 1.1.11
+ */
+ public function onBeforeAction($event)
+ {
+ $this->raiseEvent('onBeforeAction',$event);
+ }
+
+ /**
+ * This event is raised after an action finishes execution.
+ * @param CConsoleCommandEvent $event the event parameter
+ * @since 1.1.11
+ */
+ public function onAfterAction($event)
+ {
+ $this->raiseEvent('onAfterAction',$event);
+ }
+}
View
53 framework/console/CConsoleCommandBehavior.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * CConsoleCommandBehavior class file.
+ *
+ * @author Evgeny Blinov <e.a.blinov@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CConsoleCommandBehavior is a base class for behaviors that are attached to a console command component.
+ *
+ * @property CConsoleCommand $owner The owner model that this behavior is attached to.
+ *
+ * @author Evgeny Blinov <e.a.blinov@gmail.com>
+ * @package system.console
+ * @since 1.1.11
+ */
+class CConsoleCommandBehavior extends CBehavior
+{
+ /**
+ * Declares events and the corresponding event handler methods.
+ * The default implementation returns 'onAfterConstruct', 'onBeforeValidate' and 'onAfterValidate' events and handlers.
+ * If you override this method, make sure you merge the parent result to the return value.
+ * @return array events (array keys) and the corresponding event handler methods (array values).
+ * @see CBehavior::events
+ */
+ public function events()
+ {
+ return array(
+ 'onBeforeAction' => 'beforeAction',
+ 'onAfterAction' => 'afterAction'
+ );
+ }
+ /**
+ * Responds to {@link CConsoleCommand::onBeforeAction} event.
+ * Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
+ * @param CConsoleCommandEvent $event event parameter
+ */
+ protected function beforeAction($event)
+ {
+ }
+
+ /**
+ * Responds to {@link CConsoleCommand::onAfterAction} event.
+ * Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
+ * @param CConsoleCommandEvent $event event parameter
+ */
+ protected function afterAction($event)
+ {
+ }
+}
View
44 framework/console/CConsoleCommandEvent.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * CConsoleCommandEvent class file.
+ *
+ * @author Evgeny Blinov <e.a.blinov@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CConsoleCommandEvent class.
+ *
+ * CConsoleCommandEvent represents the event parameters needed by events raised by a console command.
+ *
+ * @author Evgeny Blinov <e.a.blinov@gmail.com>
+ * @package system.console
+ * @since 1.1.11
+ */
+class CConsoleCommandEvent extends CEvent
+{
+ /**
+ * @var string the action name
+ */
+ public $action;
+ /**
+ * @var boolean whether the action should be executed.
+ * If this property is set false by the event handler, the console command action will quit after handling this event.
+ * If true, the normal execution cycles will continue, including performing the action and calling
+ * {@link CConsoleCommand::afterAction}.
+ */
+ public $stopCommand=false;
+
+ /**
+ * Constructor.
+ * @param mixed $sender sender of the event
+ * @param string $params the parameters to be passed to the action method.
+ * @param string $action the action name
+ */
+ public function __construct($sender=null,$params=null,$action=null){
+ parent::__construct($sender,$params);
+ $this->action=$action;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.