|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Testbench; |
| 6 | + |
| 7 | +use Hyperf\Collection\LazyCollection; |
| 8 | +use Hypervel\Filesystem\Filesystem; |
| 9 | +use Hypervel\Foundation\ClassLoader; |
| 10 | +use Hypervel\Foundation\Testing\TestScanHandler; |
| 11 | +use Symfony\Component\Yaml\Yaml; |
| 12 | + |
| 13 | +use function Hypervel\Filesystem\join_paths; |
| 14 | + |
| 15 | +class Bootstrapper |
| 16 | +{ |
| 17 | + protected static $config = []; |
| 18 | + |
| 19 | + protected static ?Filesystem $filesystem = null; |
| 20 | + |
| 21 | + public static function bootstrap(): void |
| 22 | + { |
| 23 | + static::loadConfigFromYaml( |
| 24 | + $workingPath = defined('TESTBENCH_WORKING_PATH') ? TESTBENCH_WORKING_PATH : dirname(__DIR__) |
| 25 | + ); |
| 26 | + |
| 27 | + $basePath = "{$workingPath}/workbench"; |
| 28 | + if (static::$config['hypervel'] ?? null) { |
| 29 | + $basePath = static::$config['hypervel']; |
| 30 | + } |
| 31 | + |
| 32 | + ! defined('BASE_PATH') && define('BASE_PATH', $basePath); |
| 33 | + ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL); |
| 34 | + |
| 35 | + static::generateEnv(); |
| 36 | + static::generateComposerLock(); |
| 37 | + static::registerPurgeFiles(); |
| 38 | + |
| 39 | + ClassLoader::init(null, null, new TestScanHandler()); |
| 40 | + } |
| 41 | + |
| 42 | + public static function getConfig(): array |
| 43 | + { |
| 44 | + return static::$config; |
| 45 | + } |
| 46 | + |
| 47 | + protected static function getFilesystem(): Filesystem |
| 48 | + { |
| 49 | + if (static::$filesystem) { |
| 50 | + return static::$filesystem; |
| 51 | + } |
| 52 | + |
| 53 | + return static::$filesystem = new Filesystem(); |
| 54 | + } |
| 55 | + |
| 56 | + protected static function generateComposerLock(): void |
| 57 | + { |
| 58 | + $content = [ |
| 59 | + 'packages' => [ |
| 60 | + [ |
| 61 | + 'name' => 'hypervel-testbench', |
| 62 | + 'extra' => [ |
| 63 | + 'hypervel' => [ |
| 64 | + 'config' => static::getConfigProviders(), |
| 65 | + 'providers' => static::$config['providers'] ?? [], |
| 66 | + 'dont-discover' => static::$config['dont-discover'] ?? [], |
| 67 | + ], |
| 68 | + ], |
| 69 | + ], |
| 70 | + ], |
| 71 | + 'packages-dev' => [], |
| 72 | + ]; |
| 73 | + |
| 74 | + static::getFilesystem()->replace( |
| 75 | + BASE_PATH . '/composer.lock', |
| 76 | + json_encode($content, JSON_PRETTY_PRINT) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + protected static function loadConfigFromYaml(string $workingPath, ?string $filename = 'testbench.yaml', array $defaults = []): void |
| 81 | + { |
| 82 | + $filename = LazyCollection::make(static function () use ($filename) { |
| 83 | + yield $filename; |
| 84 | + yield "{$filename}.example"; |
| 85 | + yield "{$filename}.dist"; |
| 86 | + })->map(static function ($file) use ($workingPath) { |
| 87 | + return str_contains($file, DIRECTORY_SEPARATOR) ? $file : join_paths($workingPath, $file); |
| 88 | + })->filter(static fn ($file) => is_file($file)) |
| 89 | + ->first(); |
| 90 | + |
| 91 | + if (is_null($filename)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + static::$config = Yaml::parseFile($filename) ?? []; |
| 96 | + } |
| 97 | + |
| 98 | + protected static function generateEnv(): void |
| 99 | + { |
| 100 | + if (! $env = static::$config['env'] ?? []) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + static::getFilesystem()->replace( |
| 105 | + join_paths(BASE_PATH, '/.env'), |
| 106 | + implode(PHP_EOL, $env) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + protected static function getConfigProviders(): array |
| 111 | + { |
| 112 | + ConfigProviderRegister::add( |
| 113 | + static::$config['config-providers'] ?? [] |
| 114 | + ); |
| 115 | + |
| 116 | + return ConfigProviderRegister::get(); |
| 117 | + } |
| 118 | + |
| 119 | + protected static function registerPurgeFiles(): void |
| 120 | + { |
| 121 | + $purge = static::$config['purge'] ?? []; |
| 122 | + $files = $purge['files'] ?? []; |
| 123 | + $directories = $purge['directories'] ?? []; |
| 124 | + |
| 125 | + if (! $files && ! $directories) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + register_shutdown_function(function () use ($files, $directories) { |
| 130 | + $filesystem = static::getFilesystem(); |
| 131 | + foreach ($files as $file) { |
| 132 | + if (! $filesystem->exists($file = BASE_PATH . "/{$file}")) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + $filesystem->delete($file); |
| 136 | + } |
| 137 | + |
| 138 | + foreach ($directories as $directory) { |
| 139 | + if (! $filesystem->exists($directory = BASE_PATH . "/{$directory}")) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + $filesystem->deleteDirectory($directory); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
0 commit comments