Lines
70.58%
12 / 17
Functions and Methods
80.00%
4 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ConsoleLauncher | 70.58% | 12 / 17 | 80.00% | 4 / 5 | 18.99 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| createForInstalledProject | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| findRootInstallPath | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 7 | |||||
| launchConsole | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 6 | |||||
| loadEnvironmentFile | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Kernel; | |
| 6 | ||
| 7 | use Composer\Autoload\ClassLoader; | |
| 8 | use Composer\InstalledVersions; | |
| 9 | use Symfony\Component\Dotenv\Dotenv; | |
| 10 | use Symfony\Component\Dotenv\Exception\ExceptionInterface as DotenvException; | |
| 11 | ||
| 12 | final readonly class ConsoleLauncher | |
| 13 | { | |
| 14 | private const string ENVIRONMENT_FILE = '/.env'; | |
| 15 | ||
| 16 | public function __construct( | |
| 17 | public string $projectDir, | |
| 18 | private ClassLoader $loader, | |
| 19 | ) {} | |
| 20 | ||
| 21 | public static function createForInstalledProject(ClassLoader $loader): self | |
| 22 | { | |
| 23 | $installPath = self::findRootInstallPath($loader) ?? InstalledVersions::getRootPackage()['install_path']; | |
| 24 | ||
| 25 | return new self(realpath($installPath) ?: $installPath, $loader); | |
| 26 | } | |
| 27 | ||
| 28 | // InstalledVersions answers for the first Composer autoloader in the process, which can be one inside a PHAR. | |
| 29 | private static function findRootInstallPath(ClassLoader $loader): ?string | |
| 30 | { | |
| 31 | $vendorDir = array_search($loader, ClassLoader::getRegisteredLoaders(), true); | |
| 32 | $installedFile = \is_string($vendorDir) ? $vendorDir . '/composer/installed.php' : null; | |
| 33 | $installed = $installedFile !== null && is_file($installedFile) ? require $installedFile : null; | |
| 34 | $root = \is_array($installed) ? $installed['root'] ?? null : null; | |
| 35 | $installPath = \is_array($root) ? $root['install_path'] ?? null : null; | |
| 36 | ||
| 37 | return \is_string($installPath) ? $installPath : null; | |
| 38 | } | |
| 39 | ||
| 40 | public function launchConsole(): int | |
| 41 | { | |
| 42 | try { | |
| 43 | $this->loadEnvironmentFile(); | |
| 44 | } catch (DotenvException $e) { | |
| 45 | fwrite(\STDERR, \sprintf("Could not read %s: %s\n", $this->projectDir . self::ENVIRONMENT_FILE, $e->getMessage())); | |
| 46 | ||
| 47 | return 1; | |
| 48 | } | |
| 49 | ||
| 50 | return new ConsoleKernel($this->projectDir, $this->loader->getPrefixesPsr4())->createApplication()->run(); | |
| 51 | } | |
| 52 | ||
| 53 | /** @throws DotenvException */ | |
| 54 | public function loadEnvironmentFile(): void | |
| 55 | { | |
| 56 | $file = $this->projectDir . self::ENVIRONMENT_FILE; | |
| 57 | ||
| 58 | if (is_file($file)) { | |
| 59 | new Dotenv()->usePutenv()->load($file); | |
| 60 | } | |
| 61 | } | |
| 62 | } |