AutoloadMerger.php
41 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace TypistTech\Imposter\Plugin; |
| 6 | |
| 7 | use Composer\Package\RootPackageInterface; |
| 8 | use RuntimeException; |
| 9 | use TypistTech\Imposter\ImposterFactory; |
| 10 | |
| 11 | class AutoloadMerger |
| 12 | { |
| 13 | public static function run(RootPackageInterface $package): void |
| 14 | { |
| 15 | $autoload = $package->getAutoload(); |
| 16 | $autoload = array_merge_recursive($autoload, [ |
| 17 | 'classmap' => static::getImposterAutoloads(), |
| 18 | ]); |
| 19 | |
| 20 | $package->setAutoload($autoload); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return string[] |
| 25 | * @todo [Help Wanted] Think of a better way to handle file not found during installation |
| 26 | */ |
| 27 | protected static function getImposterAutoloads(): array |
| 28 | { |
| 29 | try { |
| 30 | $cwd = getcwd(); |
| 31 | $imposter = ImposterFactory::forProject($cwd, ['typisttech/imposter-plugin']); |
| 32 | |
| 33 | return array_map(function ($path) use ($cwd): string { |
| 34 | return str_replace($cwd . '/', '', $path); |
| 35 | }, $imposter->getAutoloads()); |
| 36 | } catch (RuntimeException $exception) { |
| 37 | return []; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 |