1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
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 | 4 | public function register(DiInterface $container) |
|
24 | { |
||
25 | /** @var Micro $application */ |
||
26 | 4 | $application = $container->getShared('application'); |
|
27 | /** @var Manager $eventsManager */ |
||
28 | 4 | $eventsManager = $container->getShared('eventsManager'); |
|
29 | |||
30 | 4 | $this->attachRoutes($application); |
|
31 | 4 | $this->attachMiddleware($application, $eventsManager); |
|
32 | |||
33 | 4 | $application->setEventsManager($eventsManager); |
|
34 | 4 | } |
|
35 | |||
36 | /** |
||
37 | * Attaches the middleware to the application |
||
38 | * |
||
39 | * @param Micro $application |
||
40 | * @param Manager $eventsManager |
||
41 | */ |
||
42 | 4 | private function attachMiddleware(Micro $application, Manager $eventsManager) |
|
43 | { |
||
44 | 4 | $middleware = $this->getMiddleware(); |
|
45 | |||
46 | /** |
||
47 | * Get the events manager and attach the middleware to it |
||
48 | */ |
||
49 | 4 | foreach ($middleware as $class => $function) { |
|
50 | 4 | $eventsManager->attach('micro', new $class()); |
|
51 | 4 | $application->{$function}(new $class()); |
|
52 | } |
||
53 | 4 | } |
|
54 | |||
55 | /** |
||
56 | * Attaches the routes to the application; lazy loaded |
||
57 | * |
||
58 | * @param Micro $application |
||
59 | */ |
||
60 | 4 | private function attachRoutes(Micro $application) |
|
61 | { |
||
62 | 4 | $routes = $this->getRoutes(); |
|
63 | |||
64 | 4 | foreach ($routes as $route) { |
|
65 | 4 | include $route; |
|
66 | } |
||
67 | 4 | } |
|
68 | |||
69 | /** |
||
70 | * Returns the array for the middleware with the action to attach |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 4 | private function getMiddleware(): array |
|
75 | { |
||
76 | return [ |
||
77 | 4 | 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 | 4 | private function getRoutes(): array |
|
89 | { |
||
90 | 4 | $path = appPath('api/routes'); |
|
91 | |||
92 | $routes = [ |
||
93 | 4 | 'api' => $path . '/api.php', |
|
94 | ]; |
||
95 | |||
96 | 4 | return $routes; |
|
97 | } |
||
98 | } |
||
99 |