-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBootstrap.php
More file actions
126 lines (108 loc) · 3.81 KB
/
Bootstrap.php
File metadata and controls
126 lines (108 loc) · 3.81 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Hypernode\Deploy;
use Composer\InstalledVersions;
use DI\Container;
use DI\ContainerBuilder;
use Exception;
use Hypernode\Deploy\Stdlib\ClassFinder;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class Bootstrap
{
private const APP_LOGO = <<<NAME
__ __ __ ____ __
/ / / /_ ______ ___ _________ ____ ____/ /__ / __ \___ ____ / /___ __ __
/ /_/ / / / / __ \/ _ \/ ___/ __ \/ __ \/ __ / _ \ / / / / _ \/ __ \/ / __ \/ / / /
/ __ / /_/ / /_/ / __/ / / / / / /_/ / /_/ / __/ / /_/ / __/ /_/ / / /_/ / /_/ /
/_/ /_/\__, / .___/\___/_/ /_/ /_/\____/\__,_/\___/ /_____/\___/ .___/_/\____/\__, /
/____/_/ /_/ /____/
Deployer version: %s
NAME;
/**
* Run application
*
* @throws Exception
*/
public function run(): int
{
$container = $this->initContainer();
$application = $this->initApplication($container);
return $application->run(
$container->get(InputInterface::class),
$container->get(OutputInterface::class)
);
}
/**
* @throws InvalidArgumentException
* @throws Exception
*/
private function initContainer(): Container
{
$builder = new ContainerBuilder();
$builder->useAutowiring(true);
$builder->useAnnotations(true);
$builder->addDefinitions(Di\ConsoleDefinition::getDefinition());
$builder->addDefinitions([
'version' => $this->getVersion(),
]);
$container = $builder->build();
if (!defined('DEPLOYER_VERSION')) {
define("DEPLOYER_VERSION", sprintf("Hypernode Deploy %s", $this->getVersion()));
}
$this->registerTwigLoader($container);
return $container;
}
private function initApplication(Container $container): Application
{
$application = new Application();
$application->getDefinition()->addOption(
new InputOption(
'--file',
'-f',
InputOption::VALUE_OPTIONAL,
'Specify configuration file',
'deploy.php'
)
);
$application->setName(
sprintf(
self::APP_LOGO,
InstalledVersions::getVersion('deployer/deployer')
)
);
$application->setVersion('Version: ' . $this->getVersion());
$this->addCommandsToApplication($container, $application);
return $application;
}
private function registerTwigLoader(Container $container): void
{
$loader = new FilesystemLoader(__DIR__ . '/Resource/template');
$twig = new Environment($loader, ['autoescape' => false]);
$container->set(Environment::class, $twig);
}
/**
* @throws InvalidArgumentException
*/
private function addCommandsToApplication(ContainerInterface $container, Application $application): void
{
$finder = new ClassFinder(__NAMESPACE__ . '\\Command');
$finder->in(__DIR__ . DIRECTORY_SEPARATOR . 'Command');
$finder->subclassOff(Command::class);
foreach ($finder->getClasses() as $class) {
/** @var Command $command */
$command = $container->get($class);
$application->add($command);
}
}
private function getVersion(): string
{
return '@git_version@ @build_datetime@';
}
}