This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
/
PackageManager.php
226 lines (185 loc) · 6.71 KB
/
PackageManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Pagekit\Installer\Package;
use Pagekit\Application as App;
use Pagekit\Installer\Helper\Composer;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
class PackageManager
{
/**
* @var OutputInterface
*/
protected $output;
/**
* @var Composer
*/
protected $composer;
/**
* Constructor.
*
* @param mixed $output
*/
public function __construct($output = null)
{
$this->output = $output ?: new StreamOutput(fopen('php://output', 'w'));
$config = [];
foreach (['path.temp', 'path.cache', 'path.vendor', 'path.artifact', 'path.packages', 'system.api'] as $key) {
$config[$key] = App::get($key);
}
$this->composer = new Composer($config, $output);
}
/**
* @param array $install
* @param bool $packagist
* @param bool $preferSource
* @return bool
*/
public function install(array $install = [], $packagist = false, $preferSource = false)
{
$previousPackageConfigs = App::package()->all(null, true);
$this->composer->install($install, $packagist, $preferSource);
$packages = App::package()->all(null, true);
foreach ($install as $name => $version) {
$moduleAlreadyExisted = isset($previousPackageConfigs[$name]) && App::module($previousPackageConfigs[$name]->get('module'));
if ($moduleAlreadyExisted == true) {
$previousPackageConfig = isset($previousPackageConfigs[$name]) ? $previousPackageConfigs[$name] : null;
$this->enable($packages[$name], $previousPackageConfig);
} elseif (isset($packages[$name])) {
$this->doInstall($packages[$name]);
}
}
}
/**
* @param array $uninstall
* @return bool
*/
public function uninstall($uninstall)
{
foreach ((array) $uninstall as $name) {
if (!$package = App::package($name)) {
throw new \RuntimeException(__('Unable to find "%name%".', ['%name%' => $name]));
}
$this->disable($package);
$this->getScripts($package)->uninstall();
App::config('system')->remove('packages.' . $package->get('module'));
if ($this->composer->isInstalled($package->getName())) {
$this->composer->uninstall($package->getName());
} else {
if (!$path = $package->get('path')) {
throw new \RuntimeException(__('Package path is missing.'));
}
$this->output->writeln(__("Removing package folder."));
App::file()->delete($path);
@rmdir(dirname($path));
}
}
}
/**
* @param $packages
* @param $previousPackageConfigs
*/
public function enable($packages, $previousPackageConfigs = [])
{
if (!is_array($packages)) {
$packages = [$packages];
}
if (!is_array($previousPackageConfigs)) {
$previousPackageConfigs = [$previousPackageConfigs];
}
foreach ($packages as $package) {
// Get the old package config if provided. If there is no old config available, then use the new config (usually fist installation).
$previousPackageConfig = $package;
foreach ($previousPackageConfigs as $packageConfig) {
if ($packageConfig->get('name') == $package->get('name')) {
$previousPackageConfig = $packageConfig;
break;
}
}
App::trigger('package.enable', [$package]);
if (!$current = App::config('system')->get('packages.' . $previousPackageConfig->get('module'))) {
$current = $this->doInstall($package);
}
$scripts = $this->getScripts($package, $current);
if ($scripts->hasUpdates()) {
$scripts->update();
}
$version = $this->getVersion($package);
App::config('system')->set('packages.' . $package->get('module'), $version);
$scripts->enable();
if ($package->getType() == 'pagekit-theme') {
App::config('system')->set('site.theme', $package->get('module'));
} elseif ($package->getType() == 'pagekit-extension') {
App::config('system')->push('extensions', $package->get('module'));
}
}
}
/**
* @param $packages
*/
public function disable($packages)
{
if (!is_array($packages)) {
$packages = [$packages];
}
foreach ($packages as $package) {
$this->getScripts($package)->disable();
if ($package->getType() == 'pagekit-extension') {
App::config('system')->pull('extensions', $package->get('module'));
}
}
}
/**
* @param array $package
* @param string $current
* @return PackageScripts
*/
protected function getScripts($package, $current = null)
{
if (!$scripts = $package->get('extra.scripts')) {
return new PackageScripts(null, $current);
}
if (!$path = $package->get('path')) {
throw new \RuntimeException(__('Package path is missing.'));
}
return new PackageScripts($path . '/' . $scripts, $current);
}
/**
* @param $package
* @return string
*/
protected function doInstall($package)
{
$this->getScripts($package)->install();
$version = $this->getVersion($package);
App::config('system')->set('packages.' . $package->get('module'), $version);
return $version;
}
/**
* Tries to obtain package version from 'composer.json' or installation log.
*
* @param $package
* @return string
*/
protected function getVersion($package)
{
if (!$path = $package->get('path')) {
throw new \RuntimeException(__('Package path is missing.'));
}
if (!file_exists($file = $path . '/composer.json')) {
throw new \RuntimeException(__('\'composer.json\' is missing.'));
}
$package = json_decode(file_get_contents($file), true);
if (isset($package['version'])) {
return $package['version'];
}
if (file_exists(App::get('path.packages') . '/composer/installed.json')) {
$installed = json_decode(file_get_contents($file), true);
foreach ($installed as $package) {
if ($package['name'] === $package->getName()) {
return $package['version'];
}
}
}
return '0.0.0';
}
}