1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
namespace Eccube\Service\Composer; |
24
|
|
|
|
25
|
|
|
use Composer\Console\Application; |
26
|
|
|
use Eccube\Annotation\Service; |
27
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
28
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class ComposerApiService |
32
|
|
|
* @package Eccube\Service\Composer |
33
|
|
|
* @Service |
34
|
|
|
*/ |
35
|
|
|
class ComposerApiService implements ComposerServiceInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $appConfig; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Application $consoleApplication |
44
|
|
|
*/ |
45
|
|
|
private $consoleApplication; |
46
|
|
|
|
47
|
|
|
private $workingDir; |
48
|
|
|
|
49
|
|
|
public function __construct($appConfig) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->appConfig = $appConfig; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Run get info command |
56
|
|
|
* |
57
|
|
|
* @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function execInfo($pluginName) |
61
|
|
|
{ |
62
|
|
|
$output = $this->runCommand(array( |
63
|
|
|
'command' => 'info', |
64
|
|
|
'package' => $pluginName, |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
return OutputParser::parseInfo($output); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Run execute command |
72
|
|
|
* |
73
|
|
|
* @param string $packageName format "foo/bar foo/bar:1.0.0" |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function execRequire($packageName) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$packageName = explode(" ", trim($packageName)); |
79
|
|
|
$output = $this->runCommand(array( |
80
|
|
|
'command' => 'require', |
81
|
|
|
'packages' => $packageName, |
82
|
|
|
'--no-interaction' => true, |
83
|
|
|
'--profile' => true, |
84
|
|
|
'--prefer-dist' => true, |
85
|
|
|
'--ignore-platform-reqs' => true, |
86
|
|
|
)); |
87
|
|
|
|
88
|
|
|
return OutputParser::parseRequire($output); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Run remove command |
93
|
|
|
* |
94
|
|
|
* @param string $packageName format "foo/bar foo/bar:1.0.0" |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function execRemove($packageName) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$packageName = explode(' ', trim($packageName)); |
100
|
|
|
$this->runCommand(array( |
101
|
|
|
'command' => 'remove', |
102
|
|
|
'packages' => $packageName, |
103
|
|
|
'--ignore-platform-reqs' => true, |
104
|
|
|
'--no-interaction' => true, |
105
|
|
|
'--profile' => true, |
106
|
|
|
)); |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get require |
113
|
|
|
* |
114
|
|
|
* @param string $packageName |
115
|
|
|
* @param string $callback |
116
|
|
|
* @param null $typeFilter |
117
|
|
|
*/ |
118
|
|
|
public function foreachRequires($packageName, $callback, $typeFilter = null) |
119
|
|
|
{ |
120
|
|
|
$info = $this->execInfo($packageName); |
121
|
|
|
if (isset($info['requires'])) { |
122
|
|
|
foreach ($info['requires'] as $name => $version) { |
123
|
|
|
$package = $this->execInfo($name); |
124
|
|
|
if (is_null($typeFilter) || @$package['type'] === $typeFilter) { |
125
|
|
|
$callback($package); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Run get config information |
133
|
|
|
* |
134
|
|
|
* @param string $key |
135
|
|
|
* @param null $value |
136
|
|
|
* @return array|mixed |
137
|
|
|
*/ |
138
|
|
|
public function execConfig($key, $value = null) |
139
|
|
|
{ |
140
|
|
|
$commands = array( |
141
|
|
|
'command' => 'config', |
142
|
|
|
'setting-key' => $key, |
143
|
|
|
'setting-value' => $value, |
144
|
|
|
); |
145
|
|
|
if ($value) { |
146
|
|
|
$commands['setting-value'] = $value; |
147
|
|
|
} |
148
|
|
|
$output = $this->runCommand($commands); |
149
|
|
|
|
150
|
|
|
return OutputParser::parseConfig($output); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get config list |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function getConfig() |
159
|
|
|
{ |
160
|
|
|
$output = $this->runCommand(array( |
161
|
|
|
'command' => 'config', |
162
|
|
|
'--list' => true, |
163
|
|
|
)); |
164
|
|
|
|
165
|
|
|
return OutputParser::parseList($output); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set work dir |
170
|
|
|
* |
171
|
|
|
* @param string $workingDir |
172
|
|
|
*/ |
173
|
|
|
public function setWorkingDir($workingDir) |
174
|
|
|
{ |
175
|
|
|
$this->workingDir = $workingDir; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Run composer command |
180
|
|
|
* |
181
|
|
|
* @param array $commands |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function runCommand($commands) |
185
|
|
|
{ |
186
|
|
|
$this->init(); |
187
|
|
|
$commands['--working-dir'] = $this->workingDir; |
188
|
|
|
$commands['--no-ansi'] = 1; |
189
|
|
|
$input = new ArrayInput($commands); |
190
|
|
|
$output = new BufferedOutput(); |
191
|
|
|
|
192
|
|
|
$exitCode = $this->consoleApplication->run($input, $output); |
193
|
|
|
|
194
|
|
|
$log = $output->fetch(); |
195
|
|
|
if ($exitCode) { |
|
|
|
|
196
|
|
|
log_error($log); |
197
|
|
|
throw new \RuntimeException($log); |
198
|
|
|
} |
199
|
|
|
log_info($log, $commands); |
200
|
|
|
|
201
|
|
|
return $log; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Init composer console application |
206
|
|
|
*/ |
207
|
|
|
private function init() |
208
|
|
|
{ |
209
|
|
|
set_time_limit(0); |
210
|
|
|
@ini_set('memory_limit', '1536M'); |
|
|
|
|
211
|
|
|
// Config for some environment |
212
|
|
|
putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer'); |
213
|
|
|
$consoleApplication = new Application(); |
214
|
|
|
$consoleApplication->resetComposer(); |
215
|
|
|
$consoleApplication->setAutoExit(false); |
216
|
|
|
$this->consoleApplication = $consoleApplication; |
217
|
|
|
$this->workingDir = $this->workingDir ? $this->workingDir : $this->appConfig['root_dir']; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|