1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\CommandBus\Handler\Modern; |
4
|
|
|
|
5
|
|
|
use Buttress\Concrete\Adapter\ModernAdapter; |
6
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Install; |
7
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\ListPackages; |
8
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Uninstall; |
9
|
|
|
use Buttress\Concrete\Exception\RuntimeException; |
10
|
|
|
use Buttress\Concrete\Locator\Site; |
11
|
|
|
use Concrete\Core\Package\PackageService; |
12
|
|
|
use League\CLImate\CLImate; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
class PackageHandler |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $site; |
19
|
|
|
|
20
|
|
|
protected $adapter; |
21
|
|
|
|
22
|
|
|
protected $logger; |
23
|
|
|
|
24
|
|
|
protected $cli; |
25
|
|
|
|
26
|
|
|
public function __construct(Site $site, ModernAdapter $adapter, LoggerInterface $logger, CLImate $cli) |
27
|
|
|
{ |
28
|
|
|
$this->site = $site; |
29
|
|
|
$this->adapter = $adapter; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
$this->cli = $cli; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function handleListPackages(ListPackages $command) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$service = $this->getService(); |
37
|
|
|
$installed = $this->names($service->getInstalledList()); |
38
|
|
|
$notInstalled = $this->names($service->getAvailablePackages()); |
39
|
|
|
|
40
|
|
|
$this->cli->flank('<green>Installed Packages</green>'); |
41
|
|
|
$this->outputList($installed); |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->cli->br()->flank('Available Packages'); |
45
|
|
|
$this->outputList($notInstalled); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function outputList(array $list) |
49
|
|
|
{ |
50
|
|
|
if (count($list) > 8) { |
51
|
|
|
$this->cli->columns($list); |
52
|
|
|
} else { |
53
|
|
|
$this->cli->out($list); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function names(array $packages) |
58
|
|
|
{ |
59
|
|
|
return (array) array_map(function($package) { |
60
|
|
|
return $package->getPackageHandle(); |
61
|
|
|
}, $packages); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function handleInstall(Install $command) |
65
|
|
|
{ |
66
|
|
|
$service = $this->getService(); |
67
|
|
|
$handle = $command->getHandle(); |
68
|
|
|
|
69
|
|
|
if ($package = $this->getUninstalledPackage($service, $handle)) { |
70
|
|
|
$service->install($package, []); |
71
|
|
|
} else { |
72
|
|
|
throw new RuntimeException('Invalid package handle "' . $handle . '"'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function handleUninstall(Uninstall $command) |
77
|
|
|
{ |
78
|
|
|
$service = $this->getService(); |
79
|
|
|
$handle = $command->getHandle(); |
80
|
|
|
if ($package = $this->getInstalledPackage($service, $handle)) { |
81
|
|
|
$service->uninstall($package); |
82
|
|
|
} else { |
83
|
|
|
throw new RuntimeException('Invalid package handle "' . $handle . '"'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getUninstalledPackage(PackageService $service, $handle) |
88
|
|
|
{ |
89
|
|
|
$packages = $service->getAvailablePackages(); |
90
|
|
|
foreach ($packages as $package) { |
91
|
|
|
if ($package->getPackageHandle() === $handle) { |
92
|
|
|
return $package; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getInstalledPackage(PackageService $service, $handle) |
98
|
|
|
{ |
99
|
|
|
$packages = $service->getAvailablePackages(false); |
100
|
|
|
foreach ($packages as $package) { |
101
|
|
|
if ($package->getPackageHandle() === $handle) { |
102
|
|
|
return $package; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return PackageService |
109
|
|
|
*/ |
110
|
|
|
private function getService() |
111
|
|
|
{ |
112
|
|
|
$this->adapter->attach(); |
113
|
|
|
$application = $this->adapter->getApplication(); |
114
|
|
|
$application->bind(LoggerInterface::class, $this->logger); |
115
|
|
|
|
116
|
|
|
/** @var PackageService $service */ |
117
|
|
|
return $application->make(PackageService::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.