forked from kiy0taka/eccube-plugin-installer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PluginInstaller.php
148 lines (120 loc) · 5.4 KB
/
PluginInstaller.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
<?php
namespace Eccube\Composer;
use Composer\Installer\LibraryInstaller;
use Composer\Json\JsonManipulator;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Eccube\Common\Constant;
use Eccube\Kernel;
use Eccube\Service\PluginService;
class PluginInstaller extends LibraryInstaller
{
public function getInstallPath(PackageInterface $package)
{
$extra = $package->getExtra();
if (!isset($extra['code'])) {
throw new \RuntimeException('`extra.code` not found in '.$package->getName().'/composer.json');
}
return "app/Plugin/".$extra['code'];
}
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
parent::update($repo, $initial, $target);
$this->addPluginIdToComposerJson($target);
}
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!isset($GLOBALS['kernel'])) {
$message = 'You can not install the EC-CUBE plugin via `composer` command.'.PHP_EOL
.'Please use the `bin/console eccube:composer:require '.$package->getName().'` instead.';
throw new \RuntimeException($message);
}
/** @var Kernel $kernel */
$kernel = $GLOBALS['kernel'];
$container = $kernel->getContainer();
$extra = $package->getExtra();
$source = $extra['id'];
$code = $extra['code'];
$version = $package->getPrettyVersion();
$pluginRepository = $container->get('Eccube\Repository\PluginRepository');
$Plugin = $pluginRepository->findOneBy([
'source' => $source,
'code' => $code,
'version' => $version
]);
// レコードがある場合はcomposer.jsonの更新のみ行う.
if ($Plugin) {
parent::install($repo, $package);
$this->addPluginIdToComposerJson($package);
return;
}
try {
parent::install($repo, $package);
$this->addPluginIdToComposerJson($package);
/** @var PluginService $pluginService */
$pluginService = $container->get(PluginService::class);
$config = $pluginService->readConfig($this->getInstallPath($package));
$Plugin = $pluginService->registerPlugin($config, $config['source']);
} catch (\Exception $e) {
// 更新されたcomposer.jsonを戻す
parent::uninstall($repo, $package);
$fileName = $kernel->getProjectDir().DIRECTORY_SEPARATOR.'composer.json';
$contents = file_get_contents($fileName);
$json = new JsonManipulator($contents);
$json->removeSubNode('require', $package->getPrettyName());
file_put_contents($fileName, $json->getContents());
throw $e;
}
}
private function addPluginIdToComposerJson(PackageInterface $package)
{
$extra = $package->getExtra();
$id = @$extra['id'];
$composerPath = $this->getInstallPath($package).DIRECTORY_SEPARATOR.'composer.json';
if (file_exists($composerPath)) {
$composerJson = json_decode(file_get_contents($composerPath), true);
$composerJson['extra']['id'] = $id;
file_put_contents($composerPath, json_encode($composerJson));
}
}
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!isset($GLOBALS['kernel'])) {
$message = 'You can not uninstall the EC-CUBE plugin via `composer` command.'.PHP_EOL
.'Please use the `bin/console eccube:composer:remove '.$package->getName().'` instead.';
throw new \RuntimeException($message);
}
$kernel = $GLOBALS['kernel'];
$container = $kernel->getContainer();
$extra = $package->getExtra();
$code = $extra['code'];
$pluginRepository = $container->get('Eccube\Repository\PluginRepository');
$pluginService = $container->get('Eccube\Service\PluginService');
// 他のプラグインから依存されている場合はアンインストールできない
$enabledPlugins = $pluginRepository->findBy(['enabled' => Constant::ENABLED]);
foreach ($enabledPlugins as $p) {
if ($p->getCode() !== $code) {
$dir = 'app/Plugin/'.$p->getCode();
$jsonText = @file_get_contents($dir.'/composer.json');
if ($jsonText) {
$json = json_decode($jsonText, true);
if (array_key_exists('require', $json) && array_key_exists('ec-cube/'.$code, $json['require'])) {
throw new \RuntimeException('このプラグインに依存しているプラグインがあるため削除できません。'.$p->getCode());
}
}
}
}
// 無効化していないとアンインストールできない
$id = @$extra['id'];
if ($id) {
$Plugin = $pluginRepository->findOneBy(['source' => $id]);
if ($Plugin && $Plugin->isEnabled()) {
throw new \RuntimeException('プラグインを無効化してください。'.$code);
}
if ($Plugin) {
$pluginService->uninstall($Plugin);
}
}
parent::uninstall($repo, $package);
}
}