From 58621567290fe951ce81a4e7943324f3be88068b Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 07:53:13 +0530 Subject: [PATCH 1/7] =?UTF-8?q?=E2=9C=A8=20Extract=20Build=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Builder.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/console/Builder.php b/core/console/Builder.php index c331345..d436070 100644 --- a/core/console/Builder.php +++ b/core/console/Builder.php @@ -12,7 +12,12 @@ class Builder private bool $isApiController = false; private ?string $associatedModel = null; - const BUILD_COMMANDS = ["build:model", "build:controller", "build:middleware", "build:command"]; + const BUILD_MODEL = "build:model"; + const BUILD_CONTROLLER = "build:controller"; + const BUILD_MIDDLEWARE = "build:middleware"; + const BUILD_COMMAND = "build:command"; + + const BUILD_COMMANDS = [self::BUILD_MODEL, self::BUILD_CONTROLLER, self::BUILD_MIDDLEWARE, self::BUILD_COMMAND]; public function __construct(private string $resource, private array $args) { @@ -24,7 +29,7 @@ public function __construct(private string $resource, private array $args) continue; } - if ($this->resource == self::BUILD_COMMANDS[1]) { + if ($this->resource == self::BUILD_CONTROLLER) { if ($arg == "--api") { $this->isApiController = true; } @@ -55,13 +60,13 @@ public function __construct(private string $resource, private array $args) public function generateResource() { switch ($this->resource) { - case self::BUILD_COMMANDS[0]: + case self::BUILD_MODEL: return $this->generateModel(); - case self::BUILD_COMMANDS[1]: + case self::BUILD_CONTROLLER: return $this->generateController(); - case self::BUILD_COMMANDS[2]: + case self::BUILD_MIDDLEWARE: return $this->generateMiddleware(); - case self::BUILD_COMMANDS[3]: + case self::BUILD_COMMAND: return $this->generateCommand(); } } From 4c376b8f375af5fb46a67fb3350e2a0499f239d3 Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 07:57:46 +0530 Subject: [PATCH 2/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Builder:=20extract?= =?UTF-8?q?=20controller=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Builder.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/core/console/Builder.php b/core/console/Builder.php index d436070..913558a 100644 --- a/core/console/Builder.php +++ b/core/console/Builder.php @@ -9,8 +9,10 @@ class Builder private string $name = ""; private bool $forceCreate = false; - private bool $isApiController = false; - private ?string $associatedModel = null; + private array $controllerConfig = [ + 'api' => false, + 'model' => null, + ]; const BUILD_MODEL = "build:model"; const BUILD_CONTROLLER = "build:controller"; @@ -31,14 +33,14 @@ public function __construct(private string $resource, private array $args) if ($this->resource == self::BUILD_CONTROLLER) { if ($arg == "--api") { - $this->isApiController = true; + $this->controllerConfig['api'] = true; } if (str_starts_with($arg, "--model=")) { - $this->associatedModel = explode("=", $arg)[1]; + $this->controllerConfig['model'] = explode("=", $arg)[1]; - if (!file_exists(Application::getInstance()->basePath() . "/app/models/" . $this->associatedModel . ".php")) { - console_log(Helper::redText("Error: The specified model (") . Helper::yellowText($this->associatedModel) . Helper::redText(") does not exist.")); + if (!file_exists(Application::getInstance()->basePath() . "/app/models/" . $this->controllerConfig['model'] . ".php")) { + console_log(Helper::redText("Error: The specified model (") . Helper::yellowText($this->controllerConfig['model']) . Helper::redText(") does not exist.")); exit(1); } } @@ -150,15 +152,15 @@ class $this->name extends Model private function getControllerContent() { - if ($this->isApiController) { - $modelParameter = is_null($this->associatedModel) ? "int \$id" : "$this->associatedModel $" . lcfirst($this->associatedModel); + if ($this->controllerConfig['api']) { + $modelParameter = is_null($this->controllerConfig['model']) ? "int \$id" : $this->controllerConfig['model'] . " $" . lcfirst($this->controllerConfig['model']); return "associatedModel) ? "use App\Models\\" . $this->associatedModel . ";\n" : "") . " +" . (!is_null($this->controllerConfig['model']) ? "use App\Models\\" . $this->controllerConfig['model'] . ";\n" : "") . " class $this->name { public function index() From c54fb342ccfcd74d2fabdd1a13c2984dcd8490c8 Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 07:59:40 +0530 Subject: [PATCH 3/7] =?UTF-8?q?=E2=9C=A8=20Builder:=20update=20message=20t?= =?UTF-8?q?o=20enable=20control-click=20in=20console?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/console/Builder.php b/core/console/Builder.php index 913558a..9b5778b 100644 --- a/core/console/Builder.php +++ b/core/console/Builder.php @@ -128,7 +128,7 @@ public function createFile($dir, $filename, $content) fwrite($file, $content); fclose($file); - console_log(Helper::greenText("File ") . Helper::purpleText("$filename.php") . Helper::greenText(" created successfully at ") . Helper::purpleText($dir)); + console_log(Helper::greenText(ucfirst(explode(":", $this->resource)[1]) . " created successfully at ") . Helper::purpleText($filePath)); } else { console_log(Helper::redText("Error: Unable to create the file.")); exit(1); From 2fa7418cfcbdfcd5514ed05cf1f0d2cd90f89c8c Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 08:10:16 +0530 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9C=A8=20Create=20controllers=20automati?= =?UTF-8?q?cally=20when=20building=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Builder.php | 26 ++++++++++++++++++++++++++ core/console/Helper.php | 32 ++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/core/console/Builder.php b/core/console/Builder.php index 9b5778b..7cfcec3 100644 --- a/core/console/Builder.php +++ b/core/console/Builder.php @@ -9,6 +9,10 @@ class Builder private string $name = ""; private bool $forceCreate = false; + private array $modelConfig = [ + 'api' => false, + 'controller' => false, + ]; private array $controllerConfig = [ 'api' => false, 'model' => null, @@ -31,6 +35,16 @@ public function __construct(private string $resource, private array $args) continue; } + if ($this->resource == self::BUILD_MODEL) { + if ($arg == "--api") { + $this->modelConfig['api'] = true; + } + + if ($arg == "-c") { + $this->modelConfig['controller'] = true; + } + } + if ($this->resource == self::BUILD_CONTROLLER) { if ($arg == "--api") { $this->controllerConfig['api'] = true; @@ -81,6 +95,18 @@ public function generateModel() $content = $this->getModelContent(); $this->createFile($dir, $this->name, $content); + + if ($this->modelConfig['controller'] || $this->modelConfig['api']) { + $args = []; + + if ($this->modelConfig['api']) { + $args[] = "--api"; + } + + $args[] = $this->name . "Controller"; + + return new Builder(self::BUILD_CONTROLLER, $args); + } } public function generateController() diff --git a/core/console/Helper.php b/core/console/Helper.php index 0cee0b5..020d8ba 100644 --- a/core/console/Helper.php +++ b/core/console/Helper.php @@ -13,28 +13,32 @@ private function logAllCommands($commands): void { console_log(self::purpleText("Candle - Elemental CLI")); - console_log(self::yellowText("\nCommand \t\t\t\t Usage")); - console_log("======= \t\t\t\t ====="); + console_log(self::yellowText("\nCommand \t\t\t\t\t Usage")); + console_log("======= \t\t\t\t\t ====="); - console_log(self::greenText("php candle ignite") . " \t\t\t Start the PHP server at default address & available port."); - console_log(self::greenText("php candle ignite --host=[]") . " \t\t Start the PHP server at specified host address."); - console_log(self::greenText("php candle ignite --port-[]") . " \t\t Start the PHP server at specified port."); - console_log(self::greenText("php candle ignite --host=[] --port-[]") . " \t Start the PHP server at specified host address & port.\n"); + console_log(self::greenText("php candle ignite") . " \t\t\t\t Start the PHP server at default address & available port."); + console_log(self::greenText("php candle ignite --host=[]") . " \t\t\t Start the PHP server at specified host address."); + console_log(self::greenText("php candle ignite --port-[]") . " \t\t\t Start the PHP server at specified port."); + console_log(self::greenText("php candle ignite --host=[] --port-[]") . " \t\t Start the PHP server at specified host address & port.\n"); - console_log(self::greenText("php candle build:model [name] -f") . "\t Build a Model class with the specified name."); - console_log(self::greenText("php candle build:controller [name] --model=[model] --api -f") . "\n\t\t\t\t\t Build a Controller class with the specified name."); - console_log(self::greenText("php candle build:middleware [name] -f") . "\t Build a Middleware class with the specified name."); - console_log(self::greenText("php candle build:command [name] -f") . "\t Build a Command class with the specified name."); - console_log(self::blueText("\t\t\t\t\t Use ") . Helper::yellowText("-f") . Helper::blueText(" for build commands to overwrite the file when it already exists.\n")); + console_log(self::greenText("php candle build:model [name] -c --api -f") . "\t Build a Model class with the specified name."); + console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("-c") . Helper::blueText(" to create a controller for the model.")); + console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--api") . Helper::blueText(" to create an API controller.")); + console_log(self::greenText("php candle build:controller [name] --model=[model] --api -f") . "\n\t\t\t\t\t\t Build a Controller class with the specified name."); + console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--model=[model]") . Helper::blueText(" to set the model name for the controller.")); + console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--api") . Helper::blueText(" to create an API controller.")); + console_log(self::greenText("php candle build:middleware [name] -f") . "\t\t Build a Middleware class with the specified name."); + console_log(self::greenText("php candle build:command [name] -f") . "\t\t Build a Command class with the specified name."); + console_log(self::blueText("\n\t\t\t\t\t\t Use ") . Helper::yellowText("-f") . Helper::blueText(" for build commands to overwrite the file when it already exists.\n")); - console_log(self::greenText("php candle route:list") . " \t\t\t List all the routes registered within the App."); - console_log(self::greenText("php candle help") . " \t\t\t List all the available commands.\n"); + console_log(self::greenText("php candle route:list") . " \t\t\t\t List all the routes registered within the App."); + console_log(self::greenText("php candle help") . " \t\t\t\t List all the available commands.\n"); console_log(self::yellowText("Custom commands")); console_log("==============="); foreach ($commands as $key => $command) { - console_log(self::greenText("php candle $key") . " \t\t\t " . $command->getDescription()); + console_log(self::greenText("php candle $key") . " \t\t\t\t " . $command->getDescription()); } } From 1ececb47c3c7e7d247e61cbfd380655d95af3e79 Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 08:12:26 +0530 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=90=9B=20Specify=20Model=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/console/Builder.php b/core/console/Builder.php index 7cfcec3..805f3a0 100644 --- a/core/console/Builder.php +++ b/core/console/Builder.php @@ -97,7 +97,7 @@ public function generateModel() $this->createFile($dir, $this->name, $content); if ($this->modelConfig['controller'] || $this->modelConfig['api']) { - $args = []; + $args = ["--model=" . $this->name]; if ($this->modelConfig['api']) { $args[] = "--api"; From d3e71b927c4f74fc63f21e0bc73e5ee016f22224 Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 08:13:53 +0530 Subject: [PATCH 6/7] =?UTF-8?q?=E2=9C=A8=20Candle:=20list=20alias=20for=20?= =?UTF-8?q?help?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Commander.php | 4 ++++ core/console/Helper.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/core/console/Commander.php b/core/console/Commander.php index c02de40..91bf2dc 100644 --- a/core/console/Commander.php +++ b/core/console/Commander.php @@ -42,6 +42,10 @@ public function resolveCommand() return new Helper($this->commands); } + if ($command === "list") { + return new Helper($this->commands); + } + if (in_array($command, Builder::BUILD_COMMANDS)) { return new Builder($command, $this->args); } diff --git a/core/console/Helper.php b/core/console/Helper.php index 020d8ba..4900128 100644 --- a/core/console/Helper.php +++ b/core/console/Helper.php @@ -25,7 +25,7 @@ private function logAllCommands($commands): void console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("-c") . Helper::blueText(" to create a controller for the model.")); console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--api") . Helper::blueText(" to create an API controller.")); console_log(self::greenText("php candle build:controller [name] --model=[model] --api -f") . "\n\t\t\t\t\t\t Build a Controller class with the specified name."); - console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--model=[model]") . Helper::blueText(" to set the model name for the controller.")); + console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--model=[model]") . Helper::blueText(" to set the model for the controller.")); console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("--api") . Helper::blueText(" to create an API controller.")); console_log(self::greenText("php candle build:middleware [name] -f") . "\t\t Build a Middleware class with the specified name."); console_log(self::greenText("php candle build:command [name] -f") . "\t\t Build a Command class with the specified name."); From ff02a13d5a5c13cd14be9b24bb7ae52eaeece86e Mon Sep 17 00:00:00 2001 From: Afaan Bilal Date: Sat, 20 Jan 2024 08:17:49 +0530 Subject: [PATCH 7/7] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Command=20list=20-=20f?= =?UTF-8?q?ix=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/console/Helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/console/Helper.php b/core/console/Helper.php index 4900128..0bc9fff 100644 --- a/core/console/Helper.php +++ b/core/console/Helper.php @@ -18,8 +18,8 @@ private function logAllCommands($commands): void console_log(self::greenText("php candle ignite") . " \t\t\t\t Start the PHP server at default address & available port."); console_log(self::greenText("php candle ignite --host=[]") . " \t\t\t Start the PHP server at specified host address."); - console_log(self::greenText("php candle ignite --port-[]") . " \t\t\t Start the PHP server at specified port."); - console_log(self::greenText("php candle ignite --host=[] --port-[]") . " \t\t Start the PHP server at specified host address & port.\n"); + console_log(self::greenText("php candle ignite --port=[]") . " \t\t\t Start the PHP server at specified port."); + console_log(self::greenText("php candle ignite --host=[] --port=[]") . " \t\t Start the PHP server at specified host address & port.\n"); console_log(self::greenText("php candle build:model [name] -c --api -f") . "\t Build a Model class with the specified name."); console_log(self::blueText("\t\t\t\t\t\t Use ") . Helper::yellowText("-c") . Helper::blueText(" to create a controller for the model."));