1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Console\Command\Package; |
4
|
|
|
|
5
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Install; |
6
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\ListPackages; |
7
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Uninstall; |
8
|
|
|
use Buttress\Concrete\Console\Command\PackageCommand; |
9
|
|
|
use Buttress\Concrete\Locator\Site; |
10
|
|
|
use League\CLImate\CLImate; |
11
|
|
|
use League\Tactician\CommandBus; |
12
|
|
|
|
13
|
|
|
class InstallController |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** @var \Buttress\Concrete\Console\Command\PackageCommand */ |
17
|
|
|
private $command; |
18
|
|
|
|
19
|
|
|
/** @var \League\Tactician\CommandBus */ |
20
|
|
|
private $bus; |
21
|
|
|
|
22
|
|
|
/** @var \League\CLImate\CLImate */ |
23
|
|
|
private $cli; |
24
|
|
|
|
25
|
|
|
public function __construct(PackageCommand $command, CommandBus $bus, CLImate $cli) |
26
|
|
|
{ |
27
|
|
|
$this->command = $command; |
28
|
|
|
$this->bus = $bus; |
29
|
|
|
$this->cli = $cli; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handle package:list |
34
|
|
|
* |
35
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
36
|
|
|
*/ |
37
|
|
|
public function listPackages(Site $site) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this->bus->handle((new ListPackages())->setCli($this->cli)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handle package:install |
44
|
|
|
* |
45
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function install(Site $site) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$definition = $this->getDefinition($site, 'package:install'); |
50
|
|
|
$definition->parse(); |
51
|
|
|
$handle = $definition->get('handle'); |
52
|
|
|
|
53
|
|
|
if (!$handle) { |
54
|
|
|
$packages = iterator_to_array($this->getAvailablePackages($site)); |
55
|
|
|
if (!$packages) { |
|
|
|
|
56
|
|
|
$this->cli->error('No packages available to install'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$handle = $this->confirmHandle($packages); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->cli->info('Installing ' . $handle); |
63
|
|
|
$this->bus->handle((new Install())->setHandle($handle)); |
64
|
|
|
$this->cli->green('Installed Successfully!'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Handle package:uninstall |
69
|
|
|
* |
70
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function uninstall(Site $site) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$definition = $this->getDefinition($site, 'package:uninstall'); |
75
|
|
|
$definition->parse(); |
76
|
|
|
$handle = $definition->get('handle'); |
77
|
|
|
|
78
|
|
|
if (!$handle) { |
79
|
|
|
$packages = iterator_to_array($this->getAvailablePackages($site)); |
80
|
|
|
if (!$packages) { |
|
|
|
|
81
|
|
|
$this->cli->error('No packages available to uninstall'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$handle = $this->confirmHandle($packages); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->cli->info('Uninstalling ' . $handle); |
88
|
|
|
$this->bus->handle((new Uninstall())->setHandle($handle)); |
89
|
|
|
$this->cli->green('Uninstalled Successfully!'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getDefinition(Site $site, $command) |
93
|
|
|
{ |
94
|
|
|
/** @var \Buttress\Concrete\Console\Command\Manager\CommandManager[] $definitions */ |
95
|
|
|
$definitions = $this->command->getCommands($site); |
96
|
|
|
foreach ($definitions as $definition) { |
97
|
|
|
if ($definition->getCommand() === $command) { |
98
|
|
|
return $definition; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function getAvailablePackages(Site $site) |
104
|
|
|
{ |
105
|
|
|
$iterator = new \DirectoryIterator($site->getPath() . '/packages'); |
106
|
|
|
|
107
|
|
|
foreach ($iterator as $item) { |
108
|
|
|
if (!$item->isDot() && $item->isDir()) { |
109
|
|
|
yield $item->getBasename(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function confirmHandle($packages) |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
$green = $this->cli->green(); |
118
|
|
|
|
119
|
|
|
if (count($packages) < 10) { |
120
|
|
|
$green->out($packages); |
121
|
|
|
} else { |
122
|
|
|
$green->columns($packages); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$input = $this->cli->input('Which package?'); |
126
|
|
|
$input->accept($packages); |
127
|
|
|
$input->strict(); |
128
|
|
|
|
129
|
|
|
return $input->prompt(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.