From b418c811039529bbcf75e680763a05de053cda7b Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 27 Jul 2022 18:17:56 +0200 Subject: [PATCH] Update console example --- bin/console.php | 26 +++++++++++++ config/container.php | 16 ++++++++ config/defaults.php | 5 +++ docs/console.md | 68 ++++++++++++++++++++++++++++++++++ src/Console/ExampleCommand.php | 25 +++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 bin/console.php create mode 100644 src/Console/ExampleCommand.php diff --git a/bin/console.php b/bin/console.php new file mode 100644 index 000000000..14e4db168 --- /dev/null +++ b/bin/console.php @@ -0,0 +1,26 @@ +getParameterOption(['--env', '-e'], 'dev'); + +if ($env) { + $_ENV['APP_ENV'] = $env; +} + +/** @var ContainerInterface $container */ +$container = (new ContainerFactory())->createInstance(); + +try { + /** @var Application $application */ + $application = $container->get(Application::class); + exit($application->run()); +} catch (Throwable $exception) { + echo $exception->getMessage(); + exit(1); +} diff --git a/config/container.php b/config/container.php index 9ca5a6c19..9edde8488 100644 --- a/config/container.php +++ b/config/container.php @@ -16,6 +16,8 @@ use Slim\Interfaces\RouteParserInterface; use Slim\Middleware\ErrorMiddleware; use Slim\Views\PhpRenderer; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Input\InputOption; use Tuupola\Middleware\HttpBasicAuthentication; return [ @@ -106,6 +108,20 @@ return $errorMiddleware; }, + Application::class => function (ContainerInterface $container) { + $application = new Application(); + + $application->getDefinition()->addOption( + new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev') + ); + + foreach ($container->get('settings')['commands'] as $class) { + $application->add($container->get($class)); + } + + return $application; + }, + PhpRenderer::class => function (ContainerInterface $container) { return new PhpRenderer($container->get('settings')['template']); }, diff --git a/config/defaults.php b/config/defaults.php index 57aa6b95a..57d79a3c7 100644 --- a/config/defaults.php +++ b/config/defaults.php @@ -71,4 +71,9 @@ ], ]; +// Console commands +$settings['commands'] = [ + \App\Console\ExampleCommand::class, +]; + return $settings; diff --git a/docs/console.md b/docs/console.md index a4841bc07..5ba7dbedb 100644 --- a/docs/console.md +++ b/docs/console.md @@ -16,6 +16,74 @@ composer list ## Console Commands +The default console executable is: `bin/console.php` + +The default console command class directory is: `src/Console` + +To start the console and list all available commands, run: + +``` bash +php bin/console.php +``` + +## Creating a console command + +Create a new command class, e.g. `src/Console/ExampleCommand.php` and copy/paste this content: + +```php +setName('example'); + $this->setDescription('A sample command'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(sprintf('Hello, World!')); + + // The error code, 0 on success + return 0; + } +} +``` + +To register a new command you have to open `config/defaults.php` +and add a new array entry to `$settings['commands']`. + +```php +$settings['commands'] = [ + // ... + \App\Console\ExampleCommand::class, +]; +``` + +To start to example command, run: + +``` bash +php bin/console.php example +``` + +The output: + +``` +Hello, World! +``` + +Read more: + +* [Symfony Console Commands](https://symfony.com/doc/current/console.html) * [Slim 4 - Console](https://odan.github.io/2021/06/23/slim-console.html) ## Managing Locks diff --git a/src/Console/ExampleCommand.php b/src/Console/ExampleCommand.php new file mode 100644 index 000000000..f59f9261e --- /dev/null +++ b/src/Console/ExampleCommand.php @@ -0,0 +1,25 @@ +setName('example'); + $this->setDescription('A console command'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln('Hello, World!'); + + return 0; + } +}