1 | <?php |
||
13 | class Bootstrap |
||
14 | { |
||
15 | |||
16 | const DEFAULT_CONFIG_FILE = 'config.neon'; |
||
17 | |||
18 | /** @var BuildFactory */ |
||
19 | private $buildFactory; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $workingDir; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * @return BuildFactory |
||
27 | */ |
||
28 | public function getBuildFactory() |
||
29 | { |
||
30 | return $this->buildFactory; |
||
31 | } |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @param BuildFactory $buildFactory |
||
36 | */ |
||
37 | 14 | public function setBuildFactory($buildFactory) |
|
38 | { |
||
39 | 14 | $this->buildFactory = $buildFactory; |
|
40 | 14 | } |
|
41 | |||
42 | |||
43 | /** |
||
44 | * @param InputArgs $inputArgs |
||
45 | * @throws TerminateException |
||
46 | */ |
||
47 | 14 | public function run(InputArgs $inputArgs) |
|
48 | { |
||
49 | 14 | $this->startup($inputArgs); |
|
50 | 13 | $arguments = $inputArgs->getArguments(); |
|
51 | 13 | if (isset($arguments[0]) && $arguments[0] === 'self-init') { |
|
52 | 1 | $directoryName = isset($arguments[1]) ? $arguments[1] : 'build'; |
|
53 | 1 | $selfInit = new Commands\SelfInit(); |
|
54 | 1 | $selfInit->setDistDirectory(__DIR__ . '/build-dist'); |
|
55 | 1 | $selfInit->setWorkingDirectory($this->workingDir); |
|
56 | 1 | $selfInit->setDirname($directoryName); |
|
57 | 1 | $selfInit->execute(); |
|
58 | 1 | $this->terminate(0); |
|
59 | } |
||
60 | 12 | $container = $this->resolveBootstrap(); |
|
61 | 11 | $configFile = $inputArgs->getOption('config') ? $inputArgs->getOption('config') : self::DEFAULT_CONFIG_FILE; |
|
62 | try { |
||
63 | 11 | $container = $this->createContainer($configFile, $container); |
|
64 | 11 | $build = $this->createBuild($container, $arguments); |
|
65 | 11 | } catch (\Throwable $e) { // fault barrier -> catch all |
|
66 | $this->handleException($e); |
||
67 | 2 | } catch (\Exception $e) { // fault barrier -> catch all, PHP 5.x compatibility |
|
68 | 2 | $this->handleException($e); |
|
69 | } |
||
70 | 9 | if (count($arguments) < 1) { |
|
71 | 2 | $this->log("Running default", 'green'); |
|
72 | 2 | $build->runDefault(); |
|
73 | 2 | $this->terminate(0); |
|
74 | } |
||
75 | |||
76 | 7 | $method = 'run' . str_replace('-', '', ucfirst($arguments[0])); |
|
77 | 7 | if (!method_exists($build, $method)) { |
|
78 | 1 | $this->log("Task '$arguments[0]' does not exists.", 'red'); |
|
79 | 1 | $this->terminate(255); |
|
80 | } |
||
81 | 6 | $this->log("Running [$arguments[0]]", 'green'); |
|
82 | try { |
||
83 | 6 | $build->$method(); |
|
84 | 7 | } catch (\Throwable $e) { // fault barrier -> catch all |
|
85 | 1 | $this->handleException($e); |
|
86 | 1 | } catch (\Exception $e) { // fault barrier -> catch all, PHP 5.x compatibility |
|
87 | 1 | $this->handleException($e); |
|
88 | } |
||
89 | 5 | $this->log("Exited with SUCCESS", 'black', 'green'); |
|
90 | 5 | echo PHP_EOL; |
|
91 | 5 | $this->terminate(0); |
|
92 | } |
||
93 | |||
94 | |||
95 | 14 | private function terminate($code) |
|
99 | |||
100 | |||
101 | 14 | private function startup(InputArgs $inputArgs) |
|
119 | |||
120 | |||
121 | /** |
||
122 | * @return Container|NULL |
||
123 | */ |
||
124 | 12 | private function resolveBootstrap() |
|
141 | |||
142 | |||
143 | /** |
||
144 | * @param $configFile |
||
145 | * @param Container|NULL $bootstrapContainer |
||
146 | * @return Container |
||
147 | */ |
||
148 | 11 | private function createContainer($configFile, Container $bootstrapContainer = NULL) |
|
161 | |||
162 | |||
163 | /** |
||
164 | * @param Container $container |
||
165 | * @return Build |
||
166 | */ |
||
167 | 11 | private function createBuild(Container $container, array $arguments = NULL) |
|
174 | |||
175 | |||
176 | /** |
||
177 | * @param \Exception|\Throwable $e |
||
178 | * @throws TerminateException |
||
179 | */ |
||
180 | 3 | private function handleException($e) |
|
187 | |||
188 | |||
189 | /** |
||
190 | * @param string $message |
||
191 | * @param string|null $color |
||
192 | * @param string|null $backgroundColor |
||
193 | */ |
||
194 | 13 | private function log($message, $color = NULL, $backgroundColor = NULL) |
|
198 | |||
199 | } |