From f8d1c66934ba3a98a3ad7efb9c74eb528625ab07 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 16 Aug 2023 16:04:35 +0200 Subject: [PATCH] Implement commands --- app/Console/TestCommand.php | 17 ++++++++ app/Controller/HelloWorldController.php | 4 +- app/Entity/Book.php | 18 +++++++++ app/Kernel.php | 13 ++++-- app/View/RouteView.php | 4 +- app/bin.php | 3 ++ public/index.php | 2 +- src/Attribute/Command.php | 15 +++++++ src/Console/BaseCommand.php | 13 ++++++ src/Console/CommandsConfig.php | 40 +++++++++++++++++++ src/Console/Console.php | 39 ++++++++++++++++++ src/Console/Output.php | 11 +++++ src/Http/GenericRouter.php | 2 +- src/{Kernel => Http}/Renderer.php | 9 +---- .../{RouterConfig.php => RoutesConfig.php} | 16 ++++++-- src/Kernel/BaseKernel.php | 21 ++++++---- src/Kernel/Console.php | 21 ---------- 17 files changed, 200 insertions(+), 48 deletions(-) create mode 100644 app/Console/TestCommand.php create mode 100644 src/Attribute/Command.php create mode 100644 src/Console/BaseCommand.php create mode 100644 src/Console/CommandsConfig.php create mode 100644 src/Console/Console.php create mode 100644 src/Console/Output.php rename src/{Kernel => Http}/Renderer.php (76%) rename src/Http/{RouterConfig.php => RoutesConfig.php} (66%) delete mode 100644 src/Kernel/Console.php diff --git a/app/Console/TestCommand.php b/app/Console/TestCommand.php new file mode 100644 index 0000000..973a7f1 --- /dev/null +++ b/app/Console/TestCommand.php @@ -0,0 +1,17 @@ +printLine('Hello, command!'); + } +} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index 0b93800..ef7a807 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -7,7 +7,7 @@ use App\View\RouteView; use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Http\GenericResponse; use Ardent\Undercurrent\Http\ResponseInterface; -use Ardent\Undercurrent\Http\RouterConfig; +use Ardent\Undercurrent\Http\RoutesConfig; use Ardent\Undercurrent\Http\StatusEnum; use Ardent\Undercurrent\View\BaseView; use Ardent\Undercurrent\View\ViewInterface; @@ -40,7 +40,7 @@ class HelloWorldController } #[Route('/routes')] - public function routes(RouterConfig $config): ViewInterface + public function routes(RoutesConfig $config): ViewInterface { return new RouteView($config); } diff --git a/app/Entity/Book.php b/app/Entity/Book.php index 9af2e01..2e06211 100644 --- a/app/Entity/Book.php +++ b/app/Entity/Book.php @@ -10,4 +10,22 @@ class Book { #[ORM\Id, ORM\Column, ORM\GeneratedValue] private int $id; + + #[ORM\Column] + private string $title; + + public function getId(): int + { + return $this->id; + } + + public function getTitle(): string + { + return $this->title; + } + + public function setTitle(string $title): void + { + $this->title = $title; + } } \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index 8b28577..24a028c 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,16 +2,23 @@ namespace App; +use App\Console\TestCommand; use App\Controller\HelloWorldController; +use Ardent\Undercurrent\Console\CommandsConfig; use Ardent\Undercurrent\Container\ContainerInterface; +use Ardent\Undercurrent\Http\RoutesConfig; use Ardent\Undercurrent\Kernel\BaseKernel; class Kernel extends BaseKernel { protected function dependencies(ContainerInterface $container): void { - $this->addControllers($container, [ - HelloWorldController::class, - ]); + $routes = new RoutesConfig(); + $routes->add(HelloWorldController::class); + $this->addControllers($container, $routes); + + $commands = new CommandsConfig(); + $commands->add(TestCommand::class); + $this->addCommands($container, $commands); } } \ No newline at end of file diff --git a/app/View/RouteView.php b/app/View/RouteView.php index 40a996c..79b16d6 100644 --- a/app/View/RouteView.php +++ b/app/View/RouteView.php @@ -2,12 +2,12 @@ namespace App\View; -use Ardent\Undercurrent\Http\RouterConfig; +use Ardent\Undercurrent\Http\RoutesConfig; use Ardent\Undercurrent\View\BaseView; class RouteView extends BaseView { - public function __construct(protected readonly RouterConfig $routes) + public function __construct(protected readonly RoutesConfig $routes) { parent::__construct('/routes.php', extends: '/base'); } diff --git a/app/bin.php b/app/bin.php index 1870b6f..4c41891 100755 --- a/app/bin.php +++ b/app/bin.php @@ -3,9 +3,12 @@ use App\Kernel; use Ardent\Undercurrent\Config\AppConfig; +use Ardent\Undercurrent\Console\Console; require_once dirname(__DIR__) . '/vendor/autoload.php'; $kernel = new Kernel(new AppConfig(__DIR__ . '/../app')); $container = $kernel->setup(); +$console = new Console($container); +$console->run(); \ No newline at end of file diff --git a/public/index.php b/public/index.php index c0405b3..7badc50 100644 --- a/public/index.php +++ b/public/index.php @@ -8,7 +8,7 @@ error_reporting(E_ALL); use App\Kernel; use Ardent\Undercurrent\Config\AppConfig; -use Ardent\Undercurrent\Kernel\Renderer; +use Ardent\Undercurrent\Http\Renderer; require_once dirname(__DIR__) . '/vendor/autoload.php'; diff --git a/src/Attribute/Command.php b/src/Attribute/Command.php new file mode 100644 index 0000000..adf65d9 --- /dev/null +++ b/src/Attribute/Command.php @@ -0,0 +1,15 @@ +run($output); + } + + abstract protected function run(Output $output): void; +} \ No newline at end of file diff --git a/src/Console/CommandsConfig.php b/src/Console/CommandsConfig.php new file mode 100644 index 0000000..fd16fbe --- /dev/null +++ b/src/Console/CommandsConfig.php @@ -0,0 +1,40 @@ +getAttributes(Command::class, ReflectionAttribute::IS_INSTANCEOF); + + if (count($attributes) === 0) { + return $this; + } + + $command = $attributes[0]->newInstance(); + + $this->commands[] = [ + $command, + $commandClass, + ]; + + return $this; + } + + /** + * @return array + */ + public function getCommands(): array + { + return $this->commands; + } +} \ No newline at end of file diff --git a/src/Console/Console.php b/src/Console/Console.php new file mode 100644 index 0000000..4a08c6a --- /dev/null +++ b/src/Console/Console.php @@ -0,0 +1,39 @@ +printLine('No command defined'); + return; + } + + $config = $this->container->get(CommandsConfig::class); + + foreach ($config->getCommands() as [$commandConfig, $commandClass]) { + if ($commandConfig->signature === $command) { + $commandInstance = $this->container->get($commandClass); + $commandInstance->execute($output); + return; + } + } + + $output->printLine(sprintf('Command not found: "%s"', $command)); + } +} \ No newline at end of file diff --git a/src/Console/Output.php b/src/Console/Output.php new file mode 100644 index 0000000..709138e --- /dev/null +++ b/src/Console/Output.php @@ -0,0 +1,11 @@ +getMessage(), StatusEnum::NOT_FOUND ); - //$response = $container->get(RouterConfig::class)->getExceptionRoute()->getController()::exception($e); + //$response = $container->get(RoutesConfig::class)->getExceptionRoute()->getController()::exception($e); } http_response_code($response->getStatus()->value); diff --git a/src/Http/RouterConfig.php b/src/Http/RoutesConfig.php similarity index 66% rename from src/Http/RouterConfig.php rename to src/Http/RoutesConfig.php index eec5cde..6a880ca 100644 --- a/src/Http/RouterConfig.php +++ b/src/Http/RoutesConfig.php @@ -6,16 +6,19 @@ use Ardent\Undercurrent\Attribute\Route; use ReflectionAttribute; use ReflectionClass; -class RouterConfig +class RoutesConfig { /** * @var array */ private array $routes = []; - public function addController(string $controller): self + private array $controllers = []; + + public function add(string $controllerClass): self { - $reflectionClass = new ReflectionClass($controller); + $this->controllers[] = $controllerClass; + $reflectionClass = new ReflectionClass($controllerClass); foreach ($reflectionClass->getMethods() as $method) { $attributes = $method->getAttributes(Route::class, ReflectionAttribute::IS_INSTANCEOF); @@ -28,7 +31,7 @@ class RouterConfig $this->routes[] = new RouteConfig( $route, - $controller, + $controllerClass, $method->getName(), ); } @@ -43,4 +46,9 @@ class RouterConfig { return $this->routes; } + + public function getControllers(): array + { + return $this->controllers; + } } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 32c2a4b..dd52b3e 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -4,10 +4,11 @@ namespace Ardent\Undercurrent\Kernel; use Ardent\Undercurrent\Config\AppConfig; use Ardent\Undercurrent\Config\GenericConfig; +use Ardent\Undercurrent\Console\CommandsConfig; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Container\GenericContainer; use Ardent\Undercurrent\Http\GenericRouter; -use Ardent\Undercurrent\Http\RouterConfig; +use Ardent\Undercurrent\Http\RoutesConfig; use Ardent\Undercurrent\Http\RouterInterface; use Ardent\Undercurrent\Logger\LogContainer; use Ardent\Undercurrent\Logger\LoggerInterface; @@ -56,16 +57,22 @@ class BaseKernel return $container; } - protected function addControllers(ContainerInterface $container, array $controllers): void + protected function addControllers(ContainerInterface $container, RoutesConfig $routes): void { - $config = new RouterConfig(); - - foreach ($controllers as $controller) { + foreach ($routes->getControllers() as $controller) { $container->add($controller); - $config->addController($controller); } - $container->add(RouterConfig::class, fn() => $config); + $container->add(RoutesConfig::class, fn() => $routes); + } + + protected function addCommands(ContainerInterface $container, CommandsConfig $commands): void + { + foreach ($commands->getCommands() as [$commandConfig, $commandClass]) { + $container->add($commandClass); + } + + $container->add(CommandsConfig::class, fn() => $commands); } protected function dependencies(ContainerInterface $container): void diff --git a/src/Kernel/Console.php b/src/Kernel/Console.php deleted file mode 100644 index c7aa207..0000000 --- a/src/Kernel/Console.php +++ /dev/null @@ -1,21 +0,0 @@ -