Annotations Parser ================== It is the first time that an annotations parser component is written in C for the PHP world. Phalcon\\Annotations is a general purpose component that provides ease of parsing and caching annotations in PHP classes to be used in applications. Annotations are read from docblocks in classes, methods and properties. An annotation can be placed at any position in the docblock: .. code-block:: php get('Example'); //Read the annotations in the class' docblock $annotations = $reflector->getClassAnnotations(); //Traverse the annotations foreach ($annotations as $annotation) { //Print the annotation name echo $annotation->getName(), PHP_EOL; //Print the number of arguments echo $annotation->numberArguments(), PHP_EOL; //Print the arguments print_r($annotation->getArguments()); } The annotation reading process is very fast, however, for performance reasons it is recommended to store the parsed annotations using an adapter. Adapters cache the processed annotations avoiding the need of parse the annotations again and again. :doc:`Phalcon\\Annotations\\Adapter\\Memory <../api/Phalcon_Annotations_Adapter_Memory>` was used in the above example. This adapter only caches the annotations while the request is running, for this reason th adapter is more suitable for development. There are other adapters to swap out when the application is in production stage. Types of Annotations -------------------- Annotations may have parameters or not. A parameter could be a simple literal (strings, number, boolean, null), an array, a hashed list or other annotation: .. code-block:: php attach('dispatch', new CacheEnablerPlugin()); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }; CacheEnablerPlugin is a plugin that intercept every action executed in the dispatcher enabling the cache if needed: .. code-block:: php annotations->getMethod( $dispatcher->getActiveController(), $dispatcher->getActiveMethod() ); //Check if the method has an annotation 'Cache' if ($annotations->has('Cache')) { //The method has the annotation 'Cache' $annotation = $annotations->get('Cache'); //Get the lifetime $lifetime = $annotation->getNamedParameter('lifetime'); $options = array('lifetime' => $lifetime); //Check if there is an user defined cache key if ($annotation->hasNamedParameter('key')) { $options['key'] = $annotation->getNamedParameter('key'); } //Enable the cache for the current method $this->view->cache($options); } } } Now, we can use the annotation in a controller: .. 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); } } Annotations Adapters -------------------- This component makes use of adapters to cache or no cache the parsed and processed annotations thus improving the performance or prodiving facilities to development/testing: +------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+ | Name | Description | API | +============+======================================================================================================================================================================================================================================+==========================================================================================+ | 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\\Memory <../api/Phalcon_Annotations_Adapter_Memory>` | +------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+ | 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\\Files <../api/Phalcon_Annotations_Adapter_Files>` | +------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+ | APC | Parsed and processed annotations are stored permanently in the APC cache improving performance. This is the faster adapter | :doc:`Phalcon\\Annotations\\Adapter\\Apc <../api/Phalcon_Annotations_Adapter_Apc>` | +------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+ | XCache | Parsed and processed annotations are stored permanently in the XCache cache improving performance. This is a fast adapter too | :doc:`Phalcon\\Annotations\\Adapter\\XCache <../api/Phalcon_Annotations_Adapter_XCache>` | +------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+ Implementing your own adapters ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The :doc:`Phalcon\\Annotations\\AdapterInterface <../api/Phalcon_Annotations_AdapterInterface>` interface must be implemented in order to create your own annotations adapters or extend the existing ones. External Resources ------------------ * `Tutorial: Creating a custom model’s initializer with Annotations `_