|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Core; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
|
11
|
|
|
use allejo\stakx\DataTransformer\DataTransformer; |
|
12
|
|
|
use allejo\stakx\DependencyInjection\Compiler\DataTransformerPass; |
|
13
|
|
|
use allejo\stakx\Filesystem\FilesystemPath; |
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
|
15
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
16
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The base application class for stakx. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Application extends BaseApplication |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var bool */ |
|
31
|
|
|
private $safeMode; |
|
32
|
|
|
/** @var bool */ |
|
33
|
|
|
private $useCache; |
|
34
|
|
|
/** @var Container */ |
|
35
|
|
|
private $container; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$input = new ArgvInput(); |
|
|
|
|
|
|
43
|
|
|
$this->handleApplicationFlags($input); |
|
44
|
|
|
|
|
45
|
|
|
$this->loadContainer([ |
|
46
|
|
|
'parameters' => [ |
|
47
|
|
|
'root_dir' => __DIR__ . '/../', |
|
48
|
|
|
], |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$output = $this->getContainer()->get('output'); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if (extension_loaded('xdebug') && !getenv('STAKX_DISABLE_XDEBUG_WARN')) |
|
54
|
|
|
{ |
|
55
|
|
|
$output->writeln('<fg=black;bg=yellow>You are running Stakx with xdebug enabled. This has a major impact on runtime performance.</>'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return parent::run($input, $output); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/// |
|
62
|
|
|
// Application Settings |
|
63
|
|
|
/// |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get whether or not the application is being run in safe mode. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function inSafeMode() |
|
71
|
|
|
{ |
|
72
|
|
|
return (bool)$this->safeMode; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set safe mode for the application. |
|
77
|
|
|
* |
|
78
|
|
|
* @param bool $safeMode |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setSafeMode($safeMode) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->safeMode = $safeMode; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get whether or not to look for and use the application cache. |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function useCache() |
|
91
|
|
|
{ |
|
92
|
|
|
return (bool)$this->useCache; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set whether or not to use an existing cache. |
|
97
|
|
|
* |
|
98
|
|
|
* @param bool $useCache |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setUseCache($useCache) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->useCache = $useCache; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Handle application wide flags. |
|
107
|
|
|
* |
|
108
|
|
|
* @param InputInterface $input |
|
109
|
|
|
*/ |
|
110
|
|
|
private function handleApplicationFlags(InputInterface $input) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->setUseCache($input->hasParameterOption('--use-cache')); |
|
113
|
|
|
$this->setSafeMode($input->hasParameterOption('--safe')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/// |
|
117
|
|
|
// Container Settings |
|
118
|
|
|
/// |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the Service container. |
|
122
|
|
|
* |
|
123
|
|
|
* @return Container |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getContainer() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->container; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Load the cached application container or build a new one. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $containerOptions |
|
134
|
|
|
*/ |
|
135
|
|
|
private function loadContainer(array $containerOptions) |
|
136
|
|
|
{ |
|
137
|
|
|
$cachedContainerPath = new FilesystemPath(getcwd() . '/' . Configuration::CACHE_FOLDER . '/container-cache.php'); |
|
138
|
|
|
|
|
139
|
|
|
if (!$this->useCache() || !file_exists($cachedContainerPath)) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->makeCacheDir(); |
|
142
|
|
|
$this->buildContainer($cachedContainerPath, $containerOptions); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
require $cachedContainerPath; |
|
146
|
|
|
|
|
147
|
|
|
$this->container = new \ProjectServiceContainer(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Build and compile the application container. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $cachePath |
|
154
|
|
|
* @param array $containerOptions |
|
155
|
|
|
*/ |
|
156
|
|
|
private function buildContainer($cachePath, array $containerOptions) |
|
157
|
|
|
{ |
|
158
|
|
|
$container = new ContainerBuilder(); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($containerOptions['parameters'] as $key => $value) |
|
161
|
|
|
{ |
|
162
|
|
|
$container->setParameter($key, $value); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../app/')); |
|
166
|
|
|
$loader->load('services.yml'); |
|
167
|
|
|
|
|
168
|
|
|
$container |
|
169
|
|
|
->addCompilerPass(new RegisterListenersPass()) |
|
170
|
|
|
->addCompilerPass(new DataTransformerPass()) |
|
171
|
|
|
|
|
172
|
|
|
->registerForAutoconfiguration(DataTransformer::class) |
|
173
|
|
|
->addTag(DataTransformer::CONTAINER_TAG) |
|
174
|
|
|
; |
|
175
|
|
|
|
|
176
|
|
|
$container->compile(); |
|
177
|
|
|
$dumper = new PhpDumper($container); |
|
178
|
|
|
|
|
179
|
|
|
file_put_contents($cachePath, $dumper->dump()); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Create a cache directory if it doesn't exist. |
|
184
|
|
|
*/ |
|
185
|
|
|
private function makeCacheDir() |
|
186
|
|
|
{ |
|
187
|
|
|
$cachedFolder = new FilesystemPath(getcwd() . '/' . Configuration::CACHE_FOLDER); |
|
188
|
|
|
|
|
189
|
|
|
if (!file_exists($cachedFolder)) |
|
190
|
|
|
{ |
|
191
|
|
|
mkdir($cachedFolder); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|