TYPO3 CMS  TYPO3_8-7
RequireJsController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
23 
28 {
32  protected $pageRenderer;
33 
34  public function __construct()
35  {
36  $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
37  }
38 
64  public function retrieveConfiguration(ServerRequestInterface $request): ResponseInterface
65  {
66  $name = $request->getQueryParams()['name'] ?? null;
67  if (empty($name) || !is_string($name)) {
68  return $this->createJsonResponse(null, 404);
69  }
70  $configuration = $this->findConfiguration($name);
71  return $this->createJsonResponse($configuration, !empty($configuration) ? 200 : 404);
72  }
73 
78  protected function findConfiguration(string $name): array
79  {
80  $relevantConfiguration = [];
81  $this->pageRenderer->loadRequireJs();
82  $configuration = $this->pageRenderer->getRequireJsConfig(PageRenderer::REQUIREJS_SCOPE_RESOLVE);
83 
84  $shim = $configuration['shim'] ?? [];
85  foreach ($shim as $baseModuleName => $baseModuleConfiguration) {
86  if (strpos($name . '/', $baseModuleName . '/') === 0) {
87  $relevantConfiguration['shim'][$baseModuleName] = $baseModuleConfiguration;
88  }
89  }
90 
91  $paths = $configuration['paths'] ?? [];
92  foreach ($paths as $baseModuleName => $baseModulePath) {
93  if (strpos($name . '/', $baseModuleName . '/') === 0) {
94  $relevantConfiguration['paths'][$baseModuleName] = $baseModulePath;
95  }
96  }
97 
98  $packages = $configuration['packages'] ?? [];
99  foreach ($packages as $package) {
100  if (!empty($package['name'])
101  && strpos($name . '/', $package['name'] . '/') === 0
102  ) {
103  $relevantConfiguration['packages'][] = $package;
104  }
105  }
106 
107  return $relevantConfiguration;
108  }
109 
115  protected function createJsonResponse($configuration, int $statusCode): Response
116  {
117  $response = (new Response())
118  ->withStatus($statusCode)
119  ->withHeader('Content-Type', 'application/json; charset=utf-8');
120 
121  if (!empty($configuration)) {
122  $options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES;
123  $response->getBody()->write(json_encode($configuration ?: null, $options));
124  $response->getBody()->rewind();
125  }
126 
127  return $response;
128  }
129 }
static makeInstance($className,... $constructorArguments)
withHeader($name, $value)
Definition: Message.php:201
retrieveConfiguration(ServerRequestInterface $request)
createJsonResponse($configuration, int $statusCode)