1 | <?php |
||
25 | class ServiceAdapter implements ServiceInterface |
||
26 | { |
||
27 | /** @var Result */ |
||
28 | private $result; |
||
29 | /** @var array */ |
||
30 | private $routes; |
||
31 | /** @var string */ |
||
32 | private $module; |
||
33 | /** @var string */ |
||
34 | private $application; |
||
35 | /** @var string */ |
||
36 | private $applicationPath; |
||
37 | /** @var ProxyInterface */ |
||
38 | private $proxy; |
||
39 | |||
40 | /** |
||
41 | * ServiceAdapter constructor. |
||
42 | * |
||
43 | * @param ConfigurationInterface $configuration |
||
44 | * @param EnvironmentInterface $environmentManager |
||
45 | * @param Result $routeResult |
||
46 | * @param null|ProxyInterface $routerProxy |
||
47 | */ |
||
48 | 10 | public function __construct( |
|
62 | |||
63 | /** |
||
64 | * Processes the Request and give a Result. |
||
65 | * |
||
66 | * @param ServerRequestInterface $request |
||
67 | * @return Result |
||
68 | */ |
||
69 | 8 | public function match(ServerRequestInterface $request) : Result |
|
70 | { |
||
71 | 8 | $httpMethod = $request->getMethod(); |
|
72 | 8 | $uri = $this->getApplicationRouteUri($request); |
|
73 | |||
74 | 8 | $result = clone $this->result; |
|
75 | 8 | $routeDefinition = $this->findRouteDefinition($uri); |
|
76 | |||
77 | 8 | if (empty($routeDefinition)) { |
|
78 | 3 | return $result->setStatus(Result::CODE_NOT_FOUND); |
|
79 | } |
||
80 | |||
81 | 7 | if (!in_array($httpMethod, $routeDefinition['allowed_methods'])) { |
|
82 | 3 | return $result->setStatus(Result::CODE_BAD_METHOD); |
|
83 | } |
||
84 | |||
85 | 6 | $result->setParameters($routeDefinition['parameters']) |
|
86 | 6 | ->setResource($routeDefinition['resource']) |
|
87 | 6 | ->setMatchedMiddleware($routeDefinition['middleware']); |
|
88 | |||
89 | // Check if we marked the middleware to be resolved by the proxy |
||
90 | 6 | if ($routeDefinition['middleware'] === 'proxy' && $this->proxy instanceof ProxyInterface) { |
|
91 | 1 | $this->proxy->resolveMiddleware($this->application, $result); |
|
92 | } |
||
93 | |||
94 | 6 | if (empty($result->getMatchedMiddleware())) { |
|
95 | 1 | return $result->setStatus(Result::CODE_NOT_FOUND); |
|
96 | } |
||
97 | |||
98 | 6 | return $result->setStatus(Result::CODE_FOUND); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * According to the application path, determines the route uri |
||
103 | * |
||
104 | * @param ServerRequestInterface $request |
||
105 | * @return string |
||
106 | */ |
||
107 | 9 | private function getApplicationRouteUri(ServerRequestInterface $request) : string |
|
117 | |||
118 | /** |
||
119 | * Searches definition and returns data is found. First find, first served. |
||
120 | * |
||
121 | * @param string $uri |
||
122 | * @return array|null |
||
123 | */ |
||
124 | 8 | private function findRouteDefinition(string $uri) : ? array |
|
148 | |||
149 | /** |
||
150 | * Gets the paramters from the route pattern match result. |
||
151 | * |
||
152 | * @param array $matches |
||
153 | * @return array |
||
154 | */ |
||
155 | 7 | private function getMatchParameters(array $matches) : array |
|
172 | } |
||
173 |