]> Raphaël G. Git Repositories - packbundle/blob - Command.php
Add configurable separator to slug member function
[packbundle] / Command.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle;
13
14 use Rapsys\PackBundle\RapsysPackBundle;
15
16 use Symfony\Component\Console\Command\Command as BaseCommand;
17 use Symfony\Component\DependencyInjection\Container;
18
19 /**
20 * {@inheritdoc}
21 */
22 class Command extends BaseCommand {
23 /**
24 * {@inheritdoc}
25 */
26 public function __construct(protected ?string $name = null) {
27 //Fix name
28 $this->name = $this->name ?? static::getName();
29
30 //Call parent constructor
31 parent::__construct($this->name);
32
33 //With description
34 if (!empty($this->description)) {
35 //Set description
36 $this->setDescription($this->description);
37 }
38
39 //With help
40 if (!empty($this->help)) {
41 //Set help
42 $this->setHelp($this->help);
43 }
44 }
45
46 /**
47 * {@inheritdoc}
48 *
49 * Return the command name
50 */
51 public function getName(): string {
52 //With namespace
53 if ($npos = strrpos(static::class, '\\')) {
54 //Set name pos
55 $npos++;
56 //Without namespace
57 } else {
58 $npos = 0;
59 }
60
61 //With trailing command
62 if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') {
63 //Set bundle pos
64 $bpos = strlen(static::class) - $npos - strlen('Command');
65 //Without bundle
66 } else {
67 //Set bundle pos
68 $bpos = strlen(static::class) - $npos;
69 }
70
71 //Return command alias
72 return RapsysPackBundle::getAlias().':'.strtolower(substr(static::class, $npos, $bpos));
73 }
74 }