|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Buttress\Concrete\Console\Command\Manager\CommandManager; |
|
6
|
|
|
use Buttress\Concrete\Console\Console; |
|
7
|
|
|
use Buttress\Concrete\Locator\Site; |
|
8
|
|
|
use Buttress\Concrete\Route\RouteCollector; |
|
9
|
|
|
use League\CLImate\CLImate; |
|
10
|
|
|
|
|
11
|
|
|
class HelpCommand implements Command |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
protected $console; |
|
15
|
|
|
protected $cli; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Console $console, CLImate $cli) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->console = $console; |
|
20
|
|
|
$this->cli = $cli; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function help(Site $site) |
|
24
|
|
|
{ |
|
25
|
|
|
list($help) = $this->getCommands($site); |
|
26
|
|
|
|
|
27
|
|
|
$help->parse(); |
|
28
|
|
|
$command = $help->get('command'); |
|
29
|
|
|
$cli = $this->cli; |
|
30
|
|
|
|
|
31
|
|
|
if ($command && $command !== 'help') { |
|
32
|
|
|
return $this->outputComamndUsage($site, $command, $cli); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $this->outputDefaultUsage($site, $help, $cli); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function table(Site $site, array $commands) |
|
39
|
|
|
{ |
|
40
|
|
|
foreach ($commands as $consoleCommand) { |
|
41
|
|
|
$available = $consoleCommand->getCommands($site); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($available as $command) { |
|
44
|
|
|
yield [ |
|
45
|
|
|
'<blue>Command</blue>' => $command->getCommand(), |
|
46
|
|
|
'<blue>Usage</blue>' => $command->shortUsage() |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the command definitions this command provides |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Buttress\Concrete\Locator\Site|null $site |
|
56
|
|
|
* @return CommandManager[] A list containing a manager for each command this instance offers |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getCommands(Site $site) |
|
60
|
|
|
{ |
|
61
|
|
|
$help = new CommandManager('help'); |
|
62
|
|
|
$help->add('command', [ |
|
63
|
|
|
'description' => 'The command to get Help for', |
|
64
|
|
|
'castTo' => 'string' |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
return [$help]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Register routes onto the route collector |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Buttress\Concrete\Route\RouteCollector $collector |
|
74
|
|
|
* @param \Buttress\Concrete\Locator\Site|null $site Null is passed when a concrete5 site wasn't located |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function registerRoutes(RouteCollector $collector, Site $site = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$collector->addRoute('help', [$this, 'help']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
|
84
|
|
|
* @param $help |
|
85
|
|
|
* @param $cli |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function outputDefaultUsage(Site $site, $help, $cli) |
|
88
|
|
|
{ |
|
89
|
|
|
$help->usage($this->cli); |
|
90
|
|
|
|
|
91
|
|
|
$cli->rule(); |
|
92
|
|
|
$cli->green('Available Commands:'); |
|
93
|
|
|
|
|
94
|
|
|
$commands = $this->console->collection->all(); |
|
95
|
|
|
$cli->table(iterator_to_array($this->table($site, $commands))); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function outputComamndUsage(Site $site, $name, $cli) |
|
99
|
|
|
{ |
|
100
|
|
|
$commands = $this->console->collection->all(); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($commands as $consoleCommand) { |
|
103
|
|
|
$available = $consoleCommand->getCommands($site); |
|
104
|
|
|
|
|
105
|
|
|
foreach ($available as $command) { |
|
106
|
|
|
if ($command->getCommand() === $name) { |
|
107
|
|
|
$command->usage($cli); |
|
108
|
|
|
return 0; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$cli->error(sprintf('Command "%s" not found.', $name)); |
|
114
|
|
|
return 1; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|