| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Analysers; |
| 8 |
|
| 9 |
use Composer\Autoload\ClassLoader; |
| 10 |
|
| 11 |
/** |
| 12 |
* Scans for classes/interfaces/traits. |
| 13 |
* |
| 14 |
* Relies on a <code>composer --optimized</code> run in order to utilize |
| 15 |
* the generated class map. |
| 16 |
*/ |
| 17 |
class ComposerAutoloaderScanner |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Collect all classes/interfaces/traits known by composer. |
| 21 |
* |
| 22 |
* @param list<string> $namespaces |
| 23 |
* |
| 24 |
* @return list<string> |
| 25 |
*/ |
| 26 |
public function scan(array $namespaces): array |
| 27 |
{ |
| 28 |
$units = []; |
| 29 |
if ($autoloader = static::getComposerAutoloader()) { |
| 30 |
foreach (array_keys($autoloader->getClassMap()) as $unit) { |
| 31 |
foreach ($namespaces as $namespace) { |
| 32 |
if (str_starts_with($unit, $namespace)) { |
| 33 |
$units[] = $unit; |
| 34 |
break; |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $units; |
| 41 |
} |
| 42 |
|
| 43 |
public static function getComposerAutoloader(): ?ClassLoader |
| 44 |
{ |
| 45 |
foreach (spl_autoload_functions() as $fkt) { |
| 46 |
if (is_array($fkt) && $fkt[0] instanceof ClassLoader) { |
| 47 |
return $fkt[0]; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return null; |
| 52 |
} |
| 53 |
} |