]> Raphaël G. Git Repositories - packbundle/blob - Command.php
Drop output, path and token parameters
[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 * Alias string
25 */
26 protected string $alias = '';
27
28 /**
29 * Bundle string
30 */
31 protected string $bundle = '';
32
33 /**
34 * {@inheritdoc}
35 */
36 public function __construct(protected ?string $name = null) {
37 //Get class
38 $class = strrchr(static::class, '\\', true);
39
40 //Without command name
41 if (substr(static::class, -strlen('\\Command')) !== '\\Command') {
42 $class = strrchr($class, '\\', true);
43 }
44
45 //Set bundle
46 $this->bundle = strtolower($class);
47
48 //With full class name
49 if (class_exists($class .= '\\'.str_replace('\\', '', $class)) && method_exists($class, 'getAlias')) {
50 //Set alias
51 $this->alias = call_user_func([$class, 'getAlias']);
52 }
53
54 //Fix name
55 $this->name = $this->name ?? static::getName($this->alias);
56
57 //Call parent constructor
58 parent::__construct($this->name);
59
60 //With description
61 if (!empty($this->description)) {
62 //Set description
63 $this->setDescription($this->description);
64 }
65
66 //With help
67 if (!empty($this->help)) {
68 //Set help
69 $this->setHelp($this->help);
70 }
71 }
72
73 /**
74 * Return the command name
75 *
76 * {@inheritdoc}
77 *
78 * @param ?string $alias The bundle alias
79 */
80 public function getName(?string $alias = null): string {
81 //With namespace
82 if ($npos = strrpos(static::class, '\\')) {
83 //Set name pos
84 $npos++;
85 //Without namespace
86 } else {
87 $npos = 0;
88 }
89
90 //Set bundle pos
91 $bpos = strlen(static::class) - $npos;
92
93 //With trailing command
94 if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') {
95 //Fix bundle pos
96 $bpos -= strlen('Command');
97 }
98
99 //Without alias
100 if ($alias === null) {
101 //Get class
102 $class = strrchr(static::class, '\\', true);
103
104 //Without command name
105 if (substr(static::class, -strlen('\\Command')) !== '\\Command') {
106 $class = strrchr($class, '\\', true);
107 }
108
109 //With full class name
110 if (class_exists($class .= '\\'.str_replace('\\', '', $class)) && method_exists($class, 'getAlias')) {
111 //Set alias
112 $alias = call_user_func([$class, 'getAlias']);
113 }
114 }
115
116 //Return command alias
117 return ($alias?$alias.':':'').strtolower(substr(static::class, $npos, $bpos));
118 }
119 }