Lines 100.00% 22 / 22
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 createApplication 100.00% 4 / 4 100.00% 1 / 1 1
 compileContainer 100.00% 1 / 1 100.00% 1 / 1 1
 buildSyntheticServicesForProject 100.00% 5 / 5 100.00% 1 / 1 1
 buildNamedArgumentsForProject 100.00% 4 / 4 100.00% 1 / 1 1
 locateConsoleScript 100.00% 2 / 2 100.00% 1 / 1 2
 detectInstalledVersion 100.00% 5 / 5 100.00% 1 / 1 2
17final readonly class ConsoleKernel
18{
19    private const string APPS_DIRECTORY = '/apps';
20
21    private const string SERVICES_FILE = '/services.php';
22
23    private const string CONSOLE_SCRIPT = '/bin/stewart';
24
25    private const string RUNTIME_PACKAGE = 'stewart-php/runtime';
26
27    private const string UNKNOWN_VERSION = 'unknown';
28
29    /** @param array<string, array<int, string>> $psr4 */
30    public function __construct(
31        private string $projectDir,
32        private array $psr4,
33        private SyntheticServices $overrides = new SyntheticServices(),
34    ) {}
35
36    public function createApplication(): StewartApplication
37    {
38        $container = $this->compileContainer();
39
40        $application = new StewartApplication($this->detectInstalledVersion()->value);
41        $application->setCommandLoader($container->resolveService(CommandLoaderInterface::class, 'console.command_loader'));
42
43        return $application;
44    }
45
46    public function compileContainer(): TypedContainer
47    {
48        return new ProfileCompiler()->compileProfileContainer(ContainerProfile::Console, $this->buildSyntheticServicesForProject(), $this->buildNamedArgumentsForProject());
49    }
50
51    private function buildSyntheticServicesForProject(): SyntheticServices
52    {
53        return new SyntheticServices()
54            ->withService(ProjectRoot::class, new ProjectRoot($this->projectDir))
55            ->withService(Psr4Map::class, new Psr4Map($this->psr4))
56            ->withService(StewartVersion::class, $this->detectInstalledVersion())
57            ->withOverrides($this->overrides);
58    }
59
60    private function buildNamedArgumentsForProject(): NamedArguments
61    {
62        return new NamedArguments()
63            ->withArgument('appsDirectory', $this->projectDir . self::APPS_DIRECTORY)
64            ->withArgument('userServicesFile', $this->projectDir . self::SERVICES_FILE)
65            ->withArgument('stewartConsoleScript', $this->locateConsoleScript());
66    }
67
68    // The project's own bin proxy; a symlinked package's bin/stewart would load the autoloader next to the package.
69    private function locateConsoleScript(): string
70    {
71        $classLoaderFile = new ReflectionClass(ClassLoader::class)->getFileName();
72
73        return \dirname($classLoaderFile === false ? $this->projectDir . '/vendor/composer/ClassLoader.php' : $classLoaderFile, 2) . self::CONSOLE_SCRIPT;
74    }
75
76    private function detectInstalledVersion(): StewartVersion
77    {
78        return new StewartVersion(
79            InstalledVersions::isInstalled(self::RUNTIME_PACKAGE)
80                ? InstalledVersions::getPrettyVersion(self::RUNTIME_PACKAGE) ?? self::UNKNOWN_VERSION
81                : self::UNKNOWN_VERSION,
82        );
83    }
84}