Skip to content

Commit

Permalink
Update console example
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jul 27, 2022
1 parent 0cd57eb commit b418c81
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bin/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use App\Factory\ContainerFactory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

require_once __DIR__ . '/../vendor/autoload.php';

$env = (new ArgvInput())->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);
}
16 changes: 16 additions & 0 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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']);
},
Expand Down
5 changes: 5 additions & 0 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@
],
];

// Console commands
$settings['commands'] = [
\App\Console\ExampleCommand::class,
];

return $settings;
68 changes: 68 additions & 0 deletions docs/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace App\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ExampleCommand extends Command
{
protected function configure(): void
{
parent::configure();

$this->setName('example');
$this->setDescription('A sample command');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln(sprintf('<info>Hello, World!</info>'));

// 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
Expand Down
25 changes: 25 additions & 0 deletions src/Console/ExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ExampleCommand extends Command
{
protected function configure(): void
{
parent::configure();

$this->setName('example');
$this->setDescription('A console command');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('<info>Hello, World!</info>');

return 0;
}
}

0 comments on commit b418c81

Please sign in to comment.