Created
May 6, 2024 21:29
-
-
Save ajardin/1193deed7306d7382f4350e3eadf8889 to your computer and use it in GitHub Desktop.
How to improve your Symfony commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer create-project symfony/website-skeleton sandbox | |
cd sandbox | |
composer install --optimize-autoloader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
# ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* {@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); | |
$application = new Application($kernel); | |
+ $application->setDefaultCommand(\App\Command\DefaultCommand::getDefaultName()); | |
$application->run($input); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- use Symfony\Bundle\FrameworkBundle\Console\Application; | |
+ use App\Application; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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