1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\LayoutBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
6
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
7
|
|
|
|
8
|
|
|
use Oro\Component\Layout\Layout; |
9
|
|
|
use Oro\Component\Layout\LayoutContext; |
10
|
|
|
use Oro\Component\Layout\LayoutManager; |
11
|
|
|
use Oro\Component\Layout\ContextInterface; |
12
|
|
|
use Oro\Component\Layout\Exception\LogicException; |
13
|
|
|
|
14
|
|
|
use Oro\Bundle\LayoutBundle\Request\LayoutHelper; |
15
|
|
|
use Oro\Bundle\LayoutBundle\Annotation\Layout as LayoutAnnotation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The LayoutListener class handles the @Layout annotation. |
19
|
|
|
*/ |
20
|
|
|
class LayoutListener |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var LayoutHelper |
24
|
|
|
*/ |
25
|
|
|
protected $layoutHelper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var LayoutManager |
29
|
|
|
*/ |
30
|
|
|
protected $layoutManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param LayoutHelper $layoutHelper |
34
|
|
|
* @param LayoutManager $layoutManager |
35
|
|
|
*/ |
36
|
|
|
public function __construct(LayoutHelper $layoutHelper, LayoutManager $layoutManager) |
37
|
|
|
{ |
38
|
|
|
$this->layoutHelper = $layoutHelper; |
39
|
|
|
$this->layoutManager = $layoutManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Renders the layout and initializes the content of a new response object |
44
|
|
|
* with the rendered layout. |
45
|
|
|
* |
46
|
|
|
* @param GetResponseForControllerResultEvent $event |
47
|
|
|
* |
48
|
|
|
* @throws LogicException if @Layout annotation is used in incorrect way |
49
|
|
|
*/ |
50
|
|
|
public function onKernelView(GetResponseForControllerResultEvent $event) |
51
|
|
|
{ |
52
|
|
|
$request = $event->getRequest(); |
53
|
|
|
|
54
|
|
|
$layoutAnnotation = $this->layoutHelper->getLayoutAnnotation($request); |
55
|
|
|
if (!$layoutAnnotation) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
if ($request->attributes->get('_template')) { |
59
|
|
|
throw new LogicException( |
60
|
|
|
'The @Template() annotation cannot be used together with the @Layout() annotation.' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$parameters = $event->getControllerResult(); |
65
|
|
|
if (is_array($parameters)) { |
66
|
|
|
$context = new LayoutContext(); |
67
|
|
|
foreach ($parameters as $key => $value) { |
68
|
|
|
$context->set($key, $value); |
69
|
|
|
} |
70
|
|
|
$this->configureContext($context, $layoutAnnotation); |
71
|
|
|
$layout = $this->getLayout($context, $layoutAnnotation); |
72
|
|
|
} elseif ($parameters instanceof ContextInterface) { |
73
|
|
|
$this->configureContext($parameters, $layoutAnnotation); |
74
|
|
|
$layout = $this->getLayout($parameters, $layoutAnnotation); |
75
|
|
|
} elseif ($parameters instanceof Layout) { |
76
|
|
|
if (!$layoutAnnotation->isEmpty()) { |
77
|
|
|
throw new LogicException( |
78
|
|
|
'The empty @Layout() annotation must be used when ' |
79
|
|
|
. 'the controller returns an instance of "Oro\Component\Layout\Layout".' |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
$layout = $parameters; |
83
|
|
|
} else { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$response = new Response(); |
88
|
|
|
$response->setContent($layout->render()); |
89
|
|
|
$event->setResponse($response); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the layout and add parameters to the layout context. |
94
|
|
|
* |
95
|
|
|
* @param ContextInterface $context |
96
|
|
|
* @param LayoutAnnotation $layoutAnnotation |
97
|
|
|
* |
98
|
|
|
* @return Layout |
99
|
|
|
*/ |
100
|
|
|
protected function getLayout(ContextInterface $context, LayoutAnnotation $layoutAnnotation) |
101
|
|
|
{ |
102
|
|
|
$layoutBuilder = $this->layoutManager->getLayoutBuilder(); |
103
|
|
|
// TODO discuss adding root automatically |
104
|
|
|
$layoutBuilder->add('root', null, 'root'); |
105
|
|
|
|
106
|
|
|
$blockThemes = $layoutAnnotation->getBlockThemes(); |
107
|
|
|
if (!empty($blockThemes)) { |
108
|
|
|
$layoutBuilder->setBlockTheme($blockThemes); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $layoutBuilder->getLayout($context); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Configures the layout context. |
116
|
|
|
* |
117
|
|
|
* @param ContextInterface $context |
118
|
|
|
* @param LayoutAnnotation $layoutAnnotation |
119
|
|
|
*/ |
120
|
|
|
protected function configureContext(ContextInterface $context, LayoutAnnotation $layoutAnnotation) |
121
|
|
|
{ |
122
|
|
|
$action = $layoutAnnotation->getAction(); |
123
|
|
|
if (!empty($action)) { |
124
|
|
|
$currentAction = $context->getOr('action'); |
125
|
|
|
if (empty($currentAction)) { |
126
|
|
|
$context->set('action', $action); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
$theme = $layoutAnnotation->getTheme(); |
130
|
|
|
if (!empty($theme)) { |
131
|
|
|
$currentTheme = $context->getOr('theme'); |
132
|
|
|
if (empty($currentTheme)) { |
133
|
|
|
$context->set('theme', $theme); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$vars = $layoutAnnotation->getVars(); |
138
|
|
|
if (!empty($vars)) { |
139
|
|
|
$context->getResolver()->setRequired($vars); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.