bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gewaer\Providers; |
||
| 6 | |||
| 7 | use function Gewaer\Core\appPath; |
||
| 8 | use Gewaer\Middleware\NotFoundMiddleware; |
||
| 9 | use Gewaer\Middleware\AuthenticationMiddleware; |
||
| 10 | use Gewaer\Middleware\TokenValidationMiddleware; |
||
| 11 | use Phalcon\Di\ServiceProviderInterface; |
||
| 12 | use Phalcon\DiInterface; |
||
| 13 | use Phalcon\Events\Manager; |
||
| 14 | use Phalcon\Mvc\Micro; |
||
| 15 | |||
| 16 | class RouterProvider implements ServiceProviderInterface |
||
| 17 | {
|
||
| 18 | /** |
||
| 19 | * {@inheritdoc}
|
||
| 20 | * |
||
| 21 | * @param DiInterface $container |
||
| 22 | */ |
||
| 23 | public function register(DiInterface $container) |
||
| 24 | {
|
||
| 25 | /** @var Micro $application */ |
||
| 26 | $application = $container->getShared('application');
|
||
| 27 | /** @var Manager $eventsManager */ |
||
| 28 | $eventsManager = $container->getShared('eventsManager');
|
||
| 29 | |||
| 30 | $this->attachRoutes($application); |
||
| 31 | $this->attachMiddleware($application, $eventsManager); |
||
| 32 | |||
| 33 | $application->setEventsManager($eventsManager); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Attaches the middleware to the application |
||
| 38 | * |
||
| 39 | * @param Micro $application |
||
| 40 | * @param Manager $eventsManager |
||
| 41 | */ |
||
| 42 | private function attachMiddleware(Micro $application, Manager $eventsManager) |
||
| 43 | {
|
||
| 44 | $middleware = $this->getMiddleware(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the events manager and attach the middleware to it |
||
| 48 | */ |
||
| 49 | foreach ($middleware as $class => $function) {
|
||
| 50 | $eventsManager->attach('micro', new $class());
|
||
| 51 | $application->{$function}(new $class());
|
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Attaches the routes to the application; lazy loaded |
||
| 57 | * |
||
| 58 | * @param Micro $application |
||
| 59 | */ |
||
| 60 | private function attachRoutes(Micro $application) |
||
| 61 | {
|
||
| 62 | $routes = $this->getRoutes(); |
||
| 63 | |||
| 64 | foreach ($routes as $route) {
|
||
| 65 | include $route; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the array for the middleware with the action to attach |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | private function getMiddleware(): array |
||
| 75 | {
|
||
| 76 | return [ |
||
| 77 | NotFoundMiddleware::class => 'before', |
||
| 78 | AuthenticationMiddleware::class => 'before', |
||
| 79 | TokenValidationMiddleware::class => 'before', |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the array for all the routes on this system |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | private function getRoutes(): array |
||
| 89 | {
|
||
| 90 | $path = appPath('api/routes');
|
||
| 91 | |||
| 92 | $routes = [ |
||
| 93 | 'api' => $path . '/api.php', |
||
| 94 | ]; |
||
| 95 | |||
| 96 | return $routes; |
||
| 97 | } |
||
| 98 | } |
||
| 99 |