|
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
|
11 |
|
public function __construct( |
|
22
|
|
|
private string $environment, |
|
23
|
|
|
) { |
|
24
|
11 |
|
$this->setUpContainer(); |
|
25
|
11 |
|
$this->setUpConsole(); |
|
26
|
11 |
|
} |
|
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
|
11 |
|
private function setUpContainer(): void |
|
39
|
|
|
{ |
|
40
|
11 |
|
$this->container = new ContainerBuilder(); |
|
41
|
11 |
|
$projectDir = dirname(__DIR__, 2); |
|
42
|
11 |
|
$loader = new YamlFileLoader($this->container, new FileLocator("{$projectDir}/config")); |
|
43
|
|
|
|
|
44
|
11 |
|
$loader->load(match ($this->environment) { |
|
45
|
11 |
|
self::ENV_PROD => 'services.yaml', |
|
46
|
11 |
|
self::ENV_TEST => 'services_test.yaml', |
|
47
|
11 |
|
default => throw GameKernelException::unknownEnvironment($this->environment), |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
11 |
|
$this->container->setParameter('kernel.project_dir', $projectDir); |
|
51
|
11 |
|
$this->container->compile(); |
|
52
|
11 |
|
} |
|
53
|
|
|
|
|
54
|
11 |
|
private function setUpConsole(): void |
|
55
|
|
|
{ |
|
56
|
11 |
|
$this->console = new Application(); |
|
57
|
11 |
|
$commands = $this->container->findTaggedServiceIds('console.command'); |
|
58
|
|
|
|
|
59
|
11 |
|
foreach ($commands as $commandId => $tags) { |
|
60
|
|
|
/** @var Command $command */ |
|
61
|
11 |
|
$command = $this->container->get($commandId); |
|
62
|
11 |
|
$this->console->add($command); |
|
63
|
|
|
} |
|
64
|
11 |
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|