1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Service\Composer; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Eccube\Common\EccubeConfig; |
18
|
|
|
use Eccube\Exception\PluginException; |
19
|
|
|
use Eccube\Service\SystemService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ComposerProcessService |
23
|
|
|
*/ |
24
|
|
|
class ComposerProcessService implements ComposerServiceInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EccubeConfig config parameter |
28
|
|
|
*/ |
29
|
|
|
protected $eccubeConfig; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EntityManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $entityManager; |
35
|
|
|
|
36
|
|
|
private $workingDir; |
37
|
|
|
private $composerFile; |
38
|
|
|
private $composerSetup; |
39
|
|
|
private $pathPHP; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ComposerProcessService constructor. |
43
|
|
|
* |
44
|
|
|
* @param EccubeConfig $eccubeConfig |
45
|
|
|
* @param EntityManagerInterface $entityManager |
46
|
|
|
* @param SystemService $systemService |
47
|
|
|
*/ |
48
|
|
|
public function __construct(EccubeConfig $eccubeConfig, EntityManagerInterface $entityManager, SystemService $systemService) |
49
|
|
|
{ |
50
|
|
|
$this->eccubeConfig = $eccubeConfig; |
51
|
|
|
$this->entityManager = $entityManager; |
52
|
|
|
$this->pathPHP = $systemService->getPHP(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* This function to install a plugin by composer require |
57
|
|
|
* |
58
|
|
|
* @param string $packageName format "foo/bar foo/bar2:1.0.0" |
59
|
|
|
* |
60
|
|
|
* @throws PluginException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function execRequire($packageName, $output = null) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
set_time_limit(0); |
65
|
|
|
$this->init(); |
66
|
|
|
|
67
|
|
|
// Build command |
68
|
|
|
$command = $this->pathPHP.' '.$this->composerFile.' require '.$packageName; |
69
|
|
|
$command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --update-with-dependencies --profile --no-ansi --no-interaction -d '; |
70
|
|
|
$command .= $this->workingDir.' 2>&1'; |
71
|
|
|
log_info($command); |
72
|
|
|
$this->runCommand($command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* This function to remove a plugin by composer remove |
77
|
|
|
* |
78
|
|
|
* @param string $packageName format "foo/bar foo/bar2" |
79
|
|
|
* |
80
|
|
|
* @throws PluginException |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function execRemove($packageName, $output = null) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
set_time_limit(0); |
85
|
|
|
$this->init(); |
86
|
|
|
|
87
|
|
|
// Build command |
88
|
|
|
$command = $this->pathPHP.' '.$this->composerFile.' remove '.$packageName; |
89
|
|
|
$command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d '; |
90
|
|
|
$command .= $this->workingDir.' 2>&1'; |
91
|
|
|
log_info($command); |
92
|
|
|
|
93
|
|
|
// Execute command |
94
|
|
|
$this->runCommand($command); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Run command |
99
|
|
|
* |
100
|
|
|
* @throws PluginException |
101
|
|
|
* |
102
|
|
|
* @param string $command |
103
|
|
|
*/ |
104
|
|
|
public function runCommand($command) |
105
|
|
|
{ |
106
|
|
|
$output = []; |
107
|
|
|
try { |
108
|
|
|
// Execute command |
109
|
|
|
$returnValue = -1; |
110
|
|
|
exec($command, $output, $returnValue); |
111
|
|
|
|
112
|
|
|
$outputString = implode(PHP_EOL, $output); |
113
|
|
|
if ($returnValue) { |
|
|
|
|
114
|
|
|
throw new PluginException($outputString); |
115
|
|
|
} |
116
|
|
|
log_info(PHP_EOL.$outputString.PHP_EOL); |
117
|
|
|
} catch (\Exception $exception) { |
118
|
|
|
throw new PluginException($exception->getMessage()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set working dir |
124
|
|
|
* |
125
|
|
|
* @param string $workingDir |
126
|
|
|
*/ |
127
|
|
|
public function setWorkingDir($workingDir) |
128
|
|
|
{ |
129
|
|
|
$this->workingDir = $workingDir; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set init |
134
|
|
|
* |
135
|
|
|
* @throws PluginException |
136
|
|
|
*/ |
137
|
|
|
private function init() |
138
|
|
|
{ |
139
|
|
|
if (!$this->isPhpCommandLine()) { |
140
|
|
|
throw new PluginException('Php cli not found.'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$composerMemory = $this->eccubeConfig['eccube_composer_memory_limit']; |
144
|
|
|
if (!$this->isSetCliMemoryLimit()) { |
145
|
|
|
$cliMemoryLimit = $this->getCliMemoryLimit(); |
146
|
|
|
if ($cliMemoryLimit < $composerMemory && $cliMemoryLimit != -1) { |
147
|
|
|
throw new PluginException('Not enough memory limit.'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Mysql lock in transaction |
153
|
|
|
* |
154
|
|
|
* @see https://dev.mysql.com/doc/refman/5.7/en/lock-tables.html |
155
|
|
|
* |
156
|
|
|
* @var EntityManagerInterface |
157
|
|
|
*/ |
158
|
|
|
$em = $this->entityManager; |
159
|
|
|
if ($em->getConnection()->isTransactionActive()) { |
160
|
|
|
$em->getConnection()->commit(); |
161
|
|
|
$em->getConnection()->beginTransaction(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
@ini_set('memory_limit', $composerMemory.'M'); |
|
|
|
|
165
|
|
|
// Config for some environment |
166
|
|
|
putenv('COMPOSER_HOME='.$this->eccubeConfig['plugin_realdir'].'/.composer'); |
167
|
|
|
$this->workingDir = $this->workingDir ? $this->workingDir : $this->eccubeConfig['root_dir']; |
168
|
|
|
$this->setupComposer(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check composer file and setup it |
173
|
|
|
*/ |
174
|
|
|
private function setupComposer() |
175
|
|
|
{ |
176
|
|
|
$this->composerFile = $this->workingDir.'/composer.phar'; |
177
|
|
|
$this->composerSetup = $this->workingDir.'/composer-setup.php'; |
178
|
|
|
if (!file_exists($this->composerFile)) { |
179
|
|
|
if (!file_exists($this->composerSetup)) { |
180
|
|
|
$result = copy('https://getcomposer.org/installer', $this->composerSetup); |
181
|
|
|
log_info($this->composerSetup.' : '.$result); |
182
|
|
|
} |
183
|
|
|
$command = $this->pathPHP.' '.$this->composerSetup; |
184
|
|
|
$this->runCommand($command); |
185
|
|
|
|
186
|
|
|
unlink($this->composerSetup); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get grep memory_limit | Megabyte |
192
|
|
|
* |
193
|
|
|
* @return int|string |
194
|
|
|
*/ |
195
|
|
|
private function getCliMemoryLimit() |
196
|
|
|
{ |
197
|
|
|
$grepMemory = exec($this->pathPHP.' -i | grep "memory_limit"'); |
198
|
|
|
if ($grepMemory) { |
199
|
|
|
$grepMemory = explode('=>', $grepMemory); |
200
|
|
|
|
201
|
|
|
// -1 unlimited |
202
|
|
|
if (trim($grepMemory[2]) == -1) { |
203
|
|
|
return -1; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$exp = preg_split('#(?<=\d)(?=[a-z])#i', $grepMemory[2]); |
207
|
|
|
$memo = trim($exp[0]); |
208
|
|
|
if ($exp[1] == 'M') { |
209
|
|
|
return $memo; |
210
|
|
|
} else { |
211
|
|
|
if ($exp[1] == 'GB') { |
212
|
|
|
return $memo * 1024; |
213
|
|
|
} else { |
214
|
|
|
return 0; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return 0; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Check to set new value grep "memory_limit" |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
private function isSetCliMemoryLimit() |
228
|
|
|
{ |
229
|
|
|
$oldMemory = exec($this->pathPHP.' -i | grep "memory_limit"'); |
230
|
|
|
$tmpMem = '1.5GB'; |
231
|
|
|
if ($oldMemory) { |
232
|
|
|
$memory = explode('=>', $oldMemory); |
233
|
|
|
$originGrepMemmory = trim($memory[2]); |
234
|
|
|
|
235
|
|
|
if ($originGrepMemmory == $tmpMem) { |
236
|
|
|
$tmpMem = '1.49GB'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$newMemory = exec($this->pathPHP.' -d memory_limit='.$tmpMem.' -i | grep "memory_limit"'); |
240
|
|
|
if ($newMemory) { |
241
|
|
|
$newMemory = explode('=>', $newMemory); |
242
|
|
|
$grepNewMemory = trim($newMemory[2]); |
243
|
|
|
if ($grepNewMemory != $originGrepMemmory) { |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Check php command line |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
private function isPhpCommandLine() |
258
|
|
|
{ |
259
|
|
|
$php = exec('which php'); |
260
|
|
|
if (null != $php) { |
261
|
|
|
if (strpos(strtolower($php), 'php') !== false) { |
262
|
|
|
return true; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|