1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle
;
14 use Rapsys\PackBundle\RapsysPackBundle
;
16 use Symfony\Component\Console\Command\Command
as BaseCommand
;
17 use Symfony\Component\DependencyInjection\Container
;
22 class Command
extends BaseCommand
{
26 protected string $alias = '';
31 protected string $bundle = '';
36 public function __construct(protected ?string $name = null) {
38 $class = strrchr(static::class, '\\', true);
40 //Without command name
41 if (substr(static::class, -strlen('\\Command')) !== '\\Command') {
42 $class = strrchr($class, '\\', true);
46 $this->bundle
= strtolower($class);
48 //With full class name
49 if (class_exists($class .= '\\'.str_replace('\\', '', $class)) && method_exists($class, 'getAlias')) {
51 $this->alias
= call_user_func([$class, 'getAlias']);
55 $this->name
= $this->name
?? static::getName($this->alias
);
57 //Call parent constructor
58 parent
::__construct($this->name
);
61 if (!empty($this->description
)) {
63 $this->setDescription($this->description
);
67 if (!empty($this->help
)) {
69 $this->setHelp($this->help
);
74 * Return the command name
78 * @param ?string $alias The bundle alias
80 public function getName(?string $alias = null): string {
82 if ($npos = strrpos(static::class, '\\')) {
91 $bpos = strlen(static::class) - $npos;
93 //With trailing command
94 if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') {
96 $bpos -= strlen('Command');
100 if ($alias === null) {
102 $class = strrchr(static::class, '\\', true);
104 //Without command name
105 if (substr(static::class, -strlen('\\Command')) !== '\\Command') {
106 $class = strrchr($class, '\\', true);
109 //With full class name
110 if (class_exists($class .= '\\'.str_replace('\\', '', $class)) && method_exists($class, 'getAlias')) {
112 $alias = call_user_func([$class, 'getAlias']);
116 //Return command alias
117 return ($alias?$alias.':':'').strtolower(substr(static::class, $npos, $bpos));