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