Skip to content

Instantly share code, notes, and snippets.

@ajardin
Created May 6, 2024 21:29
Show Gist options
  • Save ajardin/1193deed7306d7382f4350e3eadf8889 to your computer and use it in GitHub Desktop.
Save ajardin/1193deed7306d7382f4350e3eadf8889 to your computer and use it in GitHub Desktop.
How to improve your Symfony commands
composer create-project symfony/website-skeleton sandbox
cd sandbox
composer install --optimize-autoloader
<?php
declare(strict_types=1);
namespace App\Command;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DefaultCommand extends Command
{
/**
* {@inheritdoc}
*/
protected static $defaultName = 'app:default';
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this->setDescription('Wrapper of the default "list" command');
$this->setHidden(true);
}
# ...
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var Application $application */
$application = $this->getApplication();
$command = $application->find('list');
$arguments = ['namespace' => 'app'];
$listInput = new ArrayInput($arguments);
return $command->run($listInput, $output);
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$application = new Application($kernel);
+ $application->setDefaultCommand(\App\Command\DefaultCommand::getDefaultName());
$application->run($input);
- use Symfony\Bundle\FrameworkBundle\Console\Application;
+ use App\Application;
<?php
declare(strict_types=1);
namespace App;
class Application extends \Symfony\Component\Console\Application
{
/**
* {@inheritdoc}
*
* @param Kernel $kernel
*/
public function __construct(KernelInterface $kernel)
{
parent::__construct($kernel);
if ($defaultName = DefaultCommand::getDefaultName()) {
$this->setDefaultCommand($defaultName);
}
}
}
<?php
declare(strict_types=1);
namespace App;
use App\Command\DefaultCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application as SymfonyApplication;
use Symfony\Component\HttpKernel\KernelInterface;
class Application extends SymfonyApplication
{
public const CONSOLE_LOGO = <<<'ASCII'
___ _ _ _
/ __| ___ _ __ ___| |_| |_ (_)_ _ __ _
\__ \/ _ \ ' \/ -_) _| ' \| | ' \/ _` |
|___/\___/_|_|_\___|\__|_||_|_|_||_\__, |
|___/
ASCII;
public const CONSOLE_NAME = 'Something';
/**
* {@inheritdoc}
*
* @param Kernel $kernel
*/
public function __construct(KernelInterface $kernel)
{
parent::__construct($kernel);
if ($defaultName = DefaultCommand::getDefaultName()) {
$this->setDefaultCommand($defaultName);
}
}
/**
* {@inheritdoc}
*/
public function getHelp(): string
{
return self::CONSOLE_LOGO.parent::getHelp();
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return self::CONSOLE_NAME;
}
/**
* {@inheritdoc}
*/
public function getVersion(): string
{
return 'X.Y.Z';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment