-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
AbstractFragmentController.php
169 lines (135 loc) · 4.92 KB
/
AbstractFragmentController.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
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\CoreBundle\Controller;
use Contao\CoreBundle\EventListener\SubrequestCacheSubscriber;
use Contao\CoreBundle\Fragment\FragmentOptionsAwareInterface;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\FrontendTemplate;
use Contao\Model;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\Template;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
abstract class AbstractFragmentController extends AbstractController implements FragmentOptionsAwareInterface
{
protected array $options = [];
public function setFragmentOptions(array $options): void
{
$this->options = $options;
}
/**
* @return array<string>
*/
public static function getSubscribedServices()
{
$services = parent::getSubscribedServices();
$services['request_stack'] = RequestStack::class;
$services['contao.routing.scope_matcher'] = ScopeMatcher::class;
return $services;
}
protected function getPageModel(): ?PageModel
{
$request = $this->get('request_stack')->getCurrentRequest();
if (null === $request || !$request->attributes->has('pageModel')) {
return null;
}
$pageModel = $request->attributes->get('pageModel');
if ($pageModel instanceof PageModel) {
return $pageModel;
}
if (
isset($GLOBALS['objPage'])
&& $GLOBALS['objPage'] instanceof PageModel
&& (int) $GLOBALS['objPage']->id === (int) $pageModel
) {
return $GLOBALS['objPage'];
}
$this->initializeContaoFramework();
/** @var PageModel $pageAdapter */
$pageAdapter = $this->get('contao.framework')->getAdapter(PageModel::class);
return $pageAdapter->findByPk((int) $pageModel);
}
/**
* Creates a template by name or from the "customTpl" field of the model.
*/
protected function createTemplate(Model $model, string $templateName): Template
{
if (isset($this->options['template'])) {
$templateName = $this->options['template'];
}
if ($model->customTpl) {
$request = $this->get('request_stack')->getCurrentRequest();
// Use the custom template unless it is a back end request
if (null === $request || !$this->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
$templateName = $model->customTpl;
}
}
$template = $this->get('contao.framework')->createInstance(FrontendTemplate::class, [$templateName]);
$template->setData($model->row());
return $template;
}
/**
* @param string|array $headline
*/
protected function addHeadlineToTemplate(Template $template, $headline): void
{
$data = StringUtil::deserialize($headline);
$template->headline = \is_array($data) ? $data['value'] : $data;
$template->hl = \is_array($data) ? $data['unit'] : 'h1';
}
/**
* @param string|array $cssID
*/
protected function addCssAttributesToTemplate(Template $template, string $templateName, $cssID, array $classes = null): void
{
$data = StringUtil::deserialize($cssID, true);
$template->class = trim($templateName.' '.($data[1] ?? ''));
$template->cssID = !empty($data[0]) ? ' id="'.$data[0].'"' : '';
if (!empty($classes)) {
$template->class .= ' '.implode(' ', $classes);
}
}
protected function addPropertiesToTemplate(Template $template, array $properties): void
{
foreach ($properties as $k => $v) {
$template->{$k} = $v;
}
}
protected function addSectionToTemplate(Template $template, string $section): void
{
$template->inColumn = $section;
}
/**
* Returns the type from the class name.
*/
protected function getType(): string
{
if (isset($this->options['type'])) {
return $this->options['type'];
}
$className = ltrim(strrchr(static::class, '\\'), '\\');
if ('Controller' === substr($className, -10)) {
$className = substr($className, 0, -10);
}
return Container::underscore($className);
}
protected function render(string $view, array $parameters = [], Response $response = null): Response
{
if (null === $response) {
$response = new Response();
// Mark this response to affect the caching of the current page but remove any default cache headers
$response->headers->set(SubrequestCacheSubscriber::MERGE_CACHE_HEADER, '1');
$response->headers->remove('Cache-Control');
}
return parent::render($view, $parameters, $response);
}
}