-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStyleguideController.php
More file actions
277 lines (234 loc) · 10.5 KB
/
Copy pathStyleguideController.php
File metadata and controls
277 lines (234 loc) · 10.5 KB
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
<?php
declare(strict_types=1);
namespace Sitegeist\FluidStyleguide\Controller;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Sitegeist\FluidComponentsLinter\Service\CodeQualityService;
use Sitegeist\FluidComponentsLinter\Service\ConfigurationService;
use Sitegeist\FluidStyleguide\Domain\Repository\ComponentRepository;
use Sitegeist\FluidStyleguide\Event\PostProcessComponentViewEvent;
use Sitegeist\FluidStyleguide\Event\PreProcessComponentViewEvent;
use Sitegeist\FluidStyleguide\Service\ComponentDownloadService;
use Sitegeist\FluidStyleguide\Service\StyleguideConfigurationManager;
use SMS\FluidComponents\Utility\ComponentLoader;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\View\FluidViewAdapter;
use TYPO3\CMS\Fluid\View\StandaloneView;
class StyleguideController
{
protected StandaloneView|FluidViewAdapter $view;
protected ServerRequestInterface $request;
public function __construct(
protected ComponentRepository $componentRepository,
protected ComponentDownloadService $componentDownloadService,
protected StyleguideConfigurationManager $styleguideConfigurationManager,
protected ContainerInterface $container,
) {
}
public function listAction(): ResponseInterface
{
$allComponents = $this->componentRepository->findWithFixtures();
$componentPackages = $this->groupComponentsByPackage($allComponents);
// if json request, return JSON response
$accept = $this->request->getHeaderLine('Accept');
if (str_starts_with($accept, 'application/json')) {
throw new ImmediateResponseException(new JsonResponse($allComponents));
}
$this->view->assignMultiple([
'navigation' => $allComponents,
'packages' => $componentPackages
]);
return new HtmlResponse($this->view->render('Styleguide/List'));
}
public function showAction(array $arguments = []): ResponseInterface
{
$component = $arguments['component'] ?? '';
$fixture = $arguments['fixture'] ?? 'default';
// Sanitize user input
$component = $this->sanitizeComponentIdentifier($component);
$fixture = $this->sanitizeFixtureName($fixture);
// Check if component exists
$component = $this->componentRepository->findWithFixturesByIdentifier($component);
if (!$component) {
return new Response('Component not found', 404);
}
if ($this->styleguideConfigurationManager->isFeatureEnabled('CodeQuality') && class_exists(CodeQualityService::class)) {
$showQualityIssues = true;
// Initialize code quality service
$configurationService = new ConfigurationService;
$configuration = $configurationService->getFinalConfiguration(false, $component->getCodeQualityConfiguration() ?? false);
$registeredChecks = $configurationService->getRegisteredChecks();
$codeQualityService = new CodeQualityService($configuration, $registeredChecks);
// Get code quality issues for component
$qualityIssues = $codeQualityService->validateComponent(
$component->getLocation()->getFilePath()
);
} else {
$showQualityIssues = false;
$qualityIssues = [];
}
$this->view->assignMultiple([
'navigation' => $this->componentRepository->findWithFixtures(),
'activeComponent' => $component,
'activeFixture' => $fixture,
'showQualityIssues' => $showQualityIssues,
'qualityIssues' => $qualityIssues
]);
return new HtmlResponse($this->view->render('Styleguide/Show'));
}
/**
* Shows a rendered example of a component. This will be shown inside of the iframe
*
* @return void
*/
public function componentAction(array $arguments = [])
{
$component = $arguments['component'] ?? '';
$fixture = $arguments['fixture'] ?? 'default';
$formData = $arguments['formData'] ?? [];
// Sanitize user input
$component = $this->sanitizeComponentIdentifier($component);
$fixture = $this->sanitizeFixtureName($fixture);
if (!$this->styleguideConfigurationManager->isFeatureEnabled('Editor')) {
$formData = [];
} else {
$formData = $this->sanitizeFormData($formData);
}
// Check if component exists
$component = $this->componentRepository->findWithFixturesByIdentifier($component);
if (!$component) {
return new Response('Component not found', 404);
}
$package = $component->getName()->getPackage();
$this->view->assignMultiple([
'component' => $component,
'componentCss' => $this->styleguideConfigurationManager->getCssForPackage($package),
'componentJavascript' => $this->styleguideConfigurationManager->getJavascriptForPackage($package),
'fixtureName' => $fixture,
'fixtureData' => $formData
]);
$eventDispatcher = $this->container->get(EventDispatcher::class);
$eventDispatcher->dispatch(new PreProcessComponentViewEvent($component, $fixture, $formData, $this->view));
$renderedView = $this->view->render('Styleguide/Component');
$event = new PostProcessComponentViewEvent($component, $fixture, $formData, $renderedView);
$event = $eventDispatcher->dispatch($event);
$renderedView = $event->getRenderedView();
$renderedView = str_replace('<!-- ###ADDITIONAL_HEADER_DATA### -->', implode('', $event->getHeaderData()), $renderedView);
$renderedView = str_replace('<!-- ###ADDITIONAL_FOOTER_DATA### -->', implode('', $event->getFooterData()), $renderedView);
return $renderedView;
}
/**
* Provides a zip download of a component folder
*/
public function downloadComponentZipAction(array $arguments = [])
{
$component = $arguments['component'] ?? '';
// Sanitize user input
if (!$this->styleguideConfigurationManager->isFeatureEnabled('ZipDownload')) {
return new Response('Zip download is not available', 403);
}
$component = $this->sanitizeComponentIdentifier($component);
// Check if component exists
$component = $this->componentRepository->findWithFixturesByIdentifier($component);
if (!$component) {
return new Response('Component not found', 404);
}
return $this->componentDownloadService->downloadZip($component);
}
protected function groupComponentsByPackage(array $components): array
{
$componentPackages = [];
foreach ($components as $component) {
$packageNamespace = $component->getName()->getPackage()->getNamespace();
if (!isset($componentPackages[$packageNamespace])) {
$componentPackages[$packageNamespace] = [];
}
$componentPackages[$packageNamespace][] = $component;
}
return $componentPackages;
}
public function initializeView(StandaloneView|FluidViewAdapter $view): void
{
$this->view = $view;
$this->view->assignMultiple([
'styleguideConfiguration' => $this->styleguideConfigurationManager,
'styleguideLanguage' => $this->request->getAttribute('language'),
'sitename' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? '',
'baseUri' => $this->request->getAttribute('site')->getBase()
]);
$this->registerDemoComponents();
}
public function setRequest(ServerRequestInterface $request): void
{
$this->request = $request;
}
/**
* Makes sure that no malicious user input will be passed to a component
*/
protected function sanitizeFormData(array $formData): array
{
foreach ($formData as $key => &$value) {
// Throw away any input other than string
if (!is_string($value)) {
unset($formData[$key]);
continue;
}
// Convert to integer
if (MathUtility::canBeInterpretedAsInteger($value)) {
$value = (int)$value;
// Convert to float
} elseif (MathUtility::canBeInterpretedAsFloat($value)) {
$value = (float)$value;
// Convert to boolean
} elseif (mb_strtoupper($value) === 'TRUE' || mb_strtoupper($value) === 'FALSE') {
$value = (mb_strtoupper($value) === 'TRUE');
// Escape string if necessary
} elseif ($this->styleguideConfigurationManager->isFeatureEnabled('EscapeInputFromEditor')) {
$value = htmlspecialchars($value);
}
}
return $formData;
}
/**
* Make sure that the component identifier doesn't include any malicious characters
*/
protected function sanitizeComponentIdentifier(string $componentIdentifier): string
{
return trim((string) preg_replace('#[^a-z0-9_\\\\]#i', '', $componentIdentifier), '\\');
}
/**
* Make sure that the fixture name doesn't include any malicious characters
*/
protected function sanitizeFixtureName(string $fixtureName): string
{
return preg_replace('#[^a-z0-9_]#i', '', $fixtureName);
}
protected function registerDemoComponents(): void
{
$componentLoader = $this->container->get(ComponentLoader::class);
if (count($componentLoader->getNamespaces()) === 1 ||
$this->styleguideConfigurationManager->isFeatureEnabled('DemoComponents')
) {
$demoNamespace = 'Sitegeist\\FluidStyleguide\\DemoComponents';
$componentLoader->addNamespace(
$demoNamespace,
ExtensionManagementUtility::extPath(
'fluid_styleguide',
'Resources/Private/DemoComponents'
)
);
$this->view->getRenderingContext()->getViewHelperResolver()->addNamespace(
'demo',
$demoNamespace
);
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['demo'] = [$demoNamespace];
}
}
}