1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See class comment |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Netresearch |
8
|
|
|
* @package Netresearch\Kite\Service\Composer |
9
|
|
|
* @author Christian Opitz <[email protected]> |
10
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
11
|
|
|
* @link http://www.netresearch.de |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Netresearch\Kite\Service\Composer; |
15
|
|
|
use Netresearch\Kite\Exception; |
16
|
|
|
use Netresearch\Kite\Service\Composer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Package |
20
|
|
|
* |
21
|
|
|
* // Composer properties, not necessarily present: |
22
|
|
|
* |
23
|
|
|
* @property string $name Package name |
24
|
|
|
* @property string $version Package version |
25
|
|
|
* @property object $source Information about the source |
26
|
|
|
* (may have f.i. "reference") |
27
|
|
|
* |
28
|
|
|
* // Additional properties |
29
|
|
|
* |
30
|
|
|
* @property string $path Package path |
31
|
|
|
* @property bool $isRoot Whether package is root (project) |
32
|
|
|
* @property array $requires The packages from $require as array |
33
|
|
|
* @property bool $git Whether installed package is a get repository |
34
|
|
|
* @property array $branches Git branches (including remote branches) |
35
|
|
|
* @property array $upstreams Upstream branches (values) for local branches (keys) |
36
|
|
|
* @property string|null $branch The currently checked out branch, if any |
37
|
|
|
* If package is on a tag this will always be null |
38
|
|
|
* @property string|null $tag The currently checked out tag, if any |
39
|
|
|
* @property string|null $remote The remote url (no multiple urls supported) |
40
|
|
|
* |
41
|
|
|
* |
42
|
|
|
* @category Netresearch |
43
|
|
|
* @package Netresearch\Kite\Service\Composer |
44
|
|
|
* @author Christian Opitz <[email protected]> |
45
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
46
|
|
|
* @link http://www.netresearch.de |
47
|
|
|
*/ |
48
|
|
|
class Package |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var Composer |
52
|
|
|
*/ |
53
|
|
|
protected $composer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
protected static $forEachRefHeadSupported = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Package constructor. |
62
|
|
|
* |
63
|
|
|
* @param Composer $composer The parent object |
64
|
|
|
* @param string|object $composerJson The composer json |
65
|
|
|
* @param bool $isRoot Whether package is root |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Composer $composer, $composerJson, $isRoot = false) |
68
|
|
|
{ |
69
|
|
|
$this->composer = $composer; |
70
|
|
|
|
71
|
|
|
if (is_string($composerJson)) { |
72
|
|
|
$path = realpath($composerJson); |
73
|
|
|
if (!$path) { |
74
|
|
|
throw new Exception('Could not find ' . $composerJson); |
75
|
|
|
} |
76
|
|
|
$this->path = dirname($path); |
77
|
|
|
$composerJson = json_decode(file_get_contents($path)); |
78
|
|
|
if (!is_object($composerJson)) { |
79
|
|
|
throw new Exception('Could not load ' . $path); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
foreach (get_object_vars($composerJson) as $key => $value) { |
83
|
|
|
$this->$key = $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->isRoot = $isRoot; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load lazy properties |
91
|
|
|
* |
92
|
|
|
* @param string $name The property name |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function __get($name) |
97
|
|
|
{ |
98
|
|
|
if (in_array($name, ['branches', 'upstreams', 'branch', 'tag', 'git'], true)) { |
99
|
|
|
$this->loadGitInformation(); |
100
|
|
|
return $this->$name; |
101
|
|
|
} |
102
|
|
|
if ($name === 'remote') { |
103
|
|
|
$this->loadRemote(); |
104
|
|
|
return $this->$name; |
105
|
|
|
} |
106
|
|
|
if ($name === 'requires') { |
107
|
|
|
$this->loadRequires(); |
108
|
|
|
return $this->$name; |
109
|
|
|
} |
110
|
|
|
throw new Exception('Invalid property ' . $name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Mark lazy properties as present |
115
|
|
|
* |
116
|
|
|
* @param string $name The name |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function __isset($name) |
121
|
|
|
{ |
122
|
|
|
return in_array($name, ['branches', 'upstreams', 'branch', 'tag', 'git', 'remote', 'requires'], true); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load the requires - removes inline aliases |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function loadRequires() |
131
|
|
|
{ |
132
|
|
|
$this->requires = isset($this->require) ? get_object_vars($this->require) : array(); |
|
|
|
|
133
|
|
|
foreach ($this->requires as $package => $constraint) { |
134
|
|
|
if ($pos = strpos($constraint, ' as ')) { |
135
|
|
|
if ($hashPos = strpos($constraint, '#')) { |
136
|
|
|
// dev-master#old-hash isn't treated by composer, so we don't as well |
137
|
|
|
$pos = $hashPos; |
138
|
|
|
} |
139
|
|
|
$this->requires[$package] = substr($constraint, 0, $pos); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Reload requires from composer.json |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function reloadRequires() |
150
|
|
|
{ |
151
|
|
|
$file = $this->path . '/composer.json'; |
152
|
|
|
if (file_exists($file)) { |
153
|
|
|
$composerJson = json_decode(file_get_contents($file)); |
154
|
|
|
unset($this->require); |
155
|
|
|
if (isset($composerJson->require)) { |
156
|
|
|
$this->require = $composerJson->require; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
$this->loadRequires(); |
159
|
|
|
} |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the remote |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
protected function loadRemote() |
169
|
|
|
{ |
170
|
|
|
$this->remote = null; |
171
|
|
|
if ($this->git) { |
172
|
|
|
$remote = null; |
173
|
|
|
$remotesString = $this->composer->git('remote', $this->path, ['verbose' => true]); |
174
|
|
|
$lines = explode("\n", trim($remotesString)); |
175
|
|
|
foreach ($lines as $line) { |
176
|
|
|
preg_match('/^([^\s]+)\s+(.+) \((fetch|push)\)$/', $line, $match); |
177
|
|
|
array_shift($match); |
178
|
|
|
list($name, $url) = $match; |
|
|
|
|
179
|
|
|
if ($remote && $remote !== $url) { |
180
|
|
|
$this->composer->output("<warning>Can not handle multiple remote urls - using $remote</warning>"); |
181
|
|
|
break; |
182
|
|
|
} else { |
183
|
|
|
$remote = $url; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
$this->remote = $remote; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Load 'branches', 'upstreams', 'branch', 'tag', 'git' |
192
|
|
|
* |
193
|
|
|
* @throws Exception\ProcessFailedException |
194
|
|
|
* |
195
|
|
|
* @return void |
196
|
|
|
*/ |
197
|
|
|
protected function loadGitInformation() |
198
|
|
|
{ |
199
|
|
|
$this->branches = array(); |
200
|
|
|
$this->upstreams = array(); |
201
|
|
|
$this->branch = null; |
202
|
|
|
$this->tag = null; |
203
|
|
|
$gitDir = $this->path . '/.git'; |
204
|
|
|
$this->git = file_exists($gitDir) && is_dir($gitDir); |
205
|
|
|
if ($this->git) { |
206
|
|
|
$this->composer->git('fetch', $this->path, array('p' => true, 'origin')); |
207
|
|
|
try { |
208
|
|
|
$format = (self::$forEachRefHeadSupported ? '%(HEAD)' : '') . '|%(refname:short)|%(upstream:short)'; |
209
|
|
|
$gitBr = $this->composer->git('for-each-ref', $this->path, ['format' => $format, 'refs/heads/', 'refs/remotes/origin']); |
210
|
|
|
} catch (Exception\ProcessFailedException $e) { |
211
|
|
|
if (trim($e->getProcess()->getErrorOutput()) === 'fatal: unknown field name: HEAD') { |
212
|
|
|
self::$forEachRefHeadSupported = false; |
213
|
|
|
$this->loadGitInformation(); |
214
|
|
|
return; |
215
|
|
|
} else { |
216
|
|
|
throw $e; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
if (!self::$forEachRefHeadSupported && !($this->tag = $this->getTag())) { |
220
|
|
|
$this->branch = $this->composer->git('rev-parse', $this->path, ['abbrev-ref' => true, 'HEAD']) ?: null; |
221
|
|
|
} |
222
|
|
|
foreach (explode("\n", trim($gitBr)) as $line) { |
223
|
|
|
list($head, $branch, $upstream) = explode('|', $line); |
224
|
|
|
if ($branch === 'origin/HEAD') { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
$this->branches[] = $branch; |
228
|
|
|
if ($head === '*') { |
229
|
|
|
$this->branch = $branch; |
230
|
|
|
} |
231
|
|
|
if ($upstream) { |
232
|
|
|
$this->upstreams[$branch] = $upstream; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
if (self::$forEachRefHeadSupported && !$this->branch) { |
236
|
|
|
$this->tag = $this->getTag(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get the currently checked out tag |
243
|
|
|
* |
244
|
|
|
* @return string|null |
245
|
|
|
*/ |
246
|
|
|
protected function getTag() |
247
|
|
|
{ |
248
|
|
|
try { |
249
|
|
|
return trim($this->composer->git('describe', $this->path, array('exact-match' => true, 'tags' => true))); |
250
|
|
|
} catch (Exception\ProcessFailedException $e) { |
251
|
|
|
return null; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
?> |
258
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.