summaryrefslogtreecommitdiff
path: root/src/Router.php
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-02 20:42:01 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-05 12:33:59 +0100
commitb21316248572cb27ed1f504529ad6680a473022e (patch)
treef8a2f81258cae3b1d2429fb7df5a3287954b683a /src/Router.php
parentf621d95f89ded05a2e916c5ee363bfe75ea37482 (diff)
gemini
Diffstat (limited to 'src/Router.php')
-rw-r--r--src/Router.php80
1 files changed, 0 insertions, 80 deletions
diff --git a/src/Router.php b/src/Router.php
deleted file mode 100644
index 8b24000..0000000
--- a/src/Router.php
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-
-namespace App;
-
-use App\Support\RouteLoader;
-use Symfony\Component\Config\FileLocator;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\Routing\Exception\MethodNotAllowedException;
-use Symfony\Component\Routing\Exception\ResourceNotFoundException;
-use Symfony\Component\Routing\Generator\UrlGenerator;
-use Symfony\Component\Routing\Loader\AnnotationFileLoader;
-use Symfony\Component\Routing\Matcher\UrlMatcher;
-use Symfony\Component\Routing\RequestContext;
-use Symfony\Component\Routing\RouteCollection;
-
-class Router
-{
- public static Request $request;
- public static RequestContext $context;
- public static RouteCollection $routes;
-
- public static function init(Request $request): void
- {
- self::$request = $request;
-
- self::$context = new RequestContext();
- self::$context->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);
- }
-}