Lines
54.54%
6 / 11
Functions and Methods
75.00%
3 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ConsoleLauncher | 54.54% | 6 / 11 | 75.00% | 3 / 4 | 11.60 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| createForInstalledProject | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| 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 = InstalledVersions::getRootPackage()['install_path']; | |
| 24 | ||
| 25 | return new self(realpath($installPath) ?: $installPath, $loader); | |
| 26 | } | |
| 27 | ||
| 28 | public function launchConsole(): int | |
| 29 | { | |
| 30 | try { | |
| 31 | $this->loadEnvironmentFile(); | |
| 32 | } catch (DotenvException $e) { | |
| 33 | fwrite(\STDERR, \sprintf("Could not read %s: %s\n", $this->projectDir . self::ENVIRONMENT_FILE, $e->getMessage())); | |
| 34 | ||
| 35 | return 1; | |
| 36 | } | |
| 37 | ||
| 38 | return new ConsoleKernel($this->projectDir, $this->loader->getPrefixesPsr4())->createApplication()->run(); | |
| 39 | } | |
| 40 | ||
| 41 | /** @throws DotenvException */ | |
| 42 | public function loadEnvironmentFile(): void | |
| 43 | { | |
| 44 | $file = $this->projectDir . self::ENVIRONMENT_FILE; | |
| 45 | ||
| 46 | if (is_file($file)) { | |
| 47 | new Dotenv()->usePutenv()->load($file); | |
| 48 | } | |
| 49 | } | |
| 50 | } |