1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fermi; |
4
|
|
|
|
5
|
|
|
use FastRoute\Dispatcher; |
6
|
|
|
use FastRoute\RouteCollector; |
7
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
8
|
|
|
use League\Plates\Engine; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
11
|
|
|
|
12
|
|
|
class Framework |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new request from the php globals environment. |
16
|
|
|
* |
17
|
|
|
* Note: If you want to use a different PSR-7 implementation this would be |
18
|
|
|
* the proper place to replace the stock implementation of zend\diactoros. |
19
|
|
|
* |
20
|
|
|
* @return \Psr\Http\Message\RequestInterface |
21
|
|
|
*/ |
22
|
9 |
|
public static function requestFromGlobals() |
23
|
|
|
{ |
24
|
9 |
|
return ServerRequestFactory::fromGlobals(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the full middleware stack. |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
1 |
|
public static function stack() |
33
|
|
|
{ |
34
|
1 |
|
return static::config('middleware'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a middleware stack that is resolvable by PSR-15. |
39
|
|
|
* |
40
|
|
|
* @param array $stack array of callables or strings. |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
2 |
|
public static function middleware(array $stack) |
44
|
|
|
{ |
45
|
|
|
return array_map(function ($callable) { |
46
|
2 |
|
$callable = is_string($callable) ? static::lazy($callable) : $callable; |
47
|
2 |
|
return $callable; |
48
|
2 |
|
}, $stack); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns a closure which, when called, will create an instance of the |
53
|
|
|
* class passed to it and then call __invoke() with middleware arguments. |
54
|
|
|
* |
55
|
|
|
* @param string $className fully qualified name of a class |
56
|
|
|
* @return callable |
57
|
|
|
*/ |
58
|
2 |
|
public static function lazy($className) |
59
|
1 |
|
{ |
60
|
|
|
return function (RequestInterface $request, $next) use ($className) { |
61
|
2 |
|
$class = new $className(); |
62
|
1 |
|
if ($class instanceof MiddlewareInterface) { |
|
|
|
|
63
|
1 |
|
return $class->process($request, $next); |
64
|
|
|
} else { |
65
|
1 |
|
return $class($request, $next); |
66
|
|
|
} |
67
|
2 |
|
}; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a new FastRouter\simpleDispatcher and collect the routes. |
72
|
|
|
* |
73
|
|
|
* @return FastRouter\simpleDispatcher |
74
|
|
|
*/ |
75
|
2 |
|
public static function router(RequestInterface $request, $path = null) |
76
|
|
|
{ |
77
|
2 |
|
$path = $path ?: __DIR__ . "/../app/routes.php"; |
78
|
2 |
|
$dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $r) use ($path) { |
79
|
2 |
|
include $path; |
80
|
2 |
|
}); |
81
|
2 |
|
$match = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); |
82
|
2 |
|
return static::resolve($request, $match); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Resolve an HTTP request match. |
87
|
|
|
* |
88
|
|
|
* @param array $match Route match. When using fast router will be array. |
89
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
90
|
|
|
*/ |
91
|
3 |
|
public static function resolve(RequestInterface $request, $match) |
92
|
|
|
{ |
93
|
3 |
|
switch ($match[0]) { |
94
|
3 |
|
case Dispatcher::FOUND: |
95
|
1 |
|
array_unshift($match[2], $request); |
96
|
1 |
|
return call_user_func_array($match[1], $match[2]); |
97
|
2 |
|
case Dispatcher::METHOD_NOT_ALLOWED: |
98
|
1 |
|
return Response::error405($request); |
99
|
1 |
|
default: |
100
|
1 |
|
return Response::error404($request); |
101
|
1 |
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Load a given configuration file or array. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
4 |
|
public static function config($name, $dir = null) |
110
|
|
|
{ |
111
|
4 |
|
$dir = $dir ?: __DIR__ . "/../config"; |
112
|
4 |
|
$location = rtrim($dir, "/") . "/" . ltrim($name, "/") . ".php"; |
113
|
4 |
|
if (!file_exists($location)) { |
114
|
1 |
|
throw new \InvalidArgumentException('Requested configuration file does not exist: ' . $location); |
115
|
|
|
} |
116
|
3 |
|
$config = include $location; |
117
|
3 |
|
if (!is_array($config)) { |
118
|
1 |
|
throw new \InvalidArgumentException('Requested configuration file does not return an array: ' . $location); |
119
|
|
|
} |
120
|
2 |
|
return $config; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the rendering engine. |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
1 |
|
public static function engine() |
129
|
|
|
{ |
130
|
1 |
|
return new Engine(__DIR__ . "/../views"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Render a given view with our template engine. |
135
|
|
|
* |
136
|
|
|
* @param string $view our view data. |
137
|
|
|
* @param array $data data to pass through to our template |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
2 |
|
public static function render($view, $data, $engine = false) |
141
|
|
|
{ |
142
|
2 |
|
$engine = ($engine instanceof Engine) ? $engine : static::engine(); |
143
|
2 |
|
return $engine->render($view, $data); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.