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
|
|
|
$command = new ListPackages(); |
40
|
|
|
$this->bus->handle($command); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Handle package:install |
45
|
|
|
* |
46
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function install(Site $site) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$definition = $this->getDefinition($site, 'package:install'); |
51
|
|
|
$definition->parse(); |
52
|
|
|
$handle = $definition->get('handle'); |
53
|
|
|
|
54
|
|
|
if (!$handle) { |
55
|
|
|
$packages = iterator_to_array($this->getAvailablePackages($site)); |
56
|
|
|
if (!$packages) { |
|
|
|
|
57
|
|
|
$this->cli->error('No packages available to install'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$handle = $this->confirmHandle($packages); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->cli->info('Installing ' . $handle); |
64
|
|
|
$this->bus->handle((new Install())->setHandle($handle)); |
65
|
|
|
$this->cli->green('Installed Successfully!'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle package:uninstall |
70
|
|
|
* |
71
|
|
|
* @param \Buttress\Concrete\Locator\Site $site |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function uninstall(Site $site) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$definition = $this->getDefinition($site, 'package:uninstall'); |
76
|
|
|
$definition->parse(); |
77
|
|
|
$handle = $definition->get('handle'); |
78
|
|
|
|
79
|
|
|
if (!$handle) { |
80
|
|
|
$packages = iterator_to_array($this->getAvailablePackages($site)); |
81
|
|
|
if (!$packages) { |
|
|
|
|
82
|
|
|
$this->cli->error('No packages available to install'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$handle = $this->confirmHandle($packages); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->cli->info('Installing ' . $handle); |
89
|
|
|
$this->bus->handle((new Uninstall())->setHandle($handle)); |
90
|
|
|
$this->cli->green('Uninstalled Successfully!'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getDefinition(Site $site, $command) |
94
|
|
|
{ |
95
|
|
|
/** @var \Buttress\Concrete\Console\Command\Manager\CommandManager[] $definitions */ |
96
|
|
|
$definitions = $this->command->getCommands($site); |
97
|
|
|
foreach ($definitions as $definition) { |
98
|
|
|
if ($definition->getCommand() === $command) { |
99
|
|
|
return $definition; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function getAvailablePackages(Site $site) |
105
|
|
|
{ |
106
|
|
|
$iterator = new \DirectoryIterator($site->getPath() . '/packages'); |
107
|
|
|
|
108
|
|
|
foreach ($iterator as $item) { |
109
|
|
|
if (!$item->isDot() && $item->isDir()) { |
110
|
|
|
yield $item->getBasename(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function confirmHandle($packages) |
116
|
|
|
{ |
117
|
|
|
$this->cli->green()->columns($packages)->br(); |
118
|
|
|
|
119
|
|
|
$input = $this->cli->input('Which package?'); |
120
|
|
|
$input->accept($packages); |
121
|
|
|
$input->strict(); |
122
|
|
|
|
123
|
|
|
return $input->prompt(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.