1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace SWP\Bundle\TemplateEngineBundle\Service; |
15
|
|
|
|
16
|
|
|
use SWP\Bundle\TemplateEngineBundle\Container\SimpleContainer; |
17
|
|
|
use SWP\Bundle\TemplateEngineBundle\Model\Container; |
18
|
|
|
use SWP\Bundle\TemplateEngineBundle\Model\ContainerData; |
19
|
|
|
use SWP\Component\Common\Event\HttpCacheEvent; |
20
|
|
|
|
21
|
|
|
class ContainerService |
22
|
|
|
{ |
23
|
|
|
const OPEN_TAG_TEMPLATE = '<div id="swp_container_{{ id }}" class="swp_container {{ class }}" style="{% if height %}height: {{ height }}px;{% endif %}{% if width %}width: {{width}}px;{% endif %}{{styles}}"{% for value in data %} data-{{value.getKey()}}="{{value.getValue()}}"{% endfor %} >'; |
24
|
|
|
const CLOSE_TAG_TEMPLATE = '</div>'; |
25
|
|
|
|
26
|
|
|
protected $objectManager; |
27
|
|
|
protected $cacheDir; |
28
|
|
|
protected $debug; |
29
|
|
|
protected $renderer = false; |
30
|
|
|
protected $eventDispatcher; |
31
|
|
|
|
32
|
|
|
public function __construct($doctrine, $cacheDir, $eventDispatcher, $debug = false) |
33
|
|
|
{ |
34
|
|
|
$this->objectManager = $doctrine->getManager(); |
35
|
|
|
$this->cacheDir = $cacheDir.'/twig'; |
36
|
|
|
$this->debug = $debug; |
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getContainer($name, array $parameters = [], $createIfNotExists = true) |
41
|
|
|
{ |
42
|
|
|
$containerEntity = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Container') |
43
|
|
|
->getByName($name) |
44
|
|
|
->getOneOrNullResult(); |
45
|
|
|
|
46
|
|
|
if (!$containerEntity && $createIfNotExists) { |
47
|
|
|
$containerEntity = $this->createNewContainer($name, $parameters); |
48
|
|
|
} elseif (!$containerEntity) { |
49
|
|
|
throw new \Exception('Container was not found'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$widgets = []; |
53
|
|
|
$containerWidgets = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\ContainerWidget') |
54
|
|
|
->getSortedWidgets(['container' => $containerEntity]) |
55
|
|
|
->getResult(); |
56
|
|
|
|
57
|
|
|
foreach ($containerWidgets as $containerWidget) { |
58
|
|
|
$widgetModel = $containerWidget->getWidget(); |
59
|
|
|
$widgetClass = $widgetModel->getType(); |
60
|
|
|
$widgets[] = new $widgetClass($widgetModel); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$container = new SimpleContainer($containerEntity, $this->getRenderer()); |
|
|
|
|
64
|
|
|
$container->setWidgets($widgets); |
65
|
|
|
|
66
|
|
|
return $container; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getRenderer() |
70
|
|
|
{ |
71
|
|
|
if ($this->renderer !== false) { |
72
|
|
|
return $this->renderer; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$options = []; |
76
|
|
|
if ($this->debug == false) { |
|
|
|
|
77
|
|
|
// not debug turn set cache dir |
78
|
|
|
$options['cache'] = $this->cacheDir; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->renderer = new \Twig_Environment( |
|
|
|
|
82
|
|
|
new \Twig_Loader_Array([ |
83
|
|
|
'open_tag' => self::OPEN_TAG_TEMPLATE, |
84
|
|
|
'close_tag' => self::CLOSE_TAG_TEMPLATE, |
85
|
|
|
]), $options |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $this->renderer; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function createNewContainer($name, array $parameters = array()) |
92
|
|
|
{ |
93
|
|
|
$containerEntity = new Container(); |
94
|
|
|
$containerEntity->setName($name); |
95
|
|
|
foreach ($parameters as $key => $value) { |
96
|
|
|
switch ($key) { |
97
|
|
|
case 'height': |
98
|
|
|
$containerEntity->setHeight($value); |
99
|
|
|
break; |
100
|
|
|
case 'width': |
101
|
|
|
$containerEntity->setWidth($value); |
102
|
|
|
break; |
103
|
|
|
case 'cssClass': |
104
|
|
|
$containerEntity->setCssClass($value); |
105
|
|
|
break; |
106
|
|
|
case 'styles': |
107
|
|
|
$containerEntity->setStyles($value); |
108
|
|
|
break; |
109
|
|
|
case 'visible': |
110
|
|
|
$containerEntity->setVisible($value); |
111
|
|
|
break; |
112
|
|
|
case 'data': |
113
|
|
|
foreach ($value as $dataKey => $dataValue) { |
114
|
|
|
$containerData = new ContainerData($dataKey, $dataValue); |
115
|
|
|
$containerData->setContainer($containerEntity); |
116
|
|
|
$this->objectManager->persist($containerData); |
117
|
|
|
$containerEntity->addData($containerData); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
$this->objectManager->persist($containerEntity); |
122
|
|
|
$this->objectManager->flush(); |
123
|
|
|
|
124
|
|
|
$this->eventDispatcher |
125
|
|
|
->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($containerEntity)); |
126
|
|
|
|
127
|
|
|
return $containerEntity; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.