forked from jarves/jarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JarvesBundle.php
332 lines (275 loc) · 13.2 KB
/
JarvesBundle.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <[email protected]>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
use Jarves\Cache\Cacher;
use Jarves\Configuration\Connection;
use Jarves\Configuration\Database;
use Jarves\Configuration\EntryPoint;
use Jarves\DependencyInjection\AssetCompilerCompilerPass;
use Jarves\DependencyInjection\ModelBuilderCompilerPass;
use Propel\Runtime\Connection\ConnectionManagerMasterSlave;
use Propel\Runtime\Connection\ConnectionManagerSingle;
use Propel\Runtime\Propel;
use Propel\Runtime\ServiceContainer\StandardServiceContainer;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class JarvesBundle extends Bundle
{
/**
* @var boolean
*/
protected $booted = false;
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ModelBuilderCompilerPass());
$container->addCompilerPass(new AssetCompilerCompilerPass());
//necessary to get fos_rest_bundle working
$container->loadFromExtension('framework', [
'serializer' => ['enabled' => true]
]);
$container->loadFromExtension('security', [
'encoders' => ['Jarves\Model\User' => 'bcrypt'],
'providers' => ['jarves' => ['id' => 'jarves.user_provider']]
]);
if (!$container->hasParameter('jarves_admin_prefix')) {
$container->setParameter('jarves_admin_prefix', '/jarves');
}
if (!$container->hasParameter('database_driver')) {
//newer Symfony versions do not have database_driver anymore. Especially from a fresh
//installation when `composer install` is used Symfony creates parameters.yml
//with database credentials. Propel needs however a database driver, so we make
//sure we have one. This is only being used when now <database> section in the
//config.jarves.xml is given.
$container->setParameter('database_driver', 'mysql');
}
parent::build($container);
}
public function boot()
{
parent::boot();
$this->configure();
if (function_exists('ppm_log')) {
//In an environment like PPM with several workers Propel's not distributed cache will
//lead to inconsistent states across all workers, so we need to disable it here completely.
//Jarves already caches using a distributed cache where all workers are notified when
//a change changes, so we don't really need Propel's cache here.
Propel::disableInstancePooling();
}
if (!$this->booted) {
/** @var ContainerInterface $container */
$container = $this->container;
/** @var $jarves Jarves */
$jarves = $container->get('jarves');
/** @var JarvesConfig $jarvesConfig */
$jarvesConfig = $container->get('jarves.config');
$twig = $container->get('twig');
$twig->addGlobal('jarves_content_render', $container->get('jarves.content.render'));
$twig->addGlobal('pageStack', $container->get('jarves.page_stack'));
if ($jarvesConfig->getSystemConfig()->getLogs(true)->isActive()) {
/** @var $logger \Symfony\Bridge\Monolog\Logger */
$logger = $container->get('logger');
$logger->pushHandler($container->get('jarves.logger.handler'));
}
}
$this->booted = true;
}
/**
* Configures jarves, reads config files. Necessary when .xml configs changed.
*/
public function configure()
{
/** @var ContainerInterface $container */
$container = $this->container;
/** @var $jarves Jarves */
$jarves = $container->get('jarves');
/** @var Cacher $cacher */
$cacher = $container->get('jarves.cache.cacher');
/** @var JarvesConfig $jarvesConfig */
$jarvesConfig = $container->get('jarves.config');
/** @var $jarvesEventDispatcher JarvesEventDispatcher */
$jarvesEventDispatcher = $container->get('jarves.event_dispatcher');
$bootNeededCallback = $jarves->loadBundleConfigs($cacher);
$this->registerContentTypes($jarves, $container);
$this->registerFieldTypes($jarves, $container);
if ($bootNeededCallback) {
$jarves->getConfigs()->boot();
$this->setupObjects($jarves->getConfigs()->getConfigs());
$bootNeededCallback();
}
$jarvesEventDispatcher->registerBundleEvents($jarves->getConfigs());
}
/**
* Setup AutoCrud
*
* @param \Jarves\Configuration\Bundle[] $bundleConfigs
*/
protected function setupObjects(array $bundleConfigs) {
$objectsEntryPoint = $bundleConfigs['jarves']->getEntryPoint('object');
foreach ($bundleConfigs as $bundleConfig) {
/** @var EntryPoint[][] $objectEntryPoints */
$objectEntryPoints = [];
//read all entry points for each object
if ($bundleConfig->getEntryPoints()) {
foreach ($bundleConfig->getEntryPoints() as $entryPoint) {
if ($entryPoint->getObject()) {
$objectEntryPoints[$entryPoint->getObject()][$entryPoint->getType()] = $entryPoint;
}
}
}
$entryPointsForThisBundle = [];
if ($bundleConfig->getObjects()) {
foreach ($bundleConfig->getObjects() as $object) {
if (!isset($objectEntryPoints[$object->getKey()])) {
//this object does not have any entry point to manage.
if (!$object->getAutoCrud()) {
//jarves should not autobuild entry points
continue;
}
$entryPoint = new EntryPoint();
$entryPoint->setPath(lcfirst($object->getId()));
$entryPoint->setType('combine');
$entryPoint->setObject($object->getKey());
$entryPoint->setLink(true);
$entryPoint->setIcon('#' . ($object->isNested() ? 'icon-list-nested' : 'icon-list'));
$entryPoint->setLabel(($object->getLabel() ?: $object->getKey()));
$entryPointsForThisBundle[] = $entryPoint;
$objectEntryPoints[$entryPoint->getObject()][$entryPoint->getType()] = $entryPoint;
}
}
}
if ($entryPointsForThisBundle) {
//we added some autoCrud entry points
$bundleObjectContainerEntryPoint = new EntryPoint();
$bundleObjectContainerEntryPoint->setPath($bundleConfig->getName());
$bundleObjectContainerEntryPoint->setLink(true);
$bundleObjectContainerEntryPoint->setLabel($bundleConfig->getLabel() ?: $bundleConfig->getBundleName());
$objectsEntryPoint->addChildren($bundleObjectContainerEntryPoint);
foreach ($entryPointsForThisBundle as $entryPoint) {
$bundleObjectContainerEntryPoint->addChildren($entryPoint);
}
}
if ($bundleConfig->getObjects()) {
foreach ($bundleConfig->getObjects() as $object) {
//setup addEntrypoint, editEntrypoint, listEntrypoint if not set already
if (isset($objectEntryPoints[$object->getKey()]['combine'])) {
if (!$object->getAddEntryPoint()) {
$object->setAddEntryPoint($objectEntryPoints[$object->getKey()]['combine']->getFullPath());
}
if (!$object->getEditEntryPoint()) {
$object->setEditEntryPoint($objectEntryPoints[$object->getKey()]['combine']->getFullPath());
}
if (!$object->getListEntryPoint()) {
$object->setListEntryPoint($objectEntryPoints[$object->getKey()]['combine']->getFullPath());
}
}
if ($object->getAddEntryPoint() && isset($objectEntryPoints[$object->getKey()]['add'])) {
$object->setAddEntryPoint($objectEntryPoints[$object->getKey()]['add']->getFullPath());
}
if ($object->getEditEntryPoint() && isset($objectEntryPoints[$object->getKey()]['edit'])) {
$object->setEditEntryPoint($objectEntryPoints[$object->getKey()]['edit']->getFullPath());
}
if ($object->getListEntryPoint() && isset($objectEntryPoints[$object->getKey()]['list'])) {
$object->setListEntryPoint($objectEntryPoints[$object->getKey()]['list']->getFullPath());
}
}
}
}
}
protected function registerFieldTypes(Jarves $jarves, ContainerInterface $container)
{
foreach ($jarves->getConfigs() as $bundleConfig) {
if ($bundleConfig->getFieldTypes()) {
foreach ($bundleConfig->getFieldTypes() as $fieldType) {
if ($fieldType->isUserInterfaceOnly()) {
continue;
}
if (!$fieldType->getService()) {
throw new \RuntimeException(sprintf(
'For field type %s:%s is no service defined. If it does not handle model related persisting, ' .
'you should add interface-only="true"',
$bundleConfig->getName(),
$fieldType->getId()
));
}
if (!$container->has($fieldType->getService())) {
throw new \RuntimeException(sprintf(
'Service `%s` for field type %s:%s does not exist',
$fieldType->getService(),
$bundleConfig->getName(),
$fieldType->getId()
));
}
$jarves->getFieldTypes()->addType(
$fieldType->getId(),
$fieldType->getService()
);
}
}
}
}
protected function registerContentTypes(Jarves $jarves, ContainerInterface $container)
{
/** @var ContentRender $jarvesContentRender */
$jarvesContentRender = $container->get('jarves.content.render');
foreach ($jarves->getConfigs() as $bundleConfig) {
if ($bundleConfig->getContentTypes()) {
foreach ($bundleConfig->getContentTypes() as $contentType) {
if ('stopper' === $contentType->getId()) {
continue;
}
if (!$contentType->getService()) {
throw new \RuntimeException(sprintf(
'For content type %s:%s is no service defined',
$bundleConfig->getName(),
$contentType->getId()
));
}
if (!$container->has($contentType->getService())) {
throw new \RuntimeException(sprintf(
'Service `%s` for content type %s:%s does not exist',
$contentType->getService(),
$bundleConfig->getName(),
$contentType->getId()
));
}
$instance = $container->get($contentType->getService());
if ($instance instanceof ContentTypes\AbstractType) {
$jarvesContentRender->addType(
$contentType->getId(),
$container->get($contentType->getService())
);
} else {
throw new \RuntimeException(sprintf(
'Content type %s:%s with service %s has wrong parent class',
$bundleConfig->getName(),
$contentType->getId(),
$contentType->getService()
));
}
}
}
}
}
public function getManagerConfig(Connection $connection)
{
$config = [];
$config['dsn'] = $connection->getDsn();
$config['user'] = (string)$connection->getUsername();
$config['password'] = (string)$connection->getPassword();
$config['options']['ATTR_PERSISTENT'] = (boolean)$connection->getPersistent();
$config['settings']['charset'] = $connection->getCharset();
return $config;
}
}