Ground Sunlight

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

ユーザ用ツール

サイト用ツール


apricot:ext:interceptor

差分

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

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

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
apricot:ext:interceptor [2020/05/25 20:49]
y2sunlight
apricot:ext:interceptor [2020/06/08 16:25] (現在)
tanaka [Apricot インターセプター]
行 1: 行 1:
-> 編集中 
- 
------ 
- 
 ====== Apricot インターセプター ====== ====== Apricot インターセプター ======
  --- //[[http://www.y2sunlight.com|y2sunlight]] 2020-05-25//  --- //[[http://www.y2sunlight.com|y2sunlight]] 2020-05-25//
行 26: 行 22:
 インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。 インターセプター とはアクションの前処理の事です。ミドルウェアと同じでリクエストを中断してレスポンスオブジェクトを生成することもできますが、アクションの後処理はできません。これを図示すると以下のようになります。
  
-                  Middleware      Action +=== インターセプター構造 === 
-                 ┌──────────┐     ┌───────────────────────────────────────────┐ + 
-                 |          |      Interceptor   Interceptor   Real Action +{{:apricot:ext:ext-fig02.svg?nolink&800}}
-                          |      ┌─────────┐   ┌─────────┐   ┌──────────┐ | +
-  [Request ] --> | -------> | --> |--|-------->|-->|-------->|-->| ────┐    | |  +
-                    ↓          |    ↓    |      ↓    |          | | +
-                    ↓          └─────────┘   └─────────┘          | |  +
-                    ↓               ↓             ↓        |        | | +
-  [Response] <-- | <------  | <-- | <--------------------------- | <───┘    | | +
-                          |                                  └──────────┘ | +
-                 └──────────┘     └───────────────────────────────────────────┘+
  
 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。 上図から分かるようにミドルウェアパイプラインから見ると、インターセプターはアクションに含まれます。ミドルウェアとの一番の違いは、ミドルウェアは基本的に全てのコントローラを対象としているのに対し、インターセプターは、各コントローラで独自に設定ができるという点です。
行 73: 行 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); 
     }     }
  
行 104: 行 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;
             }             }
         }         }
行 148: 行 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)'' メソッド
行 162: 行 147:
 ===== インターセプターの使用 ===== ===== インターセプターの使用 =====
  
-インターセプターの登録は、コントローラーのコンストラクタで ''intercept()'' メソッドを使って行います。インターセプターにはクロージャーとメソッドの両方が使用できます。簡単なバリデーション処理ならクロジャーで以下のように書きます。+インターセプターの登録は、コントローラーのコンストラクタで ''intercept()'' メソッドを使って行います。インターセプターにはクロージャーとメソッドの両方が使用できます。簡単なバリデーション処理ならクロジャーで以下のように書きます。
  
 <code php> <code php>
行 180: 行 165:
 </code> </code>
  
-インターセプターに渡される引数は、第1引数に、コントローラのインスタンスが、その後にアクションと同じの引数が続きます。+インターセプターに渡される引数は、第1引数に、コントローラのインスタンスが、その後にアクションと同じの引数が続きます。また、インターセプターはレスポンスオブジェクトを返して以降のアクションを中止することができます。
  
-''intercept()'' にメソッドを使用する場合は、以下のように <nowiki>'クラス名@メソッド名'</nowiki> の形式で指定します。+インターセプターにメソッドを使用する場合は、以下のように <nowiki>'クラス名@メソッド名'</nowiki> の形式で指定します。
  
 <code php> <code php>
行 220: 行 205:
 }); });
 </code> </code>
-  - login() メソッド内からバリデーション( validate() ) の呼び出しを削除します。+  - login() メソッド内からバリデーション( validate() ) の呼び出し部分を削除します。
   - validate() を削除します。   - validate() を削除します。
  
-以下に修正後の AuthController.php を示します。+以下に修正後の最終的な AuthController.php を示します。
  
 {{fa>folder-open-o}} ** /apricot/app/Controllers ** {{fa>folder-open-o}} ** /apricot/app/Controllers **
行 262: 行 247:
         });         });
     }     }
-    ...+ 
 +    /** 
 +     * ログインフォーム表示 
 +     * @return \Core\Foundation\Response 
 +     */ 
 +    public function showForm() 
 +    { 
 +        if (AuthUser::check()) 
 +        { 
 +            // 認証済ならトップ画面表示 
 +            return redirect(route('')); 
 +        } 
 + 
 +        if (AuthUser::remember()) 
 +        { 
 +            // 自動認証できたらトップ画面表示 
 +            return redirect(route('')); 
 +        } 
 + 
 +        return render('login'); 
 +    }
  
     /**     /**
行 282: 行 287:
         return redirect(AuthUser::getPathAfterLogin());         return redirect(AuthUser::getPathAfterLogin());
     }     }
-    ...+ 
 +    /** 
 +     * ログアウト 
 +     * @return \Core\Foundation\Response 
 +     */ 
 +    public function logout() 
 +    { 
 +        // セッションの破棄 
 +        AuthUser::forget(); 
 + 
 +        // ログイン画面表示 
 +        return redirect(route("login")); 
 +    }
 } }
 </code> </code>
行 333: 行 350:
         // モデル         // モデル
         $this->user = new User();         $this->user = new User();
 +
 +        // インターセプター登録
 +        $this->intercept('insert', 'UserInterceptor@insert');
 +        $this->intercept('update', 'UserInterceptor@update');
  
         // トランザクションアクション登録         // トランザクションアクション登録
行 366: 行 387:
     public function insert()     public function insert()
     {     {
-        // バリデーション 
-        $response = (new Interceptors\UserInterceptor())->insert($this); 
-        if ($response instanceof \Core\Foundation\Response) 
-        { 
-            return $response; 
-        } 
- 
         $inputs = Input::all();         $inputs = Input::all();
  
行 414: 行 428:
     public function update(int $id)     public function update(int $id)
     {     {
-        // バリデーション 
-        $response = (new Interceptors\UserInterceptor())->update($this, $id); 
-        if ($response instanceof \Core\Foundation\Response) 
-        { 
-            return $response; 
-        } 
- 
         $inputs = Input::all();         $inputs = Input::all();
  
apricot/ext/interceptor.1590407359.txt.gz · 最終更新: 2020/05/25 20:49 by y2sunlight