|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Phalcon API. |
| 5 | + * |
| 6 | + * (c) Phalcon Team <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Phalcon\Api\Domain\Services\Providers; |
| 15 | + |
| 16 | +use Phalcon\Api\Domain\Interfaces\DomainInterface; |
| 17 | +use Phalcon\Api\Domain\Interfaces\ResponderInterface; |
| 18 | +use Phalcon\Api\Domain\Interfaces\RoutesInterface; |
| 19 | +use Phalcon\Api\Domain\Services\ActionHandler; |
| 20 | +use Phalcon\Api\Domain\Services\Container; |
| 21 | +use Phalcon\Di\DiInterface; |
| 22 | +use Phalcon\Di\ServiceProviderInterface; |
| 23 | +use Phalcon\Events\Manager as EventsManager; |
| 24 | +use Phalcon\Mvc\Micro; |
| 25 | + |
| 26 | +/** |
| 27 | + * @phpstan-import-type TMiddleware from RoutesInterface |
| 28 | + */ |
| 29 | +class RouterProvider implements ServiceProviderInterface |
| 30 | +{ |
| 31 | + public function register(DiInterface $container): void |
| 32 | + { |
| 33 | + /** @var Micro $application */ |
| 34 | + $application = $container->getShared(Container::APPLICATION); |
| 35 | + /** @var EventsManager $eventsManager */ |
| 36 | + $eventsManager = $container->getShared(Container::EVENTS_MANAGER); |
| 37 | + |
| 38 | + $this->attachRoutes($application); |
| 39 | + $this->attachMiddleware($application, $eventsManager); |
| 40 | + |
| 41 | + $application->get('/health', function () { |
| 42 | + /** empty */ |
| 43 | + }); |
| 44 | + |
| 45 | + $application->setEventsManager($eventsManager); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param Micro $application |
| 50 | + * @param EventsManager $eventsManager |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + private function attachMiddleware( |
| 55 | + Micro $application, |
| 56 | + EventsManager $eventsManager |
| 57 | + ): void { |
| 58 | + /** @var TMiddleware $middleware */ |
| 59 | + $middleware = RoutesEnum::middleware(); |
| 60 | + foreach ($middleware as $service => $method) { |
| 61 | + $instance = $application->getService($service); |
| 62 | + $eventsManager->attach('micro', $instance); |
| 63 | + $application->$method($instance); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Attaches routes to the application, lazy loaded |
| 69 | + * |
| 70 | + * @param Micro $application |
| 71 | + * |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + private function attachRoutes(Micro $application): void |
| 75 | + { |
| 76 | + /** @var ResponderInterface $responder */ |
| 77 | + $responder = $application->getService(Container::RESPONDER_JSON); |
| 78 | + |
| 79 | + /** @var array<RoutesInterface> $routes */ |
| 80 | + $routes = RoutesEnum::cases(); |
| 81 | + foreach ($routes as $route) { |
| 82 | + $serviceName = $route->service(); |
| 83 | + $collection = new Micro\Collection(); |
| 84 | + /** @var DomainInterface $service */ |
| 85 | + $service = $application->getService($serviceName); |
| 86 | + $action = new ActionHandler($service, $responder); |
| 87 | + |
| 88 | + $collection |
| 89 | + ->setHandler($action) |
| 90 | + ->setPrefix($route->prefix()) |
| 91 | + ->{$route->method()}( |
| 92 | + $route->suffix(), |
| 93 | + '__invoke' |
| 94 | + ) |
| 95 | + ; |
| 96 | + |
| 97 | + $application->mount($collection); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments