ActionFilter类

操作过滤器基类ActionFilter

yii2\base\ActionFilter.php。


 * @since 2.0
 */
class ActionFilter extends Behavior
{
    /**
     * @var array list of action IDs that this filter should apply to. If this property is not set,
     * then the filter applies to all actions, unless they are listed in [[except]].
     * 操作标识列表。如果该属性未设置,过滤器适用于所有的行动,除非它们被列入[[except]]中。
     * If an action ID appears in both [[only]] and [[except]], this filter will NOT apply to it.
     * 如果一个操作ID 出现在[[only]] 和[[except]]中,该筛选器将不适用它
     * Note that if the filter is attached to a module, the action IDs should also include child module IDs (if any)
     * and controller IDs.
     * 如果过滤器是链接到一个模块,操作检测还应包括子模块和控制器
     *
     * @see except
     */
    public $only;
    /**
     * @var array list of action IDs that this filter should not apply to.
     * 此筛选器不应适用于操作ID。
     * @see only
     */
    public $except = [];


    /**
     * @inheritdoc
     * 将行为对象附加到组件。
     */
    public function attach($owner)
    {
        $this->owner = $owner;
        $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
    }

    /**
     * @inheritdoc
     * 将行为对象和组件分离。
     */
    public function detach()
    {
        if ($this->owner) {
            $this->owner->off(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
            $this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
            $this->owner = null;
        }
    }

    /**
     * @param ActionEvent $event    在动作之前调用
     */
    public function beforeFilter($event)
    {
        if (!$this->isActive($event->action)) {
            return;
        }

        $event->isValid = $this->beforeAction($event->action);
        if ($event->isValid) {
            // call afterFilter only if beforeFilter succeeds    beforeFilter 执行成功调用afterFilter
            // beforeFilter and afterFilter should be properly nested  两者要配合应用
            $this->owner->on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false);
        } else {
            $event->handled = true;
        }
    }

    /**
     * @param ActionEvent $event
     */
    public function afterFilter($event)
    {
        $event->result = $this->afterAction($event->action, $event->result);
        $this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
    }

    /**
     * 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.要执行的动作
     * @return boolean whether the action should continue to be executed.
     * 是否应继续执行该动作。
     */
    public function beforeAction($action)
    {
        return true;
    }

    /**
     * This method is invoked right after an action is executed.
     * 此方法是在执行动作之后调用的。
     * You may override this method to do some postprocessing for the action.
     * @param Action $action the action just executed.  刚刚执行的动作
     * @param mixed $result the action execution result 行动执行结果
     * @return mixed the processed action result.   处理结果。
     */
    public function afterAction($action, $result)
    {
        return $result;
    }

    /**
     * Returns a value indicating whether the filer is active for the given action.
     * 返回一个值,给定的过滤器的行动是否为是积极的。
     * @param Action $action the action being filtered 被过滤的动作
     * @return boolean whether the filer is active for the given action.
     * 给定的过滤器的行动是否为是积极的。
     */
    protected function isActive($action)
    {
        if ($this->owner instanceof Module) {
            // convert action uniqueId into an ID relative to the module
            $mid = $this->owner->getUniqueId();
            $id = $action->getUniqueId();
            if ($mid !== '' && strpos($id, $mid) === 0) {
                $id = substr($id, strlen($mid) + 1);
            }
        } else {
            $id = $action->id;
        }
        return !in_array($id, $this->except, true) && (empty($this->only) || in_array($id, $this->only, true));
    }
}


你可能感兴趣的:(yii2源码分析)