From 503d8c524aa2eac801a856ad5cca3e6958e8cd69 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 28 Jul 2023 16:14:55 +0200 Subject: [PATCH] First working version with non working routes --- .gitignore | 1 + README.md | 9 +++ app/Controller/BaseController.php | 14 ++++ app/Kernel.php | 9 +++ app/public/index.php | 9 +++ composer.json | 15 ++++ composer.lock | 20 ++++++ src/Attribute/Route.php | 15 ++++ src/Collector/ClassAttributeCollector.php | 83 +++++++++++++++++++++++ src/Container/GenericContainer.php | 54 +++++++++++++++ src/Controller/GenericRouter.php | 31 +++++++++ src/Kernel/BaseKernel.php | 28 ++++++++ 12 files changed, 288 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app/Controller/BaseController.php create mode 100644 app/Kernel.php create mode 100644 app/public/index.php create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/Attribute/Route.php create mode 100644 src/Collector/ClassAttributeCollector.php create mode 100644 src/Container/GenericContainer.php create mode 100644 src/Controller/GenericRouter.php create mode 100644 src/Kernel/BaseKernel.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93f6ca3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b64da1 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Todos: +- [ ] Collect routes properly +- [ ] Process routes correctly + + + +Routing: +Currently the routes are hardcoded, we need we way to process +all classes maybe and sort them into a list of routes. \ No newline at end of file diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php new file mode 100644 index 0000000..aa0a79b --- /dev/null +++ b/app/Controller/BaseController.php @@ -0,0 +1,14 @@ +path)); + foreach ($files as $file) { + if ($file->getExtension() !== 'php') { + continue; + } + $class = $this->getClassFromFile($file); + if (!$class) { + continue; + } + $classes[] = $class; + } + return $classes; + } + + private function getClassFromFile(mixed $file) + { + $contents = file_get_contents($file->getPathname()); + $tokens = token_get_all($contents); + $namespace = ''; + $class = ''; + $classFound = false; + $namespaceFound = false; + foreach ($tokens as $token) { + if (is_array($token)) { + if ($token[0] === T_NAMESPACE) { + $namespaceFound = true; + continue; + } + if ($token[0] === T_CLASS) { + $classFound = true; + continue; + } + if ($namespaceFound && $token[0] === T_STRING) { + $namespace .= $token[1] . '\\'; + continue; + } + if ($classFound && $token[0] === T_STRING) { + $class = $token[1]; + break; + } + } + } + if (!$class) { + return null; + } + $class = $namespace . $class; + $attributes = $this->getClassAttributes($class); + if (!in_array($this->attribute, $attributes)) { + return null; + } + return $class; + } + + private function getClassAttributes(string $class): array + { + $attributes = []; + $reflection = new ReflectionClass($class); + foreach ($reflection->getAttributes() as $attribute) { + $attributes[] = $attribute->getName(); + } + return $attributes; + } +} \ No newline at end of file diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php new file mode 100644 index 0000000..d45c840 --- /dev/null +++ b/src/Container/GenericContainer.php @@ -0,0 +1,54 @@ +definitions[$className] = [ + 'definition' => $definition, + 'singleton' => $singleton, + ]; + + return $this; + } + + /** + * @template TClassName + * @param class-string $className + * @return TClassName + * @throws Exception + */ + public function get(string $className): object + { + if (!isset($this->definitions[$className])) { + throw new Exception("Class $className not found in container"); + } + + $definition = $this->definitions[$className]['definition']; + if ($this->definitions[$className]['singleton']) { + if (isset($this->instances[$className])) { + return $this->instances[$className]; + } + $instance = $definition(); + $this->instances[$className] = $instance; + } else { + $instance = $definition(); + } + + return $instance; + } +} \ No newline at end of file diff --git a/src/Controller/GenericRouter.php b/src/Controller/GenericRouter.php new file mode 100644 index 0000000..28f75b1 --- /dev/null +++ b/src/Controller/GenericRouter.php @@ -0,0 +1,31 @@ +routes = $collector->collect(); + } + + public function getRoute(string $route): array + { +// return $this->routes[0]; + return [ + 'controller' => BaseController::class, + 'method' => 'HelloWorld', + ]; + } +} \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php new file mode 100644 index 0000000..6bc8ed5 --- /dev/null +++ b/src/Kernel/BaseKernel.php @@ -0,0 +1,28 @@ +add(GenericRouter::class); + $container->add(BaseController::class); + + $this->render($container); + } + + private function render(GenericContainer $container): void + { + $router = $container->get(GenericRouter::class); + $route = $router->getRoute($_SERVER['REQUEST_URI']); + $controller = $container->get($route['controller']); + $method = $route['method']; + echo $controller->$method(); + } +} \ No newline at end of file