-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKernel.php
More file actions
92 lines (77 loc) · 3.04 KB
/
Copy pathKernel.php
File metadata and controls
92 lines (77 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
* Copyright (c) Default Value LLC.
* This source file is subject to the License https://github.com/DefaultValue/dockerizer_for_php/LICENSE.txt
* Do not change this file if you want to upgrade the tool to the newer versions in the future
* Please, contact us at https://default-value.com/#contact if you wish to customize this tool
* according to you business needs
*/
namespace DefaultValue\Dockerizer;
use DefaultValue\Dockerizer\Console\Helper\QuestionHelper;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Dotenv\Dotenv;
class Kernel
{
private static ContainerBuilder $containerBuilder;
/**
* @param array $configDirectories
* @return Application
* @throws \Exception
*/
public function getApplication(array $configDirectories): Application
{
$this->initEnv();
$containerBuilder = $this->boot($configDirectories);
/** @var CommandLoaderInterface $commandLoader */
$commandLoader = $containerBuilder->get('console.command_loader');
$application = new Application();
$application->setCommandLoader($commandLoader);
$application->getHelperSet()->set(new QuestionHelper());
// @TODO: may be needed to get exception info for parallel runs
// $application->setCatchExceptions(false);
return $application;
}
/**
* @return void
*/
private function initEnv(): void
{
$dotenv = new Dotenv();
$dotenv->usePutenv();
// @TODO: usePutenv() is deprecated since Symfony 5.1, but env vars need to be accessible via getenv()
// for services like AWS SDK. Consider migrating to $_ENV/$_SERVER access in the future.
$dotenv->load(__DIR__ . '/../.env.dist');
if (is_file(__DIR__ . '/../.env.local')) {
$dotenv->load(__DIR__ . '/../.env.local');
}
}
/**
* @param array $configDirectories
* @return ContainerBuilder
* @throws \Exception
*/
private function boot(array $configDirectories): ContainerBuilder
{
$fileLocator = new FileLocator($configDirectories);
$containerBuilder = $this->getContainerBuilder();
$yamlFileLoader = new YamlFileLoader($containerBuilder, $fileLocator);
$yamlFileLoader->load('services.yaml');
$containerBuilder->addCompilerPass(new AddConsoleCommandPass());
$containerBuilder->setParameter('kernel.project_dir', dirname(__DIR__) . DIRECTORY_SEPARATOR);
$containerBuilder->compile();
return $containerBuilder;
}
/**
* @return ContainerBuilder
*/
private function getContainerBuilder(): ContainerBuilder
{
self::$containerBuilder ??= new ContainerBuilder();
return self::$containerBuilder;
}
}