From 6c7d652391a89e19ba83cea19abaaf33848b5163 Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Wed, 26 Jun 2024 22:48:34 +0100 Subject: [PATCH 1/7] Add description about console folder --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index cec6a19..c07c385 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ All modules can communicate with the other modules using their API endpoints. * **html_admin** - A folder holding front-end templates (`*.html.twig files`) for the administrator panel. * **html_client** - A folder holding front-end templates (`*.html.twig files`) for the client / guest area. +### Console CLI folder (Optional) + +* **ClassConsole.php** - A file where you can run CLI console by ```php console.php your:cli``` +* Multiple files can be added to the folder as long as they have different names and do not conflict with existing ones. + ### Controller folder * **Admin.php** - Defines the module's routes and navigation items for the administrator panel. From 5b644c670d58039b883d810352dac3e3607e9ee5 Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Wed, 26 Jun 2024 22:51:31 +0100 Subject: [PATCH 2/7] Add console example for info message --- src/Console/ExampleInfo.php | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/Console/ExampleInfo.php diff --git a/src/Console/ExampleInfo.php b/src/Console/ExampleInfo.php new file mode 100644 index 0000000..ab62a5f --- /dev/null +++ b/src/Console/ExampleInfo.php @@ -0,0 +1,61 @@ +di = $di; + } + + /** + * @return Container|null + */ + public function getDi(): ?Container + { + return $this->di; + } + + /** + * @return void + */ + protected function configure(): void + { + $this->setName('example:info'); + $this->setDescription('Return an info message'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->info("This is your example info"); + return Command::SUCCESS; + } +} \ No newline at end of file From 21a349d451c20f73cf37fcf16de16c02af34782f Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Wed, 26 Jun 2024 22:51:53 +0100 Subject: [PATCH 3/7] Add console example for error message --- src/Console/ExampleError.php | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Console/ExampleError.php diff --git a/src/Console/ExampleError.php b/src/Console/ExampleError.php new file mode 100644 index 0000000..edd2ef1 --- /dev/null +++ b/src/Console/ExampleError.php @@ -0,0 +1,66 @@ +di = $di; + } + + /** + * @return Container|null + */ + public function getDi(): ?Container + { + return $this->di; + } + + /** + * @return void + */ + protected function configure(): void + { + $this->setName('example:error'); + $this->setDescription('Return an error message'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + try { + $this->error("This is your example error command"); + } catch (\Exception $e) { + $this->error("This is your example error command triggered by an exception"); + return Command::FAILURE; + } + return Command::SUCCESS; + } +} \ No newline at end of file From 844d90b99d23e168891d23e5c949e45a40fd38cf Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Wed, 26 Jun 2024 22:53:18 +0100 Subject: [PATCH 4/7] Add an example for DI --- src/Console/ExampleDI.php | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Console/ExampleDI.php diff --git a/src/Console/ExampleDI.php b/src/Console/ExampleDI.php new file mode 100644 index 0000000..611dc78 --- /dev/null +++ b/src/Console/ExampleDI.php @@ -0,0 +1,74 @@ +di = $di; + } + + /** + * @return Container|null + */ + public function getDi(): ?Container + { + return $this->di; + } + + /** + * @return void + */ + protected function configure(): void + { + $this->setName('example:clear'); + $this->setDescription('Clear your cache in example'); + parent::configure(); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $service = $this->di['mod_service']('system'); + try { + $service->clearCache(); + } catch (\Exception $e) { + $this->error("An error occurred: ".$e->getMessage()); + return Command::FAILURE; + } finally { + $this->info("Successfully cleared the cache."); + return Command::SUCCESS; + } + } +} From a3ef4b23384f8cbd41b7c19d61756655848e02f7 Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Sun, 30 Jun 2024 17:52:11 +0100 Subject: [PATCH 5/7] Update example where cristiang wrapper is not used and move to Symfony Command --- src/Console/ExampleDI.php | 6 +++--- src/Console/ExampleError.php | 6 +++--- src/Console/ExampleInfo.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Console/ExampleDI.php b/src/Console/ExampleDI.php index 611dc78..f2b5e0a 100644 --- a/src/Console/ExampleDI.php +++ b/src/Console/ExampleDI.php @@ -17,7 +17,7 @@ namespace Box\Mod\Example\Console; use Pimple\Container; -use CristianG\PimpleConsole\Command; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -64,10 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { $service->clearCache(); } catch (\Exception $e) { - $this->error("An error occurred: ".$e->getMessage()); + $output->writeln('An error occurred: ' . $e->getMessage() . ''); return Command::FAILURE; } finally { - $this->info("Successfully cleared the cache."); + $output->writeln('Successfully cleared the cache.'); return Command::SUCCESS; } } diff --git a/src/Console/ExampleError.php b/src/Console/ExampleError.php index edd2ef1..432a311 100644 --- a/src/Console/ExampleError.php +++ b/src/Console/ExampleError.php @@ -18,7 +18,7 @@ use Pimple\Container; -use CristianG\PimpleConsole\Command; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -56,9 +56,9 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->error("This is your example error command"); + $output->writeln('This is your example error console'); } catch (\Exception $e) { - $this->error("This is your example error command triggered by an exception"); + $output->writeln('This is your example error console triggered by an exception'); return Command::FAILURE; } return Command::SUCCESS; diff --git a/src/Console/ExampleInfo.php b/src/Console/ExampleInfo.php index ab62a5f..4fb7153 100644 --- a/src/Console/ExampleInfo.php +++ b/src/Console/ExampleInfo.php @@ -18,7 +18,7 @@ use Pimple\Container; -use CristianG\PimpleConsole\Command; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -55,7 +55,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $this->info("This is your example info"); + $output->writeln('This is your example info.'); return Command::SUCCESS; } } \ No newline at end of file From bf3f78647b256428c30134142b3466ddd8f6c499 Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Wed, 3 Jul 2024 11:01:08 +0100 Subject: [PATCH 6/7] Change console folder to commands and update examples with Attribute AsCommand --- README.md | 2 +- src/{Console => Commands}/ExampleDI.php | 17 ++++++----------- src/{Console => Commands}/ExampleError.php | 19 +++++++------------ src/{Console => Commands}/ExampleInfo.php | 17 ++++++----------- 4 files changed, 20 insertions(+), 35 deletions(-) rename src/{Console => Commands}/ExampleDI.php (87%) rename src/{Console => Commands}/ExampleError.php (80%) rename src/{Console => Commands}/ExampleInfo.php (84%) diff --git a/README.md b/README.md index c07c385..038bb02 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ All modules can communicate with the other modules using their API endpoints. * **html_admin** - A folder holding front-end templates (`*.html.twig files`) for the administrator panel. * **html_client** - A folder holding front-end templates (`*.html.twig files`) for the client / guest area. -### Console CLI folder (Optional) +### Commands CLI folder (Optional) * **ClassConsole.php** - A file where you can run CLI console by ```php console.php your:cli``` * Multiple files can be added to the folder as long as they have different names and do not conflict with existing ones. diff --git a/src/Console/ExampleDI.php b/src/Commands/ExampleDI.php similarity index 87% rename from src/Console/ExampleDI.php rename to src/Commands/ExampleDI.php index f2b5e0a..cbaf629 100644 --- a/src/Console/ExampleDI.php +++ b/src/Commands/ExampleDI.php @@ -17,11 +17,16 @@ namespace Box\Mod\Example\Console; use Pimple\Container; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; - +#[AsCommand( + name: 'example:clear', + description: 'Clear your cache for example', + hidden: false +)] class ExampleDI extends Command implements \FOSSBilling\InjectionAwareInterface { protected ?Container $di = null; @@ -43,16 +48,6 @@ public function getDi(): ?Container return $this->di; } - /** - * @return void - */ - protected function configure(): void - { - $this->setName('example:clear'); - $this->setDescription('Clear your cache in example'); - parent::configure(); - } - /** * @param InputInterface $input * @param OutputInterface $output diff --git a/src/Console/ExampleError.php b/src/Commands/ExampleError.php similarity index 80% rename from src/Console/ExampleError.php rename to src/Commands/ExampleError.php index 432a311..5f9ac80 100644 --- a/src/Console/ExampleError.php +++ b/src/Commands/ExampleError.php @@ -17,11 +17,16 @@ namespace Box\Mod\Example\Console; use Pimple\Container; - +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +#[AsCommand( + name: 'example:error', + description: 'Return an error message', + hidden: false +)] class ExampleError extends Command implements \FOSSBilling\InjectionAwareInterface { protected ?Container $di = null; @@ -43,22 +48,12 @@ public function getDi(): ?Container return $this->di; } - /** - * @return void - */ - protected function configure(): void - { - $this->setName('example:error'); - $this->setDescription('Return an error message'); - parent::configure(); - } - protected function execute(InputInterface $input, OutputInterface $output): int { try { $output->writeln('This is your example error console'); } catch (\Exception $e) { - $output->writeln('This is your example error console triggered by an exception'); + $output->writeln('This is your example error console triggered by an exception'); return Command::FAILURE; } return Command::SUCCESS; diff --git a/src/Console/ExampleInfo.php b/src/Commands/ExampleInfo.php similarity index 84% rename from src/Console/ExampleInfo.php rename to src/Commands/ExampleInfo.php index 4fb7153..6715749 100644 --- a/src/Console/ExampleInfo.php +++ b/src/Commands/ExampleInfo.php @@ -17,11 +17,16 @@ namespace Box\Mod\Example\Commands; use Pimple\Container; - +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +#[AsCommand( + name: 'example:info', + description: 'Return an info message', + hidden: false +)] class ExampleInfo extends Command implements \FOSSBilling\InjectionAwareInterface { protected ?Container $di = null; @@ -43,16 +48,6 @@ public function getDi(): ?Container return $this->di; } - /** - * @return void - */ - protected function configure(): void - { - $this->setName('example:info'); - $this->setDescription('Return an info message'); - parent::configure(); - } - protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('This is your example info.'); From f7995fd2c0d02843f9caabf28ab994feba94ed68 Mon Sep 17 00:00:00 2001 From: ChristianGabs Date: Thu, 11 Jul 2024 04:19:58 +0100 Subject: [PATCH 7/7] Update namespace to Commands for example di and error --- src/Commands/ExampleDI.php | 2 +- src/Commands/ExampleError.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/ExampleDI.php b/src/Commands/ExampleDI.php index cbaf629..cbd93ea 100644 --- a/src/Commands/ExampleDI.php +++ b/src/Commands/ExampleDI.php @@ -14,7 +14,7 @@ * with this source code in the file LICENSE */ -namespace Box\Mod\Example\Console; +namespace Box\Mod\Example\Commands; use Pimple\Container; use Symfony\Component\Console\Attribute\AsCommand; diff --git a/src/Commands/ExampleError.php b/src/Commands/ExampleError.php index 5f9ac80..2ed7122 100644 --- a/src/Commands/ExampleError.php +++ b/src/Commands/ExampleError.php @@ -14,7 +14,7 @@ * with this source code in the file LICENSE */ -namespace Box\Mod\Example\Console; +namespace Box\Mod\Example\Commands; use Pimple\Container; use Symfony\Component\Console\Attribute\AsCommand;