1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality relating Symfony2 components such as the template engine, requests, and sessions |
4
|
|
|
* |
5
|
|
|
* @package BZiON |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use BZIon\Cache\ModelCache; |
10
|
|
|
use Pixie\Connection; |
11
|
|
|
use Pixie\QueryBuilder\QueryBuilderHandler; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
14
|
|
|
use Symfony\Component\Form\FormFactory; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Service |
22
|
|
|
*/ |
23
|
|
|
abstract class Service |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Symfony's URL Generator |
27
|
|
|
* @var UrlGeneratorInterface; |
28
|
|
|
*/ |
29
|
|
|
private static $generator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Symfony's Request class |
33
|
|
|
* @var Request |
34
|
|
|
*/ |
35
|
|
|
private static $request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Symfony's FormFactory |
39
|
|
|
* @var FormFactory |
40
|
|
|
*/ |
41
|
|
|
private static $formFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The kernel's environment (prod, debug, profile or test) |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private static $environment; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The model memory cache |
51
|
|
|
* @var ModelCache |
52
|
|
|
*/ |
53
|
|
|
private static $modelCache; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The AppKernel's container |
57
|
|
|
* @var AppKernel |
58
|
|
|
*/ |
59
|
|
|
private static $kernel; |
60
|
|
|
|
61
|
|
|
private static $qbConnection; |
62
|
23 |
|
private static $qbConfig; |
63
|
|
|
|
64
|
23 |
|
public static function setQueryBuilderConfig(array $config) |
65
|
23 |
|
{ |
66
|
|
|
self::$qbConfig = $config; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function getQueryBuilderConnection() |
70
|
23 |
|
{ |
71
|
|
|
if (!self::$qbConnection) { |
72
|
23 |
|
self::$qbConnection = new Connection('mysql', self::$qbConfig); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return self::$qbConnection; |
76
|
|
|
} |
77
|
|
|
|
78
|
23 |
|
/** |
79
|
|
|
* @param Request $request |
80
|
|
|
*/ |
81
|
|
|
public static function setRequest($request) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
self::$request = $request; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
/** |
87
|
|
|
* @return Request |
88
|
1 |
|
*/ |
89
|
1 |
|
public static function getRequest() |
90
|
|
|
{ |
91
|
|
|
if (!self::$request) { |
92
|
|
|
$request = Request::createFromGlobals(); |
93
|
|
|
$request->setSession(self::getNewSession()); |
94
|
1 |
|
self::setRequest($request); |
95
|
|
|
} |
96
|
1 |
|
|
97
|
|
|
return self::$request; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets the URL Generator. |
102
|
|
|
* @param UrlGeneratorInterface $generator |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
76 |
|
public static function setGenerator($generator) |
106
|
|
|
{ |
107
|
76 |
|
self::$generator = $generator; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return UrlGeneratorInterface |
112
|
|
|
*/ |
113
|
|
|
public static function getGenerator() |
114
|
|
|
{ |
115
|
|
|
return self::$generator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets a parameter |
120
|
|
|
* |
121
|
|
|
* @param string $name The parameter name |
122
|
|
|
* @return mixed The parameter value |
123
|
|
|
*/ |
124
|
|
|
public static function getParameter($name) |
125
|
|
|
{ |
126
|
|
|
return self::getContainer()->getParameter($name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param SessionInterface $session |
131
|
|
|
*/ |
132
|
|
|
public static function setSession($session) |
133
|
|
|
{ |
134
|
|
|
self::getRequest()->setSession($session); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return SessionInterface |
139
|
|
|
*/ |
140
|
|
|
public static function getSession() |
141
|
1 |
|
{ |
142
|
|
|
return self::getRequest()->getSession(); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Create a new session |
147
|
|
|
* @return Session |
148
|
|
|
*/ |
149
|
23 |
|
public static function getNewSession() |
150
|
|
|
{ |
151
|
23 |
|
$newSession = new Session(); |
152
|
23 |
|
$newSession->start(); |
153
|
|
|
|
154
|
|
|
return $newSession; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
/** |
158
|
|
|
* @return FormFactory |
159
|
1 |
|
*/ |
160
|
|
|
public static function getFormFactory() |
161
|
|
|
{ |
162
|
|
|
return self::$formFactory; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
/** |
166
|
|
|
* @param FormFactory $formFactory |
167
|
1 |
|
*/ |
168
|
1 |
|
public static function setFormFactory($formFactory) |
169
|
|
|
{ |
170
|
|
|
self::$formFactory = $formFactory; |
171
|
|
|
} |
172
|
|
|
|
173
|
76 |
|
/** |
174
|
|
|
* @return string |
175
|
76 |
|
*/ |
176
|
|
|
public static function getEnvironment() |
177
|
|
|
{ |
178
|
|
|
return self::$environment; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
/** |
182
|
|
|
* @param string $environment |
183
|
1 |
|
*/ |
184
|
1 |
|
public static function setEnvironment($environment) |
185
|
|
|
{ |
186
|
|
|
self::$environment = $environment; |
187
|
|
|
} |
188
|
|
|
|
189
|
76 |
|
/** |
190
|
|
|
* @return ModelCache |
191
|
76 |
|
*/ |
192
|
|
|
public static function getModelCache() |
193
|
|
|
{ |
194
|
|
|
return self::$modelCache; |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
/** |
198
|
|
|
* @param ModelCache $modelCache |
199
|
1 |
|
*/ |
200
|
1 |
|
public static function setModelCache($modelCache) |
201
|
|
|
{ |
202
|
|
|
self::$modelCache = $modelCache; |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
/** |
206
|
|
|
* @return string[][] |
207
|
1 |
|
*/ |
208
|
|
|
public static function getSiteThemes() |
209
|
|
|
{ |
210
|
|
|
return self::getParameter('bzion.site.themes'); |
211
|
|
|
} |
212
|
|
|
|
213
|
76 |
|
/** |
214
|
|
|
* @return string |
215
|
76 |
|
*/ |
216
|
|
|
public static function getDefaultSiteTheme() |
217
|
|
|
{ |
218
|
|
|
$themes = self::getSiteThemes(); |
219
|
|
|
|
220
|
|
|
return $themes[0]['slug']; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return ContainerInterface |
225
|
|
|
*/ |
226
|
|
|
public static function getContainer() |
227
|
|
|
{ |
228
|
|
|
return self::$kernel->getContainer(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param $kernel |
233
|
|
|
*/ |
234
|
|
|
public static function setKernel($kernel) |
235
|
|
|
{ |
236
|
|
|
self::$kernel = $kernel; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return EventDispatcher |
241
|
|
|
*/ |
242
|
|
|
public static function getDispatcher() |
243
|
|
|
{ |
244
|
|
|
return self::getContainer()->get('event_dispatcher'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public static function isDebug() |
251
|
|
|
{ |
252
|
|
|
return self::getParameter('kernel.debug'); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|