From b0c3c554f2810b04fe2f2fb95cb3bacef7ca98f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9EAFAK?= Date: Fri, 22 Dec 2023 17:23:38 +0300 Subject: [PATCH 1/3] Symfony Console --- composer.json | 4 +- system/Console/Command.php | 7 +++- .../Console/Commands/KeyGenerateCommand.php | 28 +++++-------- .../Console/Commands/MakeCommandCommand.php | 28 +++++++------ .../Commands/MakeControllerCommand.php | 32 +++++++------- system/Console/Commands/MakeEntityCommand.php | 31 ++++++-------- .../Commands/MakeMiddlewareCommand.php | 36 +++++++--------- system/Console/Commands/MakeModelCommand.php | 42 +++++++++---------- .../Console/Commands/MakeProviderCommand.php | 40 ++++++++---------- .../Console/Commands/MakeRequestCommand.php | 37 +++++++--------- system/Console/Commands/RouteListCommand.php | 30 ++++++------- system/Console/Commands/ServeCommand.php | 23 +++++----- .../Console/Commands/StorageLinkCommand.php | 18 ++++---- .../Commands/ViewCacheClearCommand.php | 23 +++++----- system/Console/Templates/Command.txt | 28 ++++++++----- system/Providers/ConsoleServiceProvider.php | 8 ++-- 16 files changed, 197 insertions(+), 218 deletions(-) diff --git a/composer.json b/composer.json index ca0bf96..79e0648 100644 --- a/composer.json +++ b/composer.json @@ -44,10 +44,10 @@ "nesbot/carbon": "^2.72", "initphp/cookies": "^1.1", "initphp/logger": "^1.0", - "initphp/console": "^2.0", "initphp/upload": "^1.0", "filp/whoops": "^2.15", - "initphp/performance-meter": "^1.0" + "initphp/performance-meter": "^1.0", + "symfony/console": "^6.4" }, "require-dev": { "symfony/var-dumper": "^6.4" diff --git a/system/Console/Command.php b/system/Console/Command.php index b27dfac..6fc63eb 100644 --- a/system/Console/Command.php +++ b/system/Console/Command.php @@ -14,6 +14,11 @@ declare(strict_types=1); namespace InitPHP\Framework\Console; -abstract class Command extends \InitPHP\Console\Command +abstract class Command extends \Symfony\Component\Console\Command\Command { + + public const SUCCESS = 0; + public const FAILURE = 1; + public const INVALID = 2; + } diff --git a/system/Console/Commands/KeyGenerateCommand.php b/system/Console/Commands/KeyGenerateCommand.php index c40138d..004273a 100644 --- a/system/Console/Commands/KeyGenerateCommand.php +++ b/system/Console/Commands/KeyGenerateCommand.php @@ -14,33 +14,27 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; +use InitPHP\Framework\Console\Command; use InitPHP\Framework\Console\Utils\ChangeDotEnv; -use \InitPHP\Console\{Input, Output}; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class KeyGenerateCommand extends \InitPHP\Framework\Console\Command +class KeyGenerateCommand extends Command { - /** @var string Command */ - public $command = 'key:generate'; + protected static $defaultName = 'key:generate'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { $key = '"' . base64_encode(random_bytes(16)) . '"'; - if ((new ChangeDotEnv())->change('APP_KEY', $key)->save()) { - $output->success("Ok"); - } else { - $output->error("Failed"); - } - } - public function definition(): string - { - return 'Generates and replaces a new APP_KEY.'; + return ((new ChangeDotEnv())->change('APP_KEY', $key)->save()) ? Command::SUCCESS : Command::FAILURE; } - public function arguments(): array + protected function configure(): void { - return []; + $this->setDescription('Creates a command.') + ->setHelp(''); } -} +} \ No newline at end of file diff --git a/system/Console/Commands/MakeCommandCommand.php b/system/Console/Commands/MakeCommandCommand.php index 5367ede..7f0058f 100644 --- a/system/Console/Commands/MakeCommandCommand.php +++ b/system/Console/Commands/MakeCommandCommand.php @@ -14,21 +14,26 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; -use \InitPHP\Console\{Input, Output}; use InitPHP\Framework\Console\Command; use InitPHP\Framework\Console\Utils\MakeFile; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; class MakeCommandCommand extends Command { - public $command = 'make:command'; + protected static $defaultName = 'make:command'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { - $name = !$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0); + + $name = $input->getArgument('name'); + $path = APP_DIR . "Console/Commands/"; $namespace = "App\\Console\\Commands"; - if ($input->hasOption('s')) { + if ($input->getOption('system')) { $path = SYS_DIR . "Console/Commands/"; $namespace = "InitPHP\\Framework\\Console\\Commands"; } @@ -41,16 +46,15 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Command.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } + return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } - public function definition(): string + protected function configure(): void { - return 'Creates a command.'; + $this->setDescription('Creates a command.') + ->setHelp('--name=CommandName') + ->addArgument('name', InputArgument::REQUIRED, 'The name of the command class.') + ->addOption('system', 's', InputOption::VALUE_OPTIONAL, 'Creates a system command.'); } } \ No newline at end of file diff --git a/system/Console/Commands/MakeControllerCommand.php b/system/Console/Commands/MakeControllerCommand.php index b95e8c1..fd41c08 100644 --- a/system/Console/Commands/MakeControllerCommand.php +++ b/system/Console/Commands/MakeControllerCommand.php @@ -14,18 +14,21 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; +use InitPHP\Framework\Console\Command; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class MakeControllerCommand extends \InitPHP\Framework\Console\Command +class MakeControllerCommand extends Command { - /** @var string Command */ - public $command = 'make:controller'; + protected static $defaultName = 'make:controller'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { - $name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/"); + $name = $input->getArgument('name'); + $path = APP_DIR . "HTTP/Controllers/"; $namespace = "App\\HTTP\\Controllers"; @@ -39,21 +42,14 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Controller.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a controller.'; + return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } - public function arguments(): array + protected function configure(): void { - return []; + $this->setDescription('Creates a controller.') + ->setHelp('--name=ControllerName') + ->addArgument('name', InputArgument::REQUIRED, 'The name of the controller class.'); } } diff --git a/system/Console/Commands/MakeEntityCommand.php b/system/Console/Commands/MakeEntityCommand.php index f6a3093..de9cba7 100644 --- a/system/Console/Commands/MakeEntityCommand.php +++ b/system/Console/Commands/MakeEntityCommand.php @@ -14,34 +14,29 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; +use InitPHP\Framework\Console\Command; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class MakeEntityCommand extends \InitPHP\Framework\Console\Command +class MakeEntityCommand extends Command { - /** @var string Command */ - public $command = 'make:entity'; + protected static $defaultName = 'make:entity'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { - $name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/"); + $name = trim($input->getArgument('name'), "/"); - if (!empty(self::makeEntity($name))) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a entity.'; + return !empty(self::makeEntity($name)) ? Command::SUCCESS : Command::FAILURE; } - public function arguments(): array + protected function configure(): void { - return []; + $this->setDescription('Creates a entity.') + ->setHelp('--name=EntityName') + ->addArgument('name', InputArgument::REQUIRED, 'The name of the entity class.'); } public static function makeEntity(string $name): ?string diff --git a/system/Console/Commands/MakeMiddlewareCommand.php b/system/Console/Commands/MakeMiddlewareCommand.php index ec093c7..0b84530 100644 --- a/system/Console/Commands/MakeMiddlewareCommand.php +++ b/system/Console/Commands/MakeMiddlewareCommand.php @@ -15,17 +15,25 @@ namespace InitPHP\Framework\Console\Commands; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use \InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class MakeMiddlewareCommand extends \InitPHP\Framework\Console\Command +class MakeMiddlewareCommand extends Command { - /** @var string Command */ - public $command = 'make:middleware'; + protected static $defaultName = 'make:middleware'; - public function execute(Input $input, Output $output) + protected function configure(): void { - $name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/"); + $this->setDescription('Creates a middleware.') + ->addArgument('name', InputArgument::REQUIRED, 'Middleware Name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $name = trim($input->getArgument('name'), "/"); $path = APP_DIR . "HTTP/Middlewares/"; $namespace = "App\\HTTP\\Middlewares"; @@ -39,21 +47,7 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Middleware.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a middleware.'; - } - - public function arguments(): array - { - return []; + return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } } diff --git a/system/Console/Commands/MakeModelCommand.php b/system/Console/Commands/MakeModelCommand.php index 0717431..cb005aa 100644 --- a/system/Console/Commands/MakeModelCommand.php +++ b/system/Console/Commands/MakeModelCommand.php @@ -15,20 +15,30 @@ namespace InitPHP\Framework\Console\Commands; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use \InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; -class MakeModelCommand extends \InitPHP\Framework\Console\Command +class MakeModelCommand extends Command { - /** @var string Command */ - public $command = 'make:model'; + protected static $defaultName = 'make:model'; - public function execute(Input $input, Output $output) + protected function configure(): void { - $name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/"); + $this->setDescription('Creates a model.') + ->addArgument('name', InputArgument::REQUIRED, 'Model class name') + ->addOption('entity', 'e', InputOption::VALUE_OPTIONAL, 'Create Entity Class.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $name = trim($input->getArgument('name'), "/"); $entity = null; - if ($input->hasOption('e')) { + if ($input->hasOption('entity')) { $entity = MakeEntityCommand::makeEntity($name); } empty($entity) && $entity = "\\InitPHP\\Framework\\Database\\Entity::class"; @@ -45,21 +55,9 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Model.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace, 'entity' => $entity, 'schema' => camelCase2SnakeCase($name)])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a model.'; - } - - public function arguments(): array - { - return []; + return $make->to($path, ["name" => $name, "namespace" => $namespace, 'entity' => $entity, 'schema' => camelCase2SnakeCase($name)]) + ? Command::SUCCESS + : Command::FAILURE; } } diff --git a/system/Console/Commands/MakeProviderCommand.php b/system/Console/Commands/MakeProviderCommand.php index 6b040e7..d7faa8b 100644 --- a/system/Console/Commands/MakeProviderCommand.php +++ b/system/Console/Commands/MakeProviderCommand.php @@ -15,20 +15,30 @@ namespace InitPHP\Framework\Console\Commands; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use \InitPHP\Framework\Console\Command; -class MakeProviderCommand extends \InitPHP\Framework\Console\Command +class MakeProviderCommand extends Command { - /** @var string Command */ - public $command = 'make:provider'; + protected static $defaultName = 'make:provider'; - public function execute(Input $input, Output $output) + protected function configure(): void { - $name = !$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0); + $this->setDescription('Creates a service provider.') + ->addArgument('name', InputArgument::REQUIRED, 'Provider Name') + ->addOption('system', 's', InputOption::VALUE_OPTIONAL, 'System Provider'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $name = $input->getArgument('name'); $path = APP_DIR . "Providers/"; $namespace = "App\\Providers"; - if ($input->hasOption('s')) { + if ($input->hasOption('system')) { $path = SYS_DIR . "Providers/"; $namespace = "InitPHP\\Framework\\Providers"; } @@ -41,21 +51,7 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Providers.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a service provider.'; - } - - public function arguments(): array - { - return []; + return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } } diff --git a/system/Console/Commands/MakeRequestCommand.php b/system/Console/Commands/MakeRequestCommand.php index d6f1ebb..7af5dab 100644 --- a/system/Console/Commands/MakeRequestCommand.php +++ b/system/Console/Commands/MakeRequestCommand.php @@ -15,17 +15,26 @@ namespace InitPHP\Framework\Console\Commands; use InitPHP\Framework\Console\Utils\MakeFile; -use \InitPHP\Console\{Input, Output}; +use Symfony\Component\Console\Input\InputArgument; +use \InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class MakeRequestCommand extends \InitPHP\Framework\Console\Command +class MakeRequestCommand extends Command { - /** @var string Command */ - public $command = 'make:request'; + protected static $defaultName = 'make:request'; - public function execute(Input $input, Output $output) + protected function configure(): void { - $name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/"); + $this->setDescription('Creates a request.') + ->addArgument('name', InputArgument::REQUIRED, 'Request class name') + ->setHelp('--name=RequestName'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $name = $input->getArgument('name'); $path = APP_DIR . "HTTP/Requests/"; $namespace = "App\\HTTP\\Requests"; @@ -39,21 +48,7 @@ public function execute(Input $input, Output $output) $path .= $name . ".php"; $make = new MakeFile(SYS_DIR . "Console/Templates/Requests.txt"); - if ($make->to($path, ["name" => $name, "namespace" => $namespace])) { - $output->success("Ok"); - } else { - $output->error("Error"); - } - } - - public function definition(): string - { - return 'Creates a request.'; - } - - public function arguments(): array - { - return []; + return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } } diff --git a/system/Console/Commands/RouteListCommand.php b/system/Console/Commands/RouteListCommand.php index 9f2c304..a83fd7b 100644 --- a/system/Console/Commands/RouteListCommand.php +++ b/system/Console/Commands/RouteListCommand.php @@ -15,17 +15,25 @@ namespace InitPHP\Framework\Console\Commands; use InitPHP\Framework\Base; -use \InitPHP\Console\{Input, Output}; - +use \InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use const PHP_EOL; -class RouteListCommand extends \InitPHP\Framework\Console\Command +class RouteListCommand extends Command { - /** @var string Command */ - public $command = 'route:list'; + protected static $defaultName = 'route:list'; + + + protected function configure(): void + { + $this->setDescription('Routes list.') + ->addArgument('method', InputArgument::OPTIONAL, 'HTTP Method'); + } - public function execute(Input $input, Output $output) + protected function execute(InputInterface $input, OutputInterface $output): int { if ($input->hasArgument('method')) { $routes = [ @@ -56,16 +64,8 @@ public function execute(Input $input, Output $output) } $output->write(PHP_EOL . $table->getContent() . PHP_EOL); - } - public function definition(): string - { - return 'Routes list.'; - } - - public function arguments(): array - { - return []; + return Command::SUCCESS; } } diff --git a/system/Console/Commands/ServeCommand.php b/system/Console/Commands/ServeCommand.php index 0fb4767..6569238 100644 --- a/system/Console/Commands/ServeCommand.php +++ b/system/Console/Commands/ServeCommand.php @@ -14,15 +14,17 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; -use \InitPHP\Console\{Input, Output}; +use \InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class ServeCommand extends \InitPHP\Framework\Console\Command +class ServeCommand extends Command { - /** @var string Command */ - public $command = 'serve'; + protected static $defaultName = 'serve'; - public function execute(Input $input, Output $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $host = $input->hasArgument("host") ? $input->getArgument("host") : "127.0.0.1"; $port = $input->hasArgument("port") ? $input->getArgument("port") : 8000; @@ -30,16 +32,15 @@ public function execute(Input $input, Output $output) $shell = "php -S " . $host . ":" . $port . " -t \"" . PUBLIC_DIR . "\""; exec($shell); - } - public function definition(): string - { - return 'It runs a PHP Web server.'; + return Command::SUCCESS; } - public function arguments(): array + protected function configure(): void { - return []; + $this->setDescription('It runs a PHP Web server.') + ->addArgument('host', InputArgument::OPTIONAL, 'The host name or IP', '127.0.0.1') + ->addArgument('port', InputArgument::OPTIONAL, 'The port', '8000'); } } diff --git a/system/Console/Commands/StorageLinkCommand.php b/system/Console/Commands/StorageLinkCommand.php index c70e3e8..2ce9e83 100644 --- a/system/Console/Commands/StorageLinkCommand.php +++ b/system/Console/Commands/StorageLinkCommand.php @@ -14,26 +14,24 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; -use \InitPHP\Console\{Input, Output}; use InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; class StorageLinkCommand extends Command { - public $command = 'storage:link'; + protected static $defaultName = 'storage:link'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { - if (symlink(STORAGE_DIR . "public/", PUBLIC_DIR . "storage")) { - $output->success("Shortcut created!"); - } else { - $output->error("Shortcut failed!"); - } + return symlink(STORAGE_DIR . "public/", PUBLIC_DIR . "storage") ? Command::SUCCESS : Command::FAILURE; } - public function definition(): string + protected function configure(): void { - return 'Creates a shortcut to the storage/public directory in public_html.'; + $this->setDescription('Creates a shortcut to the storage/public directory in public_html.') + ->setHelp(''); } } diff --git a/system/Console/Commands/ViewCacheClearCommand.php b/system/Console/Commands/ViewCacheClearCommand.php index d0a8f3f..5383a5e 100644 --- a/system/Console/Commands/ViewCacheClearCommand.php +++ b/system/Console/Commands/ViewCacheClearCommand.php @@ -14,15 +14,16 @@ declare(strict_types=1); namespace InitPHP\Framework\Console\Commands; -use \InitPHP\Console\{Input, Output}; +use InitPHP\Framework\Console\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; -class ViewCacheClearCommand extends \InitPHP\Framework\Console\Command +class ViewCacheClearCommand extends Command { - /** @var string Command */ - public $command = 'view:clear'; + protected static $defaultName = 'view:clear'; - public function execute(Input $input, Output $output) + public function execute(InputInterface $input, OutputInterface $output): int { $path = STORAGE_DIR . "cache/views/*"; $caches = glob($path); @@ -33,17 +34,13 @@ public function execute(Input $input, Output $output) $output->writeln($cache); unlink($cache); } - $output->success("Ok"); + return Command::SUCCESS; } - public function definition(): string + protected function configure(): void { - return 'View cache clear'; - } - - public function arguments(): array - { - return []; + $this->setDescription('View cache clear') + ->setHelp(''); } } diff --git a/system/Console/Templates/Command.txt b/system/Console/Templates/Command.txt index 56ea29d..4b31ad9 100644 --- a/system/Console/Templates/Command.txt +++ b/system/Console/Templates/Command.txt @@ -2,27 +2,33 @@ namespace {namespace}; -use \InitPHP\Console\{Input, Output}; +use \InitPHP\Framework\Console\Command; +use \Symfony\Component\Console\Input\InputInterface; +use \Symfony\Component\Console\Output\OutputInterface; -class {name} extends \InitPHP\Framework\Console\Command +class {name} extends Command { - /** @var string Command */ - public $command = ''; + /** @var string */ + protected static $defaultName = 'app:my-command'; - public function execute(Input $input, Output $output) + /** + * @inheritDoc + */ + public function execute(InputInterface $input, OutputInterface $output): int { // TODO: Implement execute() method. - } - public function definition(): string - { - return ''; + return Command::SUCCESS; } - public function arguments(): array + /** + * @inheritDoc + */ + public function configure() { - return []; + $this->setDescription('Command Description') + ->setHelp('Help Message'); } } diff --git a/system/Providers/ConsoleServiceProvider.php b/system/Providers/ConsoleServiceProvider.php index 09863b2..119f270 100644 --- a/system/Providers/ConsoleServiceProvider.php +++ b/system/Providers/ConsoleServiceProvider.php @@ -15,7 +15,7 @@ namespace InitPHP\Framework\Providers; use InitPHP\Framework\Base; -use InitPHP\Framework\Providers\AbstractProvider; +use Symfony\Component\Console\Application; class ConsoleServiceProvider extends AbstractProvider { @@ -25,14 +25,14 @@ class ConsoleServiceProvider extends AbstractProvider */ public function boot(): void { - $console = new \InitPHP\Console\Application("InitPHP Framework", "3.0"); + $console = new Application("InitPHP Framework", "3.0"); $commands = glob(SYS_DIR . "Console/Commands/*.php"); foreach ($commands as $command) { $commandClass = '\\InitPHP\\Framework\\Console\\Commands\\' . basename($command, '.php'); if (!class_exists($commandClass)) { continue; } - $console->register($commandClass); + $console->add(new $commandClass()); } $commands = glob(APP_DIR . "Console/Commands/*.php"); foreach ($commands as $command) { @@ -40,7 +40,7 @@ public function boot(): void if (!class_exists($commandClass)) { continue; } - $console->register($commandClass); + $console->add(new $commandClass()); } !Base::hasProperty('console') && Base::setProperty('console', $console); } From 9b43d02f2e2a7d65d62633a41bfcf89bf515b581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9EAFAK?= Date: Sat, 23 Dec 2023 07:11:31 +0300 Subject: [PATCH 2/3] Symfony console application after bug fix --- .../Console/Commands/MakeCommandCommand.php | 3 +- .../Commands/MakeControllerCommand.php | 1 - system/Console/Commands/MakeEntityCommand.php | 1 - system/Console/Commands/MakeModelCommand.php | 5 +- .../Console/Commands/MakeProviderCommand.php | 6 +- .../Console/Commands/MakeRequestCommand.php | 9 +- system/Console/Commands/RouteListCommand.php | 7 +- system/Console/Commands/ServeCommand.php | 14 +- .../Console/Commands/StorageLinkCommand.php | 3 +- .../Commands/ViewCacheClearCommand.php | 3 +- system/Console/Templates/Model.txt | 2 +- system/Console/Utils/Table.php | 282 ++++++++++++++++++ system/Helpers/str_helper.php | 12 +- 13 files changed, 310 insertions(+), 38 deletions(-) create mode 100644 system/Console/Utils/Table.php diff --git a/system/Console/Commands/MakeCommandCommand.php b/system/Console/Commands/MakeCommandCommand.php index 7f0058f..2c99ae8 100644 --- a/system/Console/Commands/MakeCommandCommand.php +++ b/system/Console/Commands/MakeCommandCommand.php @@ -52,9 +52,8 @@ public function execute(InputInterface $input, OutputInterface $output): int protected function configure(): void { $this->setDescription('Creates a command.') - ->setHelp('--name=CommandName') ->addArgument('name', InputArgument::REQUIRED, 'The name of the command class.') - ->addOption('system', 's', InputOption::VALUE_OPTIONAL, 'Creates a system command.'); + ->addOption('system', 's', InputOption::VALUE_NONE, 'Creates a system command.'); } } \ No newline at end of file diff --git a/system/Console/Commands/MakeControllerCommand.php b/system/Console/Commands/MakeControllerCommand.php index fd41c08..f67c881 100644 --- a/system/Console/Commands/MakeControllerCommand.php +++ b/system/Console/Commands/MakeControllerCommand.php @@ -48,7 +48,6 @@ public function execute(InputInterface $input, OutputInterface $output): int protected function configure(): void { $this->setDescription('Creates a controller.') - ->setHelp('--name=ControllerName') ->addArgument('name', InputArgument::REQUIRED, 'The name of the controller class.'); } diff --git a/system/Console/Commands/MakeEntityCommand.php b/system/Console/Commands/MakeEntityCommand.php index de9cba7..160f66c 100644 --- a/system/Console/Commands/MakeEntityCommand.php +++ b/system/Console/Commands/MakeEntityCommand.php @@ -35,7 +35,6 @@ public function execute(InputInterface $input, OutputInterface $output): int protected function configure(): void { $this->setDescription('Creates a entity.') - ->setHelp('--name=EntityName') ->addArgument('name', InputArgument::REQUIRED, 'The name of the entity class.'); } diff --git a/system/Console/Commands/MakeModelCommand.php b/system/Console/Commands/MakeModelCommand.php index cb005aa..addaade 100644 --- a/system/Console/Commands/MakeModelCommand.php +++ b/system/Console/Commands/MakeModelCommand.php @@ -30,15 +30,14 @@ protected function configure(): void { $this->setDescription('Creates a model.') ->addArgument('name', InputArgument::REQUIRED, 'Model class name') - ->addOption('entity', 'e', InputOption::VALUE_OPTIONAL, 'Create Entity Class.'); + ->addOption('entity', 'e', InputOption::VALUE_NONE, 'Create Entity Class.'); } protected function execute(InputInterface $input, OutputInterface $output): int { $name = trim($input->getArgument('name'), "/"); - $entity = null; - if ($input->hasOption('entity')) { + if ($input->getOption('entity')) { $entity = MakeEntityCommand::makeEntity($name); } empty($entity) && $entity = "\\InitPHP\\Framework\\Database\\Entity::class"; diff --git a/system/Console/Commands/MakeProviderCommand.php b/system/Console/Commands/MakeProviderCommand.php index d7faa8b..93052c2 100644 --- a/system/Console/Commands/MakeProviderCommand.php +++ b/system/Console/Commands/MakeProviderCommand.php @@ -30,7 +30,7 @@ protected function configure(): void { $this->setDescription('Creates a service provider.') ->addArgument('name', InputArgument::REQUIRED, 'Provider Name') - ->addOption('system', 's', InputOption::VALUE_OPTIONAL, 'System Provider'); + ->addOption('system', 's', InputOption::VALUE_NONE, 'System Provider'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -38,7 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $name = $input->getArgument('name'); $path = APP_DIR . "Providers/"; $namespace = "App\\Providers"; - if ($input->hasOption('system')) { + if ($input->getOption('system')) { $path = SYS_DIR . "Providers/"; $namespace = "InitPHP\\Framework\\Providers"; } @@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $path .= implode("/", $split) . "/"; } $path .= $name . ".php"; - $make = new MakeFile(SYS_DIR . "Console/Templates/Providers.txt"); + $make = new MakeFile(SYS_DIR . "Console/Templates/Provider.txt"); return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; } diff --git a/system/Console/Commands/MakeRequestCommand.php b/system/Console/Commands/MakeRequestCommand.php index 7af5dab..b9b90bd 100644 --- a/system/Console/Commands/MakeRequestCommand.php +++ b/system/Console/Commands/MakeRequestCommand.php @@ -28,8 +28,7 @@ class MakeRequestCommand extends Command protected function configure(): void { $this->setDescription('Creates a request.') - ->addArgument('name', InputArgument::REQUIRED, 'Request class name') - ->setHelp('--name=RequestName'); + ->addArgument('name', InputArgument::REQUIRED, 'Request class name'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -46,9 +45,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $path .= implode("/", $split) . "/"; } $path .= $name . ".php"; - $make = new MakeFile(SYS_DIR . "Console/Templates/Requests.txt"); - return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE; + $write = (new MakeFile(SYS_DIR . 'Console/Templates/Request.txt')) + ->to($path, ['name' => $name, 'namespace' => $namespace]); + + return $write ? Command::SUCCESS : Command::FAILURE; } } diff --git a/system/Console/Commands/RouteListCommand.php b/system/Console/Commands/RouteListCommand.php index a83fd7b..d462a64 100644 --- a/system/Console/Commands/RouteListCommand.php +++ b/system/Console/Commands/RouteListCommand.php @@ -16,6 +16,7 @@ use InitPHP\Framework\Base; use \InitPHP\Framework\Console\Command; +use InitPHP\Framework\Console\Utils\Table; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -35,7 +36,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - if ($input->hasArgument('method')) { + if (!empty($input->getArgument('method'))) { $routes = [ strtoupper($input->getArgument('method')) => Base::getProperty('router')->getRoutes($input->getArgument('method')) ]; @@ -44,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $i = 0; - $table = new \InitPHP\Console\Utils\Table(); + $table = new Table(); foreach ($routes as $method => $route) { foreach ($route as $path => $row) { $execute = $row['execute']; @@ -63,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } - $output->write(PHP_EOL . $table->getContent() . PHP_EOL); + $output->writeln(PHP_EOL . $table->getContent() . PHP_EOL); return Command::SUCCESS; } diff --git a/system/Console/Commands/ServeCommand.php b/system/Console/Commands/ServeCommand.php index 6569238..6206094 100644 --- a/system/Console/Commands/ServeCommand.php +++ b/system/Console/Commands/ServeCommand.php @@ -17,6 +17,7 @@ use \InitPHP\Framework\Console\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class ServeCommand extends Command @@ -24,11 +25,10 @@ class ServeCommand extends Command protected static $defaultName = 'serve'; - protected function execute(InputInterface $input, OutputInterface $output): int + protected function execute(InputInterface $input, OutputInterface $output) { - $host = $input->hasArgument("host") ? $input->getArgument("host") : "127.0.0.1"; - $port = $input->hasArgument("port") ? $input->getArgument("port") : 8000; - !is_int($port) && $port = 8000; + $host = $input->getOption("host"); + $port = intval($input->getOption("port")); $shell = "php -S " . $host . ":" . $port . " -t \"" . PUBLIC_DIR . "\""; exec($shell); @@ -36,11 +36,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::SUCCESS; } - protected function configure(): void + protected function configure() { $this->setDescription('It runs a PHP Web server.') - ->addArgument('host', InputArgument::OPTIONAL, 'The host name or IP', '127.0.0.1') - ->addArgument('port', InputArgument::OPTIONAL, 'The port', '8000'); + ->addOption('host', 'host', InputOption::VALUE_OPTIONAL, 'The host name or IP', '127.0.0.1') + ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'The port', '8000'); } } diff --git a/system/Console/Commands/StorageLinkCommand.php b/system/Console/Commands/StorageLinkCommand.php index 2ce9e83..39fe99b 100644 --- a/system/Console/Commands/StorageLinkCommand.php +++ b/system/Console/Commands/StorageLinkCommand.php @@ -30,8 +30,7 @@ public function execute(InputInterface $input, OutputInterface $output): int protected function configure(): void { - $this->setDescription('Creates a shortcut to the storage/public directory in public_html.') - ->setHelp(''); + $this->setDescription('Creates a shortcut to the storage/public directory in public_html.'); } } diff --git a/system/Console/Commands/ViewCacheClearCommand.php b/system/Console/Commands/ViewCacheClearCommand.php index 5383a5e..90a9f4b 100644 --- a/system/Console/Commands/ViewCacheClearCommand.php +++ b/system/Console/Commands/ViewCacheClearCommand.php @@ -39,8 +39,7 @@ public function execute(InputInterface $input, OutputInterface $output): int protected function configure(): void { - $this->setDescription('View cache clear') - ->setHelp(''); + $this->setDescription('View cache clear'); } } diff --git a/system/Console/Templates/Model.txt b/system/Console/Templates/Model.txt index da66c76..e4ead96 100644 --- a/system/Console/Templates/Model.txt +++ b/system/Console/Templates/Model.txt @@ -7,7 +7,7 @@ use \InitPHP\Framework\Database\Model; class {name} extends Model { - protected string $schema = {schema}; + protected string $schema = "{schema}"; protected string $schemaId = 'id'; diff --git a/system/Console/Utils/Table.php b/system/Console/Utils/Table.php new file mode 100644 index 0000000..ae84488 --- /dev/null +++ b/system/Console/Utils/Table.php @@ -0,0 +1,282 @@ + + * @copyright Copyright © 2023 InitPHP Framework + * @license http://initphp.github.io/license.txt MIT + * @version 3.0 + * @link https://www.muhammetsafak.com.tr + */ + +declare(strict_types=1); +namespace InitPHP\Framework\Console\Utils; + +class Table +{ + + public const COLOR_DEFAULT = 39; + public const COLOR_BLACK = 30; + public const COLOR_RED = 31; + public const COLOR_GREEN = 32; + public const COLOR_YELLOW = 33; + public const COLOR_BLUE = 34; + public const COLOR_MAGENTA = 35; + public const COLOR_CYAN = 36; + public const COLOR_LIGHT_GRAY = 37; + public const COLOR_DARK_GRAY = 90; + public const COLOR_LIGHT_RED = 91; + public const COLOR_LIGHT_GREEN = 92; + public const COLOR_LIGHT_YELLOW = 93; + public const COLOR_LIGHT_BLUE = 94; + public const COLOR_LIGHT_MAGENTA= 95; + public const COLOR_LIGHT_CYAN = 96; + public const COLOR_WHITE = 97; + + public const BACKGROUND_BLACK = 40; + public const BACKGROUND_RED = 41; + public const BACKGROUND_GREEN = 42; + public const BACKGROUND_YELLOW = 43; + public const BACKGROUND_BLUE = 44; + public const BACKGROUND_MAGENTA = 45; + public const BACKGROUND_CYAN = 46; + + public const ITALIC = 3; + public const BOLD = 1; + public const UNDERLINE = 4; + public const STRIKETHROUGH = 9; + + private $headerStyle = [ + self::BOLD, + ]; + + private $cellStyle = []; + + private $borderStyle = []; + + private $columnCellStyle = []; + + private $chars = [ + 'top' => '═', + 'top-mid' => '╤', + 'top-left' => '╔', + 'top-right' => '╗', + 'bottom' => '═', + 'bottom-mid' => '╧', + 'bottom-left' => '╚', + 'bottom-right' => '╝', + 'left' => '║', + 'left-mid' => '╟', + 'mid' => '─', + 'mid-mid' => '┼', + 'right' => '║', + 'right-mid' => '╢', + 'middle' => '│ ', + ]; + + /** @var array */ + private $rows = []; + + public function __construct() + { + } + + public function __toString() + { + return $this->getContent(); + } + + public static function create(): Table + { + return new self(); + } + + public function setHeaderStyle(int ...$format): self + { + $styles = []; + foreach ($format as $style) { + $styles[] = $style; + } + $this->headerStyle = $styles; + return $this; + } + + public function setCellStyle(int ...$format): self + { + $styles = []; + foreach ($format as $style) { + $styles[] = $style; + } + $this->cellStyle = $styles; + return $this; + } + + public function setBorderStyle(int ...$format): self + { + $styles = []; + foreach ($format as $style) { + $styles[] = $style; + } + $this->borderStyle = $styles; + return $this; + } + + public function setColumnCellStyle(string $column, int ...$format): self + { + $styles = []; + foreach ($format as $style) { + $styles[] = $style; + } + $this->columnCellStyle[$column] = $styles; + return $this; + } + + public function row(array $assoc): self + { + $row = []; + foreach ($assoc as $key => $value) { + if(!\is_string($value)){ + if(\is_object($value)){ + $value = \get_class($value); + }elseif (\is_resource($value)) { + $value = '[RESOURCE]'; + }elseif (\is_callable($value)){ + $value = '[CALLABLE]'; + }elseif(\is_null($value)){ + $value = '[NULL]'; + }elseif(\is_bool($value)){ + $value = $value === FALSE ? '[FALSE]' : '[TRUE]'; + }else{ + $value = (string)$value; + } + } + $key = \trim((string)$key); + $row[$key] = \trim($value); + } + $this->rows[] = $row; + return $this; + } + + public function getContent(): string + { + $columnLengths = []; + $headerData = []; + + foreach ($this->rows as $row) { + $keys = \array_keys($row); + foreach ($keys as $key) { + if(isset($headerData[$key])){ + continue; + } + $headerData[$key] = $key; + $columnLengths[$key] = $this->strlen($key); + } + } + + foreach ($this->rows as $row) { + foreach ($headerData as $column) { + $len = \max($columnLengths[$column], $this->strlen($row[$column])); + if($len % 2 !== 0){ + ++$len; + } + $columnLengths[$column] = $len; + } + } + foreach ($columnLengths as &$length) { + $length += 4; + } + + $res = $this->getTableTopContent($columnLengths) + . $this->getFormattedRowContent($headerData, $columnLengths, "\e[" . \implode(';', $this->headerStyle) . "m", true) + . $this->getTableSeparatorContent($columnLengths); + foreach ($this->rows as $row) { + foreach ($headerData as $column) { + if(!isset($row[$column])){ + $row[$column] = '[NULL]'; + } + } + $res .= $this->getFormattedRowContent($row, $columnLengths, "\e[" . \implode(';', $this->cellStyle) . "m"); + } + return $res . $this->getTableBottomContent($columnLengths); + } + + private function getFormattedRowContent($data, $lengths, string $format = '', bool $isHeader = false): string + { + $res = $this->getChar('left') . ' '; + $rows = []; + foreach ($data as $key => $value) { + $customFormat = ''; + $value = ' ' . $value; + $len = $this->strlen($value) - $lengths[$key] + 1; + if($isHeader === FALSE && isset($this->columnCellStyle[$key]) && !empty($this->columnCellStyle[$key])){ + $customFormat = "\e[" . \implode(";", $this->columnCellStyle[$key]) . "m"; + } + $rows[] = ($format !== '' ? $format : '') + . ($customFormat !== '' ? $customFormat : '') + . $value + . ($format !== '' || $customFormat !== '' ? "\e[0m" : '') + . \str_repeat(' ', (int)\abs($len)); + } + $res .= \implode($this->getChar('middle'), $rows); + return $res . $this->getChar('right') . \PHP_EOL; + } + + private function getTableTopContent($lengths): string + { + $res = $this->getChar('top-left'); + $rows = []; + foreach ($lengths as $length) { + $rows[] = $this->getChar('top', $length); + } + $res .= \implode($this->getChar('top-mid'), $rows); + return $res . $this->getChar('top-right') . \PHP_EOL; + } + + private function getTableBottomContent($lengths): string + { + $res = $this->getChar('bottom-left'); + $rows = []; + foreach ($lengths as $length) { + $rows[] = $this->getChar('bottom', $length); + } + $res .= \implode($this->getChar('bottom-mid'), $rows); + return $res . $this->getChar('bottom-right') . \PHP_EOL; + } + + private function getTableSeparatorContent($lengths): string + { + $res = $this->getChar('left-mid'); + $rows = []; + foreach ($lengths as $length) { + $rows[] = $this->getChar('mid', $length); + } + $res .= \implode($this->getChar('mid-mid'), $rows); + return $res . $this->getChar('right-mid') . \PHP_EOL; + } + + private function getChar(string $char, int $len = 1): string + { + if(!isset($this->chars[$char])){ + return ''; + } + $res = (empty($this->borderStyle) ? '' : "\e[" . \implode(";", $this->borderStyle) . "m"); + if($len === 1){ + $res .= $this->chars[$char];; + }else{ + $res .= \str_repeat($this->chars[$char], $len); + } + $res .= empty($this->borderStyle) ? '' : "\e[0m"; + return $res; + } + + private function strlen(string $str): int + { + if(!\function_exists('mb_strlen')){ + return \strlen($str); + } + return \mb_strlen($str); + } + +} \ No newline at end of file diff --git a/system/Helpers/str_helper.php b/system/Helpers/str_helper.php index 3c1e3ef..0ecaa0b 100644 --- a/system/Helpers/str_helper.php +++ b/system/Helpers/str_helper.php @@ -17,16 +17,10 @@ function camelCase2SnakeCase(string $camelCase): string { $string = lcfirst($camelCase); - $split = preg_split('', $string, -1, PREG_SPLIT_NO_EMPTY); - $snake_case = ''; - $i = 0; - foreach ($split as $row) { - $snake_case .= ($i === 0 ? '_' : '') - . strtolower($row); - ++$i; - } - return lcfirst($snake_case); + return preg_replace_callback('/[A-Z]/', function ($match) { + return '_' . strtolower($match[0]); + }, $string); } } From cc192a547c0269caa79253781aad45d9ce803acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9EAFAK?= Date: Sat, 23 Dec 2023 07:13:49 +0300 Subject: [PATCH 3/3] added license --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..568f8f5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 InitPHP + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file