From b757b6bccf2dffbcf80225561c30eb9186b0f499 Mon Sep 17 00:00:00 2001 From: Ryuk <41i.6h0rb4n1@gmail.com> Date: Tue, 8 Sep 2020 21:05:34 +0430 Subject: [PATCH] Add cli for creating controllers and middlewares --- composer.json | 3 +- src/Commands/ControllerTemplate.php | 18 ++++ src/Commands/CreateControllerCommand.php | 89 +++++++++++++++++++ src/Commands/CreateMiddlewareCommand.php | 89 +++++++++++++++++++ src/Commands/MiddlewareTemplate.php | 21 +++++ src/console | 14 +++ .../commands/CreateControllerCommandTest.php | 75 ++++++++++++++++ .../commands/CreateMiddlewareCommandTest.php | 75 ++++++++++++++++ 8 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 src/Commands/ControllerTemplate.php create mode 100644 src/Commands/CreateControllerCommand.php create mode 100644 src/Commands/CreateMiddlewareCommand.php create mode 100644 src/Commands/MiddlewareTemplate.php create mode 100644 src/console create mode 100644 tests/commands/CreateControllerCommandTest.php create mode 100644 tests/commands/CreateMiddlewareCommandTest.php diff --git a/composer.json b/composer.json index 949090f..c7267b8 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=5.5.0" + "php": ">=5.5.0", + "symfony/console": "^5.1" }, "require-dev": { "phpunit/phpunit": "^4.8 || ^5.7 || ^6.5 || ^7.0", diff --git a/src/Commands/ControllerTemplate.php b/src/Commands/ControllerTemplate.php new file mode 100644 index 0000000..13391fc --- /dev/null +++ b/src/Commands/ControllerTemplate.php @@ -0,0 +1,18 @@ + + +namespace ; + +/** + * Class + + * + * @package + +*/ +class + +{ + public function main() + { + } +} diff --git a/src/Commands/CreateControllerCommand.php b/src/Commands/CreateControllerCommand.php new file mode 100644 index 0000000..5164104 --- /dev/null +++ b/src/Commands/CreateControllerCommand.php @@ -0,0 +1,89 @@ +setDescription('Create a new Controller'); + $this->addArgument('name', InputArgument::REQUIRED, 'Name of the controller'); + $this->addArgument('path', InputArgument::OPTIONAL, 'Absolute path of the controller\'s directory'); + $this->addArgument('namespace', InputArgument::OPTIONAL, 'Namespace of the controller directory'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $className = $input->getArgument('name'); + + if (!is_dir($directory = $input->getArgument('path'))) { + if (is_null($directory)) { + $directory = explode('/vendor/', getcwd())[0] . '/Controllers'; + + $output->writeln([ + '', + 'Default directory is: ' . $directory, + ]); + } else { + try { + mkdir($directory); + } catch (\Exception $exception) { + throw new \Exception($exception->getMessage()); + } + } + } + + if (is_null($namespace = $input->getArgument('namespace'))) { + $namespace = 'Controllers'; + + $output->writeln([ + '', + 'Default namespace is: ' . $namespace, + ]); + } + + $filePath = $directory . "/{$className}.php"; + if (is_file($filePath)) { + throw new \Exception('The controller already exists!'); + } + + try { + ob_start(); + echo 'writeln([ + '', + 'SUCCESS!', + ]); + + return Command::SUCCESS; + } catch (\Exception $exception) { + $output->writeln([ + '', + 'ERROR: ' . $exception->getMessage(), + ]); + + return Command::FAILURE; + } + } +} diff --git a/src/Commands/CreateMiddlewareCommand.php b/src/Commands/CreateMiddlewareCommand.php new file mode 100644 index 0000000..d243739 --- /dev/null +++ b/src/Commands/CreateMiddlewareCommand.php @@ -0,0 +1,89 @@ +setDescription('Create a new Middleware'); + $this->addArgument('name', InputArgument::REQUIRED, 'Name of the middleware'); + $this->addArgument('path', InputArgument::OPTIONAL, 'Absolute path of the middleware\'s directory'); + $this->addArgument('namespace', InputArgument::OPTIONAL, 'Namespace of the middleware directory'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $className = $input->getArgument('name'); + + if (!is_dir($directory = $input->getArgument('path'))) { + if (is_null($directory)) { + $directory = explode('/vendor/', getcwd())[0] . '/Middlewares'; + + $output->writeln([ + '', + 'Default directory is: ' . $directory, + ]); + } else { + try { + mkdir($directory); + } catch (\Exception $exception) { + throw new \Exception($exception->getMessage()); + } + } + } + + if (is_null($namespace = $input->getArgument('namespace'))) { + $namespace = 'Middlewares'; + + $output->writeln([ + '', + 'Default namespace is: ' . $namespace, + ]); + } + + $filePath = $directory . "/{$className}.php"; + if (is_file($filePath)) { + throw new \Exception('The middleware already exists!'); + } + + try { + ob_start(); + echo 'writeln([ + '', + 'SUCCESS!', + ]); + + return Command::SUCCESS; + } catch (\Exception $exception) { + $output->writeln([ + '', + 'ERROR: ' . $exception->getMessage(), + ]); + + return Command::FAILURE; + } + } +} diff --git a/src/Commands/MiddlewareTemplate.php b/src/Commands/MiddlewareTemplate.php new file mode 100644 index 0000000..44d4ca4 --- /dev/null +++ b/src/Commands/MiddlewareTemplate.php @@ -0,0 +1,21 @@ + + +namespace ; + +/** + * Class + + * + * @package + +*/ +class + +{ + public function handle() + { + // your middleware codes + + return true; + } +} diff --git a/src/console b/src/console new file mode 100644 index 0000000..08476ef --- /dev/null +++ b/src/console @@ -0,0 +1,14 @@ +#!/usr/bin/env php +add(new \Buki\Commands\CreateControllerCommand()); +$application->add(new \Buki\Commands\CreateMiddlewareCommand()); + +$application->run(); + diff --git a/tests/commands/CreateControllerCommandTest.php b/tests/commands/CreateControllerCommandTest.php new file mode 100644 index 0000000..37a7b04 --- /dev/null +++ b/tests/commands/CreateControllerCommandTest.php @@ -0,0 +1,75 @@ +add(new CreateControllerCommand()); + $command = $application->find('make:controller'); + $this->commandTester = new CommandTester($command); + } + + public function testExecute() + { + $this->commandTester->execute([ + 'name' => 'TestController', + 'path' => __DIR__ . '/Controllers', + 'namespace' => 'Controllers', + ]); + + $this->assertEquals(Command::SUCCESS, $this->commandTester->getStatusCode()); + } + + public function testExecuteShouldThrowExceptionForEmptyName() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments (missing: "name")'); + + $this->commandTester->execute([ + // name is required + ]); + } + + public function testExecuteShouldThrowExceptionForExistentController() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The controller already exists!'); + + $this->commandTester->execute([ + 'name' => 'TestController', + 'path' => __DIR__ . '/Controllers', + 'namespace' => 'Controllers', + ]); + } + + public function testExecuteShouldThrowExceptionForMkdirPermissionDenied() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('mkdir(): Permission denied'); + + $this->commandTester->execute([ + 'name' => 'TestController', + 'path' => '/SomeDirectory', + 'namespace' => 'Controllers', + ]); + } +} diff --git a/tests/commands/CreateMiddlewareCommandTest.php b/tests/commands/CreateMiddlewareCommandTest.php new file mode 100644 index 0000000..229d209 --- /dev/null +++ b/tests/commands/CreateMiddlewareCommandTest.php @@ -0,0 +1,75 @@ +add(new CreateMiddlewareCommand()); + $command = $application->find('make:middleware'); + $this->commandTester = new CommandTester($command); + } + + public function testExecute() + { + $this->commandTester->execute([ + 'name' => 'TestMiddleware', + 'path' => __DIR__ . '/Middlewares', + 'namespace' => 'Middlewares', + ]); + + $this->assertEquals(Command::SUCCESS, $this->commandTester->getStatusCode()); + } + + public function testExecuteShouldThrowExceptionForEmptyName() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments (missing: "name")'); + + $this->commandTester->execute([ + // name is required + ]); + } + + public function testExecuteShouldThrowExceptionForExistentMiddleware() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The middleware already exists!'); + + $this->commandTester->execute([ + 'name' => 'TestMiddleware', + 'path' => __DIR__ . '/Middlewares', + 'namespace' => 'Middlewares', + ]); + } + + public function testExecuteShouldThrowExceptionForMkdirPermissionDenied() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('mkdir(): Permission denied'); + + $this->commandTester->execute([ + 'name' => 'TestMiddleware', + 'path' => '/SomeDirectory', + 'namespace' => 'Middlewares', + ]); + } +}