|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Service\Package\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Buttress\Concrete\Client\Connection\Connection; |
|
6
|
|
|
use Buttress\Concrete\Exception\RuntimeException; |
|
7
|
|
|
use Buttress\Concrete\Service\Package\PackageItem; |
|
8
|
|
|
use Buttress\Concrete\Service\Package\PackageItemFactory; |
|
9
|
|
|
use Buttress\Concrete\Service\Result; |
|
10
|
|
|
use League\CLImate\CLImate; |
|
11
|
|
|
use Loader; |
|
12
|
|
|
use Package; |
|
13
|
|
|
|
|
14
|
|
|
class LegacyDriver implements Driver |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Buttress\Concrete\Client\Connection\Connection */ |
|
18
|
|
|
private $connection; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Buttress\Concrete\Service\Package\PackageItemFactory */ |
|
21
|
|
|
private $factory; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Connection $connection, PackageItemFactory $factory) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->connection = $connection; |
|
26
|
|
|
$this->factory = $factory; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \Buttress\Concrete\Service\Package\PackageItem $item |
|
31
|
|
|
* @return Package|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private function getPackage(PackageItem $item) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$package = Package::getByHandle($item->getHandle())) { |
|
36
|
|
|
$package = Loader::package($item->getHandle()); |
|
37
|
|
|
} |
|
38
|
|
|
return is_object($package) ? $package : null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Install a package |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Buttress\Concrete\Service\Package\PackageItem $item |
|
45
|
|
|
* @return \Buttress\Concrete\Service\Result |
|
46
|
|
|
*/ |
|
47
|
|
|
public function install(PackageItem $item) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->connection->isConnected()) { |
|
50
|
|
|
return new Result(false, 'Not connected to concrete5 site.'); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!$package = $this->getPackage($item)) { |
|
54
|
|
|
return new Result(false, 'Invalid package handle.'); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Fill the item with real data |
|
58
|
|
|
$item = $this->factory->fromLegacy($package); |
|
59
|
|
|
|
|
60
|
|
|
if ($item->isInstalled()) { |
|
61
|
|
|
return new Result(false, sprintf('<underline><bold>%s</bold></underline> is already installed.', $package->getPackageName())); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($package->showInstallOptionsScreen()) { |
|
65
|
|
|
return new Result(false, 'Install options are not currently supported. Please install through the dashboard.'); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$tests = $this->test($item); |
|
69
|
|
|
if (!$tests->success()) { |
|
70
|
|
|
return $tests; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
|
|
$package->install([]); |
|
75
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
76
|
|
|
return new Result(false, [$e->getMessage()]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new Result(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Uninstall a package |
|
84
|
|
|
* |
|
85
|
|
|
* @param PackageItem $item |
|
86
|
|
|
* @return \Buttress\Concrete\Service\Result |
|
87
|
|
|
*/ |
|
88
|
|
|
public function uninstall(PackageItem $item) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!$this->connection->isConnected()) { |
|
91
|
|
|
return new Result(false, 'Not connected to concrete5 site.'); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!$package = $this->getPackage($item)) { |
|
95
|
|
|
return new Result(false, 'Invalid package handle.'); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Fill the item with real data |
|
99
|
|
|
$item = $this->factory->fromLegacy($package); |
|
100
|
|
|
|
|
101
|
|
|
if (!$item->isInstalled()) { |
|
102
|
|
|
return new Result(false, sprintf('<underline><bold>%s</bold></underline> hasn\'t been installed yet.', $package->getPackageName())); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$package->uninstall(); |
|
107
|
|
|
} catch (\Exception $e) { |
|
108
|
|
|
return new Result(false, [$e->getMessage()]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return new Result(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Test a package for install |
|
116
|
|
|
* |
|
117
|
|
|
* @param PackageItem $package |
|
118
|
|
|
* @return \Buttress\Concrete\Service\Result |
|
119
|
|
|
*/ |
|
120
|
|
|
public function test(PackageItem $package) |
|
121
|
|
|
{ |
|
122
|
|
|
if (!$this->connection->isConnected()) { |
|
123
|
|
|
return new Result(false, 'Not connected to concrete5 site.'); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$package = $this->getPackage($package); |
|
127
|
|
|
$errors = []; |
|
128
|
|
|
|
|
129
|
|
|
if (is_array($tests = $package->testForInstall($package->getPackageHandle(), false))) { |
|
130
|
|
|
$errors = Package::mapError($tests); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return new Result((bool) !$errors, $errors); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Show information about a package |
|
138
|
|
|
* |
|
139
|
|
|
* @param PackageItem $package |
|
140
|
|
|
* @return \Buttress\Concrete\Service\Result |
|
141
|
|
|
*/ |
|
142
|
|
View Code Duplication |
public function show(PackageItem $package, CLImate $cli) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
if (!$this->connection->isConnected()) { |
|
145
|
|
|
return new Result(false, 'Not connected to concrete5 site.'); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$cli->flank($package->getHandle()); |
|
149
|
|
|
$cli->out($package->isInstalled() ? 'Installed Version ' : 'Not Installed: '); |
|
150
|
|
|
$cli->bold()->inline($package->getVersion()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get a list of package item objects |
|
155
|
|
|
* @return PackageItem[] |
|
156
|
|
|
* @throws \Buttress\Concrete\Exception\RuntimeException |
|
157
|
|
|
*/ |
|
158
|
|
|
public function all() |
|
159
|
|
|
{ |
|
160
|
|
|
if (!$this->connection->isConnected()) { |
|
161
|
|
|
throw new RuntimeException('Not connected to concrete5 site.'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$packages = Package::getAvailablePackages(false); |
|
165
|
|
|
|
|
166
|
|
|
foreach ($packages as $package) { |
|
167
|
|
|
yield $this->factory->fromLegacy($package); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: