Allows you to use annotations for routing on Phalcon Micro Applications. While the built in Phalcon class for an annotation based router works on full suite Phalcon apps, Micro uses a different mounting methodology and does not support this. The Micro-Annotation Router leverages the build in Micro mounting method while allowing for annotation parsing with the caching method of your choice.
Git:
git clone https://github.com/SageITSolutions/phalcon-micro-annotations.git
Composer:
composer require sageit/phalcon-micro-annotations
This project consists of an included Router class extension which follows closely to the Phalcon 5 Namespace convention. Once a service is added in the micro app, this can easily be leveraged.
namespace App\Service;
class Router implements \Phalcon\Di\ServiceProviderInterface
{
public function register(\Phalcon\Di\DiInterface $di): void
{
$di->setShared(
'router',
function () use ($di) {
return new \Phalcon\Mvc\Router\Annotations\MicroRouter($di, (object) [
"adapter" => "apcu",
"directory" => "app/controllers/",
"namespace" => "App\Controllers",
"lifetime" => 21600000,
"cachedirectory" => "/app/storage/cache/annotations/"
]);
}
);
}
}
When creating the service, MicroRouter requires 2 parameters.
- The Dependancy Injector the service is added to.
- An Object containing optional settings.
The provided example demonstrates the default values assumed if the option is ommited.
From within the Micro class call:
$this->getDI()->get('router')->mountMicro($this);
this will pass the micro app ($this
) to the parsor and call the micro mount method using the parsed annotions
This class supports safeguarded Caching. You can choose between APCu
, Stream
, or Memory
(Not Caching) when creating the router service to optimize usage.
Within the Options array passed to the MicroRouter, specify the adapter of choice.
APCu
"adapter" => "apcu",
"lifetime" => 21600000,
APCu
"adapter" => "stream",
"cachedirectory" => "/app/storage/cache/annotations/"
Memory
"adapter" => "memory"
technically, Memory
is the fallback when all others fail, so any value not APCU or Stream would resolve to memory
Once specified, the class checks for the presence of the required components before using a caching method. This works in a tiered manner.
APCu
is specified, checks are made if APCu is installed and enabled, if not the adapter will revert to StreamStream
is specified (or APCu specification failed), checks are made that the directory specified forcachedirectory
exists, if not adapter will revert to MemoryMemory
is specified (or result of reverting), no checks are required, all parsing is done in memory each time.
When caching methods are enabled, they are processed first before iterating files producing I/O calls. If annotations are present, no files are parsed, and routes are generated from memory. When no annotations are present (first call or after clearing cache) then Controller files are parsed from the provided directory
and annotations are added.
A public utility method is included to facilitate the need to rebuild the cache either in testing or publishing scenarios. Clearing Cache is dependant on the adapter specified.
clearCache()
From within the micro class
$this->getDI()->get('router')->clearCache();
This will remove all APCu entries with the prefix _phan
or removing all files in the cachedirectory
depending on the adaptor type.
if the dependancy injector identifies as logger
service, the notices are logged indicating the cleared cache
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Sage IT Solutions - Email
Project Link: https://github.com/SageITSolutions/phalcon-micro-annotations