From 687a51abbffbe68e2bb6086d1ebd466afe7e4d1e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Mon, 4 Mar 2024 16:43:09 +0100 Subject: [PATCH] Import Command base class --- Command.php | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Command.php diff --git a/Command.php b/Command.php new file mode 100644 index 0000000..0832e2e --- /dev/null +++ b/Command.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\PackBundle; + +use Symfony\Component\Console\Command\Command as BaseCommand; +use Symfony\Component\DependencyInjection\Container; + +use Rapsys\PackBundle\RapsysPackBundle; + +/** + * {@inheritdoc} + */ +class Command extends BaseCommand { + /** + * {@inheritdoc} + */ + public function __construct(protected ?string $name = null) { + //Fix name + $this->name = $this->name ?? static::getName(); + + //Call parent constructor + parent::__construct($this->name); + + //With description + if (!empty($this->description)) { + //Set description + $this->setDescription($this->description); + } + + //With help + if (!empty($this->help)) { + //Set help + $this->setHelp($this->help); + } + } + + /** + * Return the command name + * + * {@inheritdoc} + */ + public function getName(): string { + //With namespace + if ($npos = strrpos(static::class, '\\')) { + //Set name pos + $npos++; + //Without namespace + } else { + $npos = 0; + } + + //With trailing command + if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') { + //Set bundle pos + $bpos = strlen(static::class) - $npos - strlen('Command'); + //Without bundle + } else { + //Set bundle pos + $bpos = strlen(static::class) - $npos; + } + + //Return command alias + return RapsysPackBundle::getAlias().':'.strtolower(substr(static::class, $npos, $bpos)); + } +} -- 2.41.1