1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lepton\Core\Handler; |
4
|
|
|
|
5
|
|
|
use Lepton\Http\Response\{HttpResponse, SuccessResponse, NotFoundResponse, InternalErrorResponse}; |
6
|
|
|
use Lepton\Routing\Match\{BaseMatch, MatchRoute, Match404}; |
7
|
|
|
use Lepton\Routing\UrlResolver; |
8
|
|
|
use Lepton\Core\Application; |
9
|
|
|
use Lepton\Exceptions; |
10
|
|
|
|
11
|
|
|
class BaseHandler extends AbstractHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get the matcher that matches the current request |
15
|
|
|
* |
16
|
|
|
* @return BaseMatch |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
protected function resolveRequest(): BaseMatch |
21
|
|
|
{ |
22
|
|
|
$resolver = new UrlResolver($this->request->url); |
23
|
|
|
|
24
|
|
|
foreach (Application::$routes as $pattern => $callback) { |
25
|
|
|
|
26
|
|
|
// Add base_url to $pattern, if needed |
27
|
|
|
|
28
|
|
|
$link = "%s/%s"; |
29
|
|
|
$pattern = sprintf($link, Application::getAppConfig()->base_url, $pattern); |
30
|
|
|
// If it matches, return the match |
31
|
|
|
$parameters = $resolver->match($pattern); |
32
|
|
|
if (is_array($parameters)) { |
33
|
|
|
// Check if callback is a controller method |
34
|
|
|
$controller = $callback[0]; |
35
|
|
|
$method = $callback[1]; |
36
|
|
|
if (! method_exists($controller, $method)) { |
37
|
|
|
throw new Exceptions\ControllerNotFoundException("Invalid Controller and/or method in routes.php"); |
38
|
|
|
} |
39
|
|
|
return new MatchRoute(controller: $controller, method: $method, parameters: $parameters); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
return new Match404(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Handles the request forwarding it to the $handler |
48
|
|
|
* @param Match404|MatchRoute $matcher |
49
|
|
|
* @return NotFoundResponde|SuccessResponse |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
protected function handle(BaseMatch $matcher): HttpResponse |
52
|
|
|
{ |
53
|
|
|
if($matcher instanceof Match404) { |
54
|
|
|
return new NotFoundResponse(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if($matcher instanceof MatchRoute) { |
58
|
|
|
|
59
|
|
|
foreach($this->middlewares as $middleware => $args){ |
60
|
|
|
$middlewareInstance = new $middleware(); |
61
|
|
|
$middlewareInstance->addMatcher($matcher); |
62
|
|
|
$middlewareInstance->setRequest($this->request); |
63
|
|
|
$middlewareResult = $middlewareInstance(...$args); |
64
|
|
|
if($middlewareResult instanceof HttpResponse){ |
65
|
|
|
return $middlewareResult; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
Application::$controller = $matcher->controller; |
70
|
|
|
$controller = new $matcher->controller(); |
71
|
|
|
$method = $matcher->method; |
72
|
|
|
return $controller->$method(...$matcher->parameters); |
73
|
|
|
} |
74
|
|
|
throw new \Exception("Wrong matcher!"); |
75
|
|
|
return new InternalErrorResponse(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths