-
Notifications
You must be signed in to change notification settings - Fork 2
/
Packages.php
268 lines (218 loc) 路 6.13 KB
/
Packages.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/*
* Inphinit
*
* Copyright (c) 2024 Guilherme Nascimento (brcontainer@yahoo.com.br)
*
* Released under the MIT license
*/
namespace Inphinit;
class Packages
{
private static $composerLock;
private $composerPath;
private $classmapName = 'autoload_classmap.php';
private $psrZeroName = 'autoload_namespaces.php';
private $psrFourName = 'autoload_psr4.php';
private $libs = array();
private $log = array();
/**
* Create a `Inphinit\Packages` instance.
*
* @param string $path Define composer path, like `./vendor/composer` (if null or not defined, assume `./system/vender`)
* @throws \Inphinit\Exception
* @return void
*/
public function __construct($path = null)
{
if (empty($path)) {
$path = INPHINIT_SYSTEM . '/vendor/composer';
}
if (is_dir($path) === false) {
throw new Exception('Composer path is not accessible: ' . $path, 0, 2);
}
$this->composerPath = str_replace('\\', '/', realpath($path)) . '/';
}
/**
* Get log
*
* @return array
*/
public function logs()
{
return $this->log;
}
/**
* Auto import composer packages
*
* @return int
*/
public function auto()
{
return 0 + $this->classmap() + $this->psr0() + $this->psr4();
}
/**
* Load `./system/boot/namespaces.php` classes
*
* @return int|bool Return total packages loaded, if `namespaces.php`
* is not accessible return `false`
*/
public function inAutoload()
{
$path = INPHINIT_SYSTEM . '/boot/namespaces.php';
if (is_file($path)) {
$data = include $path;
if (is_array($data)) {
$this->libs = $data + $this->libs;
}
return count($this->libs);
}
return false;
}
/**
* Load `autoload_classmap.php` classes
*
* @return int|bool Return total packages loaded, if `autoload_classmap.php`
* is not accessible return `false`
*/
public function classmap()
{
$path = $this->composerPath . $this->classmapName;
$i = 0;
if (is_file($path)) {
$data = include $path;
foreach ($data as $key => $value) {
if (empty($value) === false) {
$this->libs[$key] = $value;
++$i;
}
}
$this->log[] = 'Imported ' . $i . ' classes from classmap';
return $i;
}
$this->log[] = 'Warn: classmap not found';
return false;
}
/**
* Load `autoload_namespaces.php` classes, used by PSR-0 packages
*
* @return int|bool Return total packages loaded, if `autoload_namespaces.php`
* is not accessible return `false`
*/
public function psr0()
{
$i = $this->load($this->composerPath . $this->psrZeroName);
if ($i !== false) {
$this->log[] = 'Imported ' . $i . ' classes from psr0';
return $i;
}
$this->log[] = 'Warn: psr0 not found';
return false;
}
/**
* Load `autoload_psr4.php` classes, used by PSR-4 packages
*
* @return int|bool Return total packages loaded, if `autoload_psr4.php`
* is not accessible return `false`
*/
public function psr4()
{
$i = $this->load($this->composerPath . $this->psrFourName);
if ($i !== false) {
$this->log[] = 'Imported ' . $i . ' classes from psr4';
return $i;
}
$this->log[] = 'Warn: psr4 not found';
return false;
}
/**
* Save imported packages path to file in PHP format
*
* @param string $path File to save packages paths, eg. `/foo/namespaces.php`
* @throws \Inphinit\Exception
* @return bool
*/
public function save($path)
{
if (count($this->libs) === 0) {
return false;
}
$handle = fopen($path, 'w');
if ($handle === false) {
throw new Exception('This path is not writabled: ' . $path, 0, 2);
}
$libs = $this->libs;
foreach ($libs as $key => &$value) {
$value = self::relativePath($value);
}
fwrite($handle, "<?php\nreturn");
fwrite($handle, " " . var_export($libs, true) . ";\n");
fclose($handle);
return true;
}
/**
* Return array of libs
*
* @return array
*/
public function getLibs()
{
return $this->libs;
}
private static function relativePath($path)
{
$path = str_replace('\\', '/', $path);
$system = INPHINIT_SYSTEM . '/';
if (strpos($path, $system) === 0) {
$path = substr($path, strlen($system));
}
return $path;
}
/**
* Get package version from composer.lock file
*
* @param string $name set package for detect version
* @return string|null
*/
public static function version($name)
{
if (self::$composerLock === null) {
$file = INPHINIT_ROOT . '/composer.lock';
if (is_file($file)) {
self::$composerLock = json_decode(file_get_contents($file));
}
}
$data = self::$composerLock;
if (empty($data->packages)) {
return null;
}
$version = null;
foreach ($data->packages as $package) {
if ($package->name === $name) {
$version = $package->version;
break;
}
}
$data = null;
return $version;
}
private function load($path)
{
if (is_file($path) === false) {
return false;
}
$data = include $path;
$i = 0;
foreach ($data as $key => $value) {
if (isset($value[0]) && is_string($value[0])) {
$this->libs[$key] = $value[0];
++$i;
}
}
return $i;
}
public function __destruct()
{
$this->log = $this->libs = null;
}
}