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