This repository has been archived by the owner on Oct 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PluginInstaller.php
178 lines (144 loc) · 5.5 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
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
<?php
namespace Shyim;
use Shyim\Api\Client;
use Shyim\Struct\License\Binaries;
use Shyim\Struct\License\License;
use Shyim\Struct\License\Plugin;
class PluginInstaller
{
/**
* @var Client
*/
private $client;
/**
* @var array
*/
private $extra;
/**
* @var License[]
*/
private $licenses;
public function __construct(Client $client, array $extra)
{
$this->client = $client;
$this->extra = $extra;
$this->licenses = $client->getLicenses();
}
public function installPlugin(string $name, string $version)
{
$license = array_filter($this->licenses, function (License $license) use ($name) {
// Basic Plugins like SwagCore
if (!isset($license->plugin)) {
return false;
}
return $license->plugin->name === $name || $license->plugin->code === $name;
});
if (empty($license)) {
$this->checkExistenceOfPlugin($name);
}
/** @var License $license */
$license = array_values($license)[0];
// Fix plugin name
$name = $license->plugin->name;
$versions = array_map(function (Binaries $binary) {
return $binary->version;
}, $license->plugin->binaries);
$version = VersionSelector::getVersion($name, $version, $versions);
if (!in_array($version, $versions)) {
throw new \RuntimeException(sprintf('[Installer] Plugin with name "%s" doesnt have the version "%s", Available versions are %s', $name, $version, implode(', ', array_reverse($versions))));
}
if ($path = LocalCache::getPlugin($name, $version)) {
ComposerPlugin::$io->write(sprintf('[Installer] Using plugin "%s" with version %s from cache', $name, $version), true);
self::extractPlugin($path);
return;
}
$binaryVersion = array_values(array_filter($license->plugin->binaries, function (Binaries $binary) use ($version) {
return $binary->version === $version;
}))[0];
ComposerPlugin::$io->write(sprintf('[Installer] Downloading plugin "%s" with version %s', $name, $version), true);
$this->movePlugin($this->client->downloadPlugin($binaryVersion), $name, $version);
}
/**
* @param string|array $content
* @param string $name
* @param string $version
*
* @throws \Exception
*/
private function movePlugin($content, $name, $version)
{
if (is_array($content)) {
if (array_key_exists('success', $content)) {
if (!$content['success']) {
throw new \InvalidArgumentException(sprintf('Could not download plugin %s in version %s maybe not a valid licence for this version', $name, $version));
}
}
}
$file = LocalCache::getCachePath($name, $version);
file_put_contents($file, $content);
$this->extractPlugin($file);
}
private function extractPlugin(string $zipFile)
{
try {
$zip = new \ZipArchive();
$zip->open($zipFile);
$folderpath = str_replace('\\', '/', $zip->statIndex(0)['name']);
$pos = strpos($folderpath, '/');
$path = substr($folderpath, 0, $pos);
$location = $this->getExtractLocation($path);
$zip->extractTo($location);
$zip->close();
} catch (\Exception $e) {
LocalCache::cleanByPath($zipFile);
Util::throwException($e);
}
}
/**
* @param string $name
*
* @return string
*/
private function getExtractLocation(string $name)
{
if (isset($this->extra['installer-paths'])) {
foreach ($this->extra['installer-paths'] as $folder => $types) {
$possibleValues = ['shopware-backend-plugin', 'shopware-frontend-plugin', 'shopware-core-plugin'];
$possibleTypes = ['Frontend', 'Core', 'Backend'];
$types[0] = str_replace('type:', '', $types[0]);
if (in_array($name, $possibleTypes)) {
if (in_array($types[0], $possibleValues)) {
return dirname(getcwd() . '/' . str_replace('{$name}/', '', $folder));
}
} elseif ($types[0] === 'shopware-plugin') {
return dirname(getcwd() . '/' . str_replace('{$name}/', '', $folder));
}
}
}
switch ($name) {
case 'Frontend':
case 'Core':
case 'Backend':
return getcwd() . '/Plugins/Community/';
default:
return getcwd() . '/custom/plugins/';
}
}
private function checkExistenceOfPlugin(string $name)
{
$plugins = $this->client->searchPlugin($name);
$found = false;
foreach ($plugins as $plugin) {
if ($plugin->code === $name || $plugin->name === $name) {
$found = true;
}
}
if ($found || empty($plugins)) {
throw new \RuntimeException(sprintf('[Installer] Plugin with name "%s" is not available in your Account. Please buy the plugin first', $name));
}
$names = array_map(function(\Shyim\Struct\Plugin\Plugin $plugin) {
return $plugin->name;
}, $plugins);
throw new \RuntimeException(sprintf('[Installer] Could not find plugin by name "%s". Did you mean some of %s', $name, implode(', ', $names)));
}
}