InlineAction.php 1.66 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\base;
Qiang Xue committed
9

Qiang Xue committed
10 11
use Yii;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * InlineAction represents an action that is defined as a controller method.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16
 * The name of the controller method is available via [[actionMethod]] which
 * is set by the [[controller]] who creates this action.
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
19
 * @since 2.0
Qiang Xue committed
20
 */
Qiang Xue committed
21
class InlineAction extends Action
Qiang Xue committed
22
{
Qiang Xue committed
23 24 25 26 27 28 29 30 31 32 33
	/**
	 * @var string the controller method that  this inline action is associated with
	 */
	public $actionMethod;

	/**
	 * @param string $id the ID of this action
	 * @param Controller $controller the controller that owns this action
	 * @param string $actionMethod the controller method that  this inline action is associated with
	 * @param array $config name-value pairs that will be used to initialize the object properties
	 */
Alexander Makarov committed
34
	public function __construct($id, $controller, $actionMethod, $config = [])
Qiang Xue committed
35 36 37 38 39
	{
		$this->actionMethod = $actionMethod;
		parent::__construct($id, $controller, $config);
	}

Qiang Xue committed
40
	/**
Qiang Xue committed
41 42 43
	 * Runs this action with the specified parameters.
	 * This method is mainly invoked by the controller.
	 * @param array $params action parameters
44
	 * @return mixed the result of the action
Qiang Xue committed
45
	 */
Qiang Xue committed
46
	public function runWithParams($params)
Qiang Xue committed
47
	{
Qiang Xue committed
48
		$args = $this->controller->bindActionParams($this, $params);
Qiang Xue committed
49
		Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);
Qiang Xue committed
50 51 52
		if (Yii::$app->requestedParams === null) {
			Yii::$app->requestedParams = $args;
		}
Alexander Makarov committed
53
		return call_user_func_array([$this->controller, $this->actionMethod], $args);
Qiang Xue committed
54 55
	}
}