1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Bootstrap; |
||
6 | |||
7 | use function Gewaer\Core\appPath; |
||
8 | use Phalcon\Di\FactoryDefault; |
||
9 | use Phalcon\Mvc\Micro; |
||
10 | use Gewaer\Http\Response; |
||
11 | use Phalcon\Http\Request; |
||
12 | use Throwable; |
||
13 | use Dmkit\Phalcon\Auth\Middleware\Micro as AuthMicro; |
||
14 | use Gewaer\Exception\ServerErrorHttpException; |
||
15 | use Gewaer\Constants\Flags; |
||
16 | use Baka\Http\RouterCollection; |
||
17 | |||
18 | /** |
||
19 | * Class Api |
||
20 | * |
||
21 | * @package Gewaer\Bootstrap |
||
22 | * |
||
23 | * @property Micro $application |
||
24 | */ |
||
25 | class Api extends AbstractBootstrap |
||
26 | { |
||
27 | /** |
||
28 | * Run the application |
||
29 | * |
||
30 | * @return mixed |
||
31 | */ |
||
32 | 1 | public function run() |
|
33 | { |
||
34 | try { |
||
35 | 1 | $config = $this->container->getConfig()->jwt->toArray(); |
|
36 | |||
37 | //if the router has jwt ignore url we always overwrite the app config |
||
38 | 1 | $routerJwtIgnoreUrl = RouterCollection::getJwtIgnoreRoutes(); |
|
39 | 1 | if (!empty($routerJwtIgnoreUrl)) { |
|
40 | 1 | $config['ignoreUri'] = $routerJwtIgnoreUrl; |
|
41 | } elseif (!$this->container->getConfig()->application->jwtSecurity) { |
||
42 | //ignore token validation if disable |
||
43 | $config['ignoreUri'] = ['regex: *']; |
||
44 | } |
||
45 | |||
46 | //JWT Validation |
||
47 | 1 | $auth = new AuthMicro($this->application, $config); |
|
48 | |||
49 | 1 | return $this->application->handle(); |
|
50 | } catch (Throwable $e) { |
||
51 | $this->handleException($e)->send(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Handle the exception we throw from our api |
||
57 | * |
||
58 | * @param Throwable $e |
||
59 | * @return Response |
||
60 | */ |
||
61 | public function handleException(Throwable $e): Response |
||
62 | { |
||
63 | $response = new Response(); |
||
64 | $request = new Request(); |
||
65 | $identifier = $request->getServerAddress(); |
||
66 | $config = $this->container->getConfig(); |
||
67 | |||
68 | $httpCode = (method_exists($e, 'getHttpCode')) ? $e->getHttpCode() : 400; |
||
69 | $httpMessage = (method_exists($e, 'getHttpMessage')) ? $e->getHttpMessage() : 'Bad Request'; |
||
70 | $data = (method_exists($e, 'getData')) ? $e->getData() : []; |
||
71 | |||
72 | $message = $e->getMessage(); |
||
73 | $response->setHeader('Access-Control-Allow-Origin', '*'); //@todo check why this fails on nginx |
||
74 | $response->setStatusCode($httpCode, $httpMessage); |
||
75 | $response->setContentType('application/json'); |
||
76 | $response->setJsonContent([ |
||
77 | 'errors' => [ |
||
78 | 'type' => $httpMessage, |
||
79 | 'identifier' => $identifier, |
||
80 | 'message' => $e->getMessage(), |
||
81 | 'trace' => strtolower($config->app->env) != Flags::PRODUCTION ? $e->getTraceAsString() : null, |
||
82 | 'data' => $data, |
||
83 | ], |
||
84 | ]); |
||
85 | |||
86 | //only log when server error production is seerver error or dev |
||
87 | if ($e instanceof ServerErrorHttpException || strtolower($config->app->env) != Flags::PRODUCTION) { |
||
88 | $this->container->getLog()->error($e->getTraceAsString()); |
||
89 | } |
||
90 | |||
91 | return $response; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return mixed |
||
96 | */ |
||
97 | 4 | public function setup() |
|
98 | { |
||
99 | //set the default DI |
||
100 | 4 | $this->container = new FactoryDefault(); |
|
101 | //set all the services |
||
102 | 4 | $this->providers = require appPath('api/config/providers.php'); |
|
103 | |||
104 | //run my parents setup |
||
105 | 4 | parent::setup(); |
|
106 | 4 | } |
|
107 | } |
||
108 |