| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if (config('environment') !== 'test') { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |             exit(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function collect(array $namespaces): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         foreach ($namespaces as $container => $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $routes     = $this->getRoutes($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $flatRoutes = array_merge(...$routes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             foreach ($flatRoutes as $route) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |                 Router::set($route); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function getRoutes(string $container): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $cacheDir  = "../app/cache"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $cacheTime = config('cache.time', 'routes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $routesDir = $cacheDir . "/routes"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $cacheFile = $routesDir . "/routes_" . $container . ".php"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $cacheLifetime = strtotime($cacheTime) - time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (!is_dir($cacheDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             mkdir($cacheDir, 0755, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!is_dir($routesDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             mkdir($routesDir, 0755, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (file_exists($cacheFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $cacheTime   = filemtime($cacheFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $currentTime = time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if ($currentTime - $cacheTime < $cacheLifetime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 return include $cacheFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $path = "../app/Containers/" . ucfirst($container) . "/routes"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (!file_exists($path . ".php")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             throw new \RuntimeException("Routes file not found for container: " . $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // Генерация маршрутов и сохранение их в кеш | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes")); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         file_put_contents($cacheFile, "<?php\nreturn " . var_export($routes, true) . ";"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |         return $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                        
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.