1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Console\Command; |
15
|
|
|
|
16
|
|
|
use Valkyrja\Console\Commander\Commander; |
17
|
|
|
use Valkyrja\Console\Constant\ExitCode; |
18
|
|
|
use Valkyrja\Console\Contract\Console; |
19
|
|
|
use Valkyrja\Console\Input\Contract\Input as InputContract; |
20
|
|
|
use Valkyrja\Console\Input\Input; |
21
|
|
|
use Valkyrja\Console\Output\Contract\Output as OutputContract; |
22
|
|
|
use Valkyrja\Console\Output\Output; |
23
|
|
|
use Valkyrja\Console\Support\Provides; |
24
|
|
|
|
25
|
|
|
use function array_keys; |
26
|
|
|
use function implode; |
27
|
|
|
use function strpos; |
28
|
|
|
use function substr; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class CommandsListForBash. |
32
|
|
|
* |
33
|
|
|
* @author Melech Mizrachi |
34
|
|
|
*/ |
35
|
|
|
class CommandsListForBash extends Commander |
36
|
|
|
{ |
37
|
|
|
use Provides; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
public const string COMMAND = 'console:commandsForBash'; |
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
public const string PATH = self::COMMAND . ' valkyrja[ {commandTyped:[a-zA-Z0-9\:]+}]'; |
43
|
|
|
/** @var string */ |
44
|
|
|
public const string SHORT_DESCRIPTION = 'List all the commands for bash auto complete'; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
protected Console $console, |
48
|
|
|
InputContract $input = new Input(), |
49
|
|
|
OutputContract $output = new Output() |
50
|
|
|
) { |
51
|
|
|
parent::__construct($input, $output); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
* |
57
|
|
|
* @param string|null $commandTyped The command typed |
58
|
|
|
*/ |
59
|
|
|
public function run(string|null $commandTyped = null): int |
60
|
|
|
{ |
61
|
|
|
/** @var string[] $allCommands */ |
62
|
|
|
$allCommands = array_keys($this->console->getNamedCommands()); |
63
|
|
|
|
64
|
|
|
if ($commandTyped !== null && $commandTyped !== '') { |
65
|
|
|
$colonAt = strpos($commandTyped, ':'); |
66
|
|
|
$possibleCommands = []; |
67
|
|
|
|
68
|
|
|
foreach ($allCommands as $command) { |
69
|
|
|
// Return command in result if it starts with $commandTyped |
70
|
|
|
if (str_starts_with($command, $commandTyped)) { |
71
|
|
|
// Colons acts as separators in bash, so return only second |
72
|
|
|
// part if colon is in commandTyped. |
73
|
|
|
$possibleCommands[] = $colonAt !== false ? substr($command, $colonAt + 1) : $command; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
// Nothing typed, return all |
78
|
|
|
$possibleCommands = $allCommands; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->output->writeMessage(implode(' ', $possibleCommands)); |
82
|
|
|
|
83
|
|
|
return ExitCode::SUCCESS; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|