1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AardsGerds\Game\Infrastructure; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
12
|
|
|
|
13
|
|
|
final class GameKernel |
14
|
|
|
{ |
15
|
|
|
public const ENV_PROD = 'prod'; |
16
|
|
|
public const ENV_TEST = 'test'; |
17
|
|
|
|
18
|
|
|
private ContainerBuilder $container; |
19
|
|
|
private Application $console; |
20
|
|
|
|
21
|
2 |
|
public function __construct( |
22
|
|
|
private string $environment, |
23
|
|
|
) { |
24
|
2 |
|
$this->setUpContainer(); |
25
|
2 |
|
$this->setUpConsole(); |
26
|
2 |
|
} |
27
|
|
|
|
28
|
|
|
public function runConsole(): void |
29
|
|
|
{ |
30
|
|
|
$this->console->run(); |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function getContainer(): ContainerBuilder |
34
|
|
|
{ |
35
|
2 |
|
return $this->container; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
private function setUpContainer(): void |
39
|
|
|
{ |
40
|
2 |
|
$this->container = new ContainerBuilder(); |
41
|
2 |
|
$projectDir = dirname(__DIR__, 2); |
42
|
2 |
|
$loader = new YamlFileLoader($this->container, new FileLocator("{$projectDir}/config")); |
43
|
|
|
|
44
|
2 |
|
$loader->load(match ($this->environment) { |
45
|
2 |
|
self::ENV_PROD => 'services.yaml', |
46
|
2 |
|
self::ENV_TEST => 'services_test.yaml', |
47
|
2 |
|
default => throw GameKernelException::unknownEnvironment($this->environment), |
48
|
|
|
}); |
49
|
|
|
|
50
|
2 |
|
$this->container->setParameter('kernel.project_dir', $projectDir); |
51
|
2 |
|
$this->container->compile(); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
private function setUpConsole(): void |
55
|
|
|
{ |
56
|
2 |
|
$this->console = new Application(); |
57
|
2 |
|
$commands = $this->container->findTaggedServiceIds('console.command'); |
58
|
|
|
|
59
|
2 |
|
foreach ($commands as $commandId => $tags) { |
60
|
|
|
/** @var Command $command */ |
61
|
2 |
|
$command = $this->container->get($commandId); |
62
|
2 |
|
$this->console->add($command); |
63
|
|
|
} |
64
|
2 |
|
} |
65
|
|
|
} |
66
|
|
|
|