アノテーション パーサー ======================= アノテーションのパーサーがCで実装されたのは、PHPの世界では初めてのことです。:code:`Phalcon\Annotations` は、汎用的なコンポーネントで、アプリケーションで使われるPHPのクラスのアノテーションをパースしてキャッシュしておくことが、簡単にできます。 アノテーションはクラス、メソッド、プロパティのコメントブロックから読み込まれます。アノテーションはコメントブロック内のどこにでも配置することができます: .. code-block:: php get("Example"); // クラスのコメントブロックのアノテーションを読み取り $annotations = $reflector->getClassAnnotations(); // アノテーションをトラバースする foreach ($annotations as $annotation) { // アノテーション名を表示する echo $annotation->getName(), PHP_EOL; // 引数の数を表示する echo $annotation->numberArguments(), PHP_EOL; // 引数を表示する print_r($annotation->getArguments()); } アノテーションを読み取る処理は非常に高速ですが、パフォーマンス上の理由から、アダプタを使用してパースしたアノテーションを保存しておくことが推奨されます。アダプタは処理後のアノテーションをキャッシュし、何度もアノテーションを読み取らなくても良いようにします。 上記サンプルでは、 :doc:`Phalcon\\Annotations\\Adapter\\Memory <../api/Phalcon_Annotations_Adapter_Memory>` が使用されています。このアダプタはリクエストの間にだけ、キャッシュを行います。そのため、このアダプタは開発用に適しています。本番環境では、他のアダプタを使ってキャッシュを行うこともできます。 アノテーションの種類 -------------------- アノテーションは、パラメータを持つこともあれば持たないこともあります。パラメータには、単純なリテラル(文字列、数値、真偽値、null)、配列、連想配列、別のアノテーション、があります: .. code-block:: php attach( "dispatch", new CacheEnablerPlugin() ); $dispatcher = new MvcDispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }; CacheEnablerPluginはディスパッチャで実行された全てのアクションに割り込み、必要に応じてキャッシュを有効化します: .. code-block:: php annotations->getMethod( $dispatcher->getControllerClass(), $dispatcher->getActiveMethod() ); // メソッドに「Cache」というアノテーションがあるか確認する if ($annotations->has("Cache")) { // メソッドに「Cache」というアノテーションがある場合 $annotation = $annotations->get("Cache"); // キャッシュの有効期限を取得 $lifetime = $annotation->getNamedParameter("lifetime"); $options = [ "lifetime" => $lifetime, ]; // ユーザーが定義したキャッシュのキーがあるか確認する if ($annotation->hasNamedParameter("key")) { $options["key"] = $annotation->getNamedParameter("key"); } // 現在のメソッドのキャッシュを有効にする $this->view->cache($options); } } } これで、コントローラーでアノテーションを使えるようになりました: .. code-block:: php view->article = Articles::find(); } /** * This is a comment * * @Cache(key="my-key", lifetime=86400) */ public function showAction($slug) { $this->view->article = Articles::findFirstByTitle($slug); } } Private/Public areas with Annotations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ You can use annotations to tell the ACL which controllers belong to the administrative areas: .. code-block:: php getControllerClass(); // Possible method name $actionName = $dispatcher->getActiveMethod(); // Get annotations in the controller class $annotations = $this->annotations->get($controllerName); // The controller is private? if ($annotations->getClassAnnotations()->has("Private")) { // Check if the session variable is active? if (!$this->session->get("auth")) { // The user is no logged redirect to login $dispatcher->forward( [ "controller" => "session", "action" => "login", ] ); return false; } } // Continue normally return true; } } アノテーションアダプタ ---------------------- このコンポーネントはアダプタを利用して、パースした処理済みのアノテーションをキャッシュすることができ、パフォーマンスを向上させ開発・テストを便利にします: +------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Class | Description | +==========================================================================================+===================================================================================================================================================================================+ | :doc:`Phalcon\\Annotations\\Adapter\\Memory <../api/Phalcon_Annotations_Adapter_Memory>` | The annotations are cached only in memory. When the request ends the cache is cleaned reloading the annotations in each request. This adapter is suitable for a development stage | +------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :doc:`Phalcon\\Annotations\\Adapter\\Files <../api/Phalcon_Annotations_Adapter_Files>` | Parsed and processed annotations are stored permanently in PHP files improving performance. This adapter must be used together with a bytecode cache. | +------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :doc:`Phalcon\\Annotations\\Adapter\\Apc <../api/Phalcon_Annotations_Adapter_Apc>` | Parsed and processed annotations are stored permanently in the APC cache improving performance. This is the faster adapter | +------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :doc:`Phalcon\\Annotations\\Adapter\\Xcache <../api/Phalcon_Annotations_Adapter_Xcache>` | Parsed and processed annotations are stored permanently in the XCache cache improving performance. This is a fast adapter too | +------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 独自のアダプタを実装する ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :doc:`Phalcon\\Annotations\\AdapterInterface <../api/Phalcon_Annotations_AdapterInterface>` インターフェースを実装することで、独自のアノテーションアダプタを作成したり、既存のものを継承したりできます。 外部資料 ------------------ * `Tutorial: Creating a custom model's initializer with Annotations `_