Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


apricot:ext:interceptor

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
apricot:ext:interceptor [2020/05/25 21:07]
y2sunlight [ユーザコントローラ]
apricot:ext:interceptor [2020/06/08 16:25] (現在)
tanaka [Apricot インターセプター]
行 22: 行 22:
 インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。
  
-                  Middleware      Action +=== インターセプター構造 === 
-                 ┌──────────┐     ┌───────────────────────────────────────────┐ + 
-                 |          |      Interceptor   Interceptor   Real Action +{{:apricot:ext:ext-fig02.svg?nolink&800}}
-                          |      ┌─────────┐   ┌─────────┐   ┌──────────┐ | +
-  [Request ] --> | -------> | --> |--|-------->|-->|-------->|-->| ────┐    | |  +
-                    ↓          |    ↓    |      ↓    |          | | +
-                    ↓          └─────────┘   └─────────┘          | |  +
-                    ↓               ↓             ↓        |        | | +
-  [Response] <-- | <------  | <-- | <--------------------------- | <───┘    | | +
-                          |                                  └──────────┘ | +
-                 └──────────┘     └───────────────────────────────────────────┘+
  
 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。
行 69: 行 61:
     protected function intercept($actionName, $interceptors)     protected function intercept($actionName, $interceptors)
     {     {
-        $interceptor_arr is_array($interceptors) ? $interceptors array_slice(func_get_args(),1); +        if ($actionName == \Core\Application::getInstance()->getActionName())
-        if (!array_key_exists($actionName, $this->interceptors))+
         {         {
-            $this->interceptors[$actionName] array();+            $interceptor_arr = is_array($interceptors) ? $interceptors : array_slice(func_get_args(),1); 
 +            $this->interceptors = array_merge($this->interceptors , $interceptor_arr);
         }         }
-        $this->interceptors[$actionName] = array_merge($this->interceptors[$actionName] , $interceptor_arr); 
     }     }
  
行 100: 行 91:
  
         // Invoke Interceptor         // Invoke Interceptor
-        if (array_key_exists($actionName, $this->interceptors))+        $response = null; 
 +        foreach($this->interceptors as $interceptor)
         {         {
-            $response = null; +            if (is_callable($interceptor))
-            foreach($this->interceptors[$actionName] as $interceptor)+
             {             {
-                if (is_callable($interceptor))+                // Case of callable 
 +                $response = call_user_func_array($interceptor, $iparams); 
 +            } 
 +            elseif(strpos($interceptor,'@')!==false) 
 +            { 
 +                // Case of Controller/Action 
 +                list($class, $method) = explode('@', $interceptor); 
 +                if (empty($class))
                 {                 {
-                    // Case of callable +                    $instance = $this;
-                    $response call_user_func_array($interceptor, $iparams);+
                 }                 }
-                elseif(strpos($interceptor,'@')!==false)+                else
                 {                 {
-                    // Case of Controller/Action +                    $class = "\\App\\Controllers\\Interceptors\\{$class}"; 
-                    list($class, $method) = explode('@', $interceptor); +                    $instance = new $class;
-                    if (empty($class)) +
-                    { +
-                        $instance = $this; +
-                    } +
-                    else +
-                    { +
-                        $class = "\\App\\Controllers\\Interceptors\\{$class}"; +
-                        $instance = new $class+
-                    } +
- +
-                    // Call interceptor +
-                    $response = call_user_func_array(array($instance, $method), $iparams);+
                 }                 }
  
-                if ($response instanceof \Core\Foundation\Response) +                // Call interceptor 
-                +                $response = call_user_func_array(array($instance, $method), $iparams); 
-                    return $response; +            } 
-                }+ 
 +            if ($response instanceof \Core\Foundation\Response) 
 +            
 +                return $response;
             }             }
         }         }
行 144: 行 132:
  
   * ''intercept($actionName, $interceptors)'' メソッド   * ''intercept($actionName, $interceptors)'' メソッド
 +    * リクエストされているアクション(Application::getActionName())が対象となります。
     * アクションにインターセプターを追加します。     * アクションにインターセプターを追加します。
-    * BaseControllerは、インターセプターを配列( <nowiki>$this->$interceptors</nowiki> )で管理しています。+    * BaseControllerは、インターセプターを配列( <nowiki>$this->interceptors</nowiki> )で管理しています。
  
   * ''invokeAction($actionName, $params)'' メソッド   * ''invokeAction($actionName, $params)'' メソッド
apricot/ext/interceptor.1590408462.txt.gz · 最終更新: 2020/05/25 21:07 by y2sunlight