このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
apricot:ext:interceptor [2020/05/25 21:07] y2sunlight [ユーザコントローラ] |
apricot:ext:interceptor [2020/06/08 16:25] (現在) tanaka [Apricot インターセプター] |
||
---|---|---|---|
行 22: | 行 22: | ||
インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 | ||
- | Middleware | + | === インターセプター構造 === |
- | ┌──────────┐ | + | |
- | | | | + | {{: |
- | | + | |
- | [Request ] --> | -------> | --> |--|--------> | + | |
- | | + | |
- | | + | |
- | | + | |
- | [Response] <-- | < | + | |
- | | + | |
- | | + | |
上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 | ||
行 69: | 行 61: | ||
protected function intercept($actionName, | protected function intercept($actionName, | ||
{ | { | ||
- | $interceptor_arr | + | |
- | if (!array_key_exists($actionName, | + | |
{ | { | ||
- | $this-> | + | |
+ | | ||
} | } | ||
- | $this-> | ||
} | } | ||
行 100: | 行 91: | ||
// Invoke Interceptor | // Invoke Interceptor | ||
- | | + | |
+ | foreach($this-> | ||
{ | { | ||
- | | + | |
- | foreach($this-> | + | |
{ | { | ||
- | | + | |
+ | $response = call_user_func_array($interceptor, | ||
+ | } | ||
+ | elseif(strpos($interceptor,' | ||
+ | { | ||
+ | // Case of Controller/ | ||
+ | list($class, | ||
+ | if (empty($class)) | ||
{ | { | ||
- | | + | $instance |
- | | + | |
} | } | ||
- | | + | |
{ | { | ||
- | | + | $class = " |
- | list($class, | + | $instance = new $class; |
- | if (empty($class)) | + | |
- | { | + | |
- | $instance = $this; | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | | + | |
- | $instance = new $class; | + | |
- | } | + | |
- | + | ||
- | // Call interceptor | + | |
- | $response = call_user_func_array(array($instance, | + | |
} | } | ||
- | if ($response instanceof \Core\Foundation\Response) | + | |
- | { | + | $response = call_user_func_array(array($instance, |
- | return $response; | + | } |
- | } | + | |
+ | | ||
+ | { | ||
+ | return $response; | ||
} | } | ||
} | } | ||
行 144: | 行 132: | ||
* '' | * '' | ||
+ | * リクエストされているアクション(Application:: | ||
* アクションにインターセプターを追加します。 | * アクションにインターセプターを追加します。 | ||
- | * BaseControllerは、インターセプターを配列( < | + | * BaseControllerは、インターセプターを配列( < |
* '' | * '' |