-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
notes
268 lines (224 loc) · 7.23 KB
/
notes
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
/*
* This file is part of Composer.
*
* (c) Nils Adermann <[email protected]>
* Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Plugin;
/**
* The Plugin Events.
*
* @author Nils Adermann <[email protected]>
*/
class PluginEvents
{
/**
* The INIT event occurs after a Composer instance is done being initialized
*
* The event listener method receives a
* Composer\EventDispatcher\Event instance.
*
* @var string
*/
public const INIT = 'init';
/**
* The COMMAND event occurs as a command begins
*
* The event listener method receives a
* Composer\Plugin\CommandEvent instance.
*
* @var string
*/
public const COMMAND = 'command';
/**
* The PRE_FILE_DOWNLOAD event occurs before downloading a file
*
* The event listener method receives a
* Composer\Plugin\PreFileDownloadEvent instance.
*
* @var string
*/
public const PRE_FILE_DOWNLOAD = 'pre-file-download';
/**
* The POST_FILE_DOWNLOAD event occurs after downloading a package dist file
*
* The event listener method receives a
* Composer\Plugin\PostFileDownloadEvent instance.
*
* @var string
*/
public const POST_FILE_DOWNLOAD = 'post-file-download';
/**
* The PRE_COMMAND_RUN event occurs before a command is executed and lets you modify the input arguments/options
*
* The event listener method receives a
* Composer\Plugin\PreCommandRunEvent instance.
*
* @var string
*/
public const PRE_COMMAND_RUN = 'pre-command-run';
/**
* The PRE_POOL_CREATE event occurs before the Pool of packages is created, and lets
* you filter the list of packages which is going to enter the Solver
*
* The event listener method receives a
* Composer\Plugin\PrePoolCreateEvent instance.
*
* @var string
*/
public const PRE_POOL_CREATE = 'pre-pool-create';
}
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
/*
* This script checks for dependencies between our components
* and fails if a component has a wrong dependency.
*/
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;
$loader = require './vendor/autoload.php';
$namespace = 'ApiPlatform';
$prefix = 'api-platform';
$ignoreList = ['ApiPlatform\\Api', 'ApiPlatform\\Exception', 'ApiPlatform\\Util'];
$stopOnFailure = in_array('--stop-on-failure', $_SERVER['argv'], true);
$skipPaths = [__DIR__.'/src/GraphQl/Resolver/Stage'];
/**
* Reads the beginning of a PHP class and returns "use" instructions matching our namespace.
*/
$class_uses_namespaces = function (ReflectionClass $r) use ($namespace): \Generator {
$fp = fopen($r->getFileName(), 'r');
$u = 'use';
$c = false;
while (($buffer = fgets($fp, 4096)) !== false) {
if ($c && \PHP_EOL === $buffer) {
break;
}
if (!str_starts_with($buffer, $u)) {
continue;
}
$c = true;
$buffer = substr($buffer, 4, -2);
if (str_starts_with($buffer, $namespace)) {
yield substr($buffer, 0, strpos($buffer, ' ') ?: null);
}
}
fclose($fp);
};
// Creates and require the map of dependencies
$directories = [];
$map = [];
foreach (Finder::create()->in('src')->notPath('vendor')->name('composer.json') as $f) {
if (null === ($component = json_decode($f->getContents(), true))) {
continue;
}
$filter = fn ($v) => str_starts_with($v, $prefix);
$dependencies =
array_merge(
array_filter(array_keys((array) $component['require']), $filter),
array_filter(array_keys((array) $component['require-dev'] ?? []), $filter)
);
$map[$component['name']] = ['namespace' => substr(key($component['autoload']['psr-4']), 0, -1), 'dependencies' => $dependencies];
$directories[] = substr($f->getRealPath(), 0, -14);
foreach (Finder::create()->in($f->getPath())->notPath('vendor')->notPath('var')->name('*.php')->notName('*.tpl.php')->notName('bootstrap.php') as $f) {
require_once $f->getRealPath();
}
}
// create a PSR map of dependencies
$psrMap = [];
foreach ($map as $component) {
$depsPsr = [];
foreach ($component['dependencies'] as $d) {
$depsPsr[] = $map[$d]['namespace'];
}
$psrMap[$component['namespace']] = $depsPsr;
}
$warned = [];
$getComponentNamespace = function (ReflectionClass $r, ?ReflectionClass $inside = null) use ($psrMap, $warned, $ignoreList, $namespace) {
$ns = $r->getNamespaceName();
// Find this components namespace
$nsParts = explode('\\', $ns);
$n = count($nsParts);
$componentNs = $nsParts[0].'\\'.$nsParts[1];
$i = 2;
while (!isset($psrMap[$componentNs]) && $i < $n) {
if ($part = ($nsParts[$i++] ?? null)) {
$componentNs .= '\\'.$part;
}
}
if (!isset($psrMap[$componentNs])) {
if (in_array($componentNs, $ignoreList, true)) {
return null;
}
$guess = $nsParts[0].'\\'.$nsParts[1];
if ($warned[$guess] ?? true) {
echo sprintf('"%s" is not an %s component at "%s" %s', $guess, $namespace, ($inside ?? $r)->getFileName(), \PHP_EOL);
$warned[$guess] = false;
}
return null;
}
return $componentNs;
};
$exitCode = 0;
$lnamespace = strlen($namespace);
foreach (array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits()) as $className) {
$r = new ReflectionClass($className);
$ns = $r->getNamespaceName();
foreach ($skipPaths as $base) {
if (!($fileName = $r->getFileName())) {
continue;
}
if (Path::isBasePath($skipPaths[0], $fileName)) {
continue 2;
}
}
if (!str_starts_with($ns, $namespace)) {
continue;
}
$componentNs = $getComponentNamespace($r);
if (!$componentNs) {
continue;
}
foreach ($class_uses_namespaces($r) as $u) {
if (str_starts_with($u, $componentNs)) {
continue;
}
$useNs = $getComponentNamespace(new ReflectionClass($u), $r);
if (!$useNs || $useNs === $componentNs) {
continue;
}
if (!in_array($useNs, $psrMap[$componentNs], true)) {
echo sprintf('"%s" uses "%s" although "%s" is not one of its dependencies %s', $className, $u, $useNs, \PHP_EOL);
$exitCode = 1;
if ($stopOnFailure) {
exit($exitCode);
}
}
}
}
exit($exitCode);
// another way of duping
$config = $composer->getConfig();
$locker = $composer->getLocker();
$classMap = $generator->dump(
$config,
$localRepo,
$package,
$installationManager,
'tmp',
true,
null,
$locker
);
// var_dump($classMap->getMap(), $classMap, $locker);
// $output->writeln('Executing');