fromRequest($request); self::$routes = new RouteCollection(); $loader = new AnnotationFileLoader(new FileLocator(), new RouteLoader()); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/Controller')); foreach ($iterator as $file) { /**@var \SplFileInfo $file*/ if (in_array($file->getFilename(), ['.', '..'])) continue; $collection = $loader->load($file->getPathname(), 'attribute'); self::$routes->addCollection($collection); } } public static function execute(): Response { try { $matcher = new UrlMatcher(self::$routes, self::$context); $match = $matcher->matchRequest(self::$request); foreach ($match as $key => $value) { if (str_starts_with($key, '_')) continue; self::$request->query->set($key, $value); } /**@var \ReflectionClass $class*/ $class = $match['_']['class']; /**@var \ReflectionMethod $method*/ $method = $match['_']['method']; return ($class->newInstance())->{$method->getName()}(self::$request); } catch (ResourceNotFoundException $exception) { return new Response('404', 404); } catch (MethodNotAllowedException $exception) { return new Response('403', 403); } catch (\Exception $exception) { return new Response('500: ' . $exception->getMessage(), 500); } } /** * @param string $name * @param array $parameters * @param int $referenceType */ public static function generate(string $name, array $parameters = [], int $referenceType = 1): string { $generator = new UrlGenerator(self::$routes, self::$context); return $generator->generate($name, $parameters, $referenceType); } }