1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Ship; |
4
|
|
|
|
5
|
|
|
use Rudra\Container\Facades\Rudra; |
6
|
|
|
use Rudra\Exceptions\RouterException; |
7
|
|
|
use Rudra\Router\RouterFacade as Router; |
8
|
|
|
|
9
|
|
|
class Route |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @throws RouterException |
13
|
|
|
*/ |
14
|
|
|
public function run() |
15
|
|
|
{ |
16
|
|
|
if (config('environment') === 'development') { |
17
|
|
|
$timeCollector = Rudra::get("debugbar")['time']; |
18
|
|
|
|
19
|
|
|
if ($timeCollector->hasStartedMeasure('index')) { |
20
|
|
|
$timeCollector->stopMeasure('index'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (!$timeCollector->hasStartedMeasure('routing')) { |
24
|
|
|
$timeCollector->startMeasure('routing'); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->collect(config('containers')); |
29
|
|
|
throw new RouterException("404"); |
30
|
|
|
|
31
|
|
|
if (config('environment') !== 'test') { |
|
|
|
|
32
|
|
|
exit(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function collect(array $namespaces): void |
37
|
|
|
{ |
38
|
|
|
foreach ($namespaces as $container => $item) { |
39
|
|
|
$routes = $this->getRoutes($container); |
40
|
|
|
$flatRoutes = array_merge(...$routes); |
41
|
|
|
|
42
|
|
|
foreach ($flatRoutes as $route) { |
43
|
|
|
Router::set($route); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getRoutes(string $container): array |
49
|
|
|
{ |
50
|
|
|
$cacheDir = "../app/cache"; |
51
|
|
|
$cacheTime = config('cache.time', 'routes'); |
52
|
|
|
$routesDir = $cacheDir . "/routes"; |
53
|
|
|
$cacheFile = $routesDir . "/routes_" . $container . ".php"; |
54
|
|
|
$cacheLifetime = strtotime($cacheTime) - time(); |
55
|
|
|
|
56
|
|
|
if (!is_dir($cacheDir)) { |
57
|
|
|
mkdir($cacheDir, 0755, true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!is_dir($routesDir)) { |
61
|
|
|
mkdir($routesDir, 0755, true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (file_exists($cacheFile)) { |
65
|
|
|
$cacheTime = filemtime($cacheFile); |
66
|
|
|
$currentTime = time(); |
67
|
|
|
|
68
|
|
|
if ($currentTime - $cacheTime < $cacheLifetime) { |
69
|
|
|
return include $cacheFile; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$path = "../app/Containers/" . ucfirst($container) . "/routes"; |
74
|
|
|
|
75
|
|
|
if (!file_exists($path . ".php")) { |
76
|
|
|
throw new \RuntimeException("Routes file not found for container: " . $container); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Генерация маршрутов и сохранение их в кеш |
80
|
|
|
$routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes")); |
|
|
|
|
81
|
|
|
file_put_contents($cacheFile, "<?php\nreturn " . var_export($routes, true) . ";"); |
82
|
|
|
|
83
|
|
|
return $routes; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.