|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | +namespace TYPO3\CMS\Core\Controller; |
| 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 | + |
| 18 | +use Psr\Http\Message\ResponseInterface; |
| 19 | +use Psr\Http\Message\ServerRequestInterface; |
| 20 | +use TYPO3\CMS\Core\Http\JsonResponse; |
| 21 | +use TYPO3\CMS\Core\Page\PageRenderer; |
| 22 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 23 | + |
| 24 | +/** |
| 25 | + * Handling requirejs client requests. |
| 26 | + */ |
| 27 | +class RequireJsController |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var PageRenderer |
| 31 | + */ |
| 32 | + protected $pageRenderer; |
| 33 | + |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Retrieves additional requirejs configuration for a given module name or module path. |
| 41 | + * |
| 42 | + * The JSON result e.g. could look like: |
| 43 | + * { |
| 44 | + * "shim": { |
| 45 | + * "vendor/module": ["exports" => "TheModule"] |
| 46 | + * }, |
| 47 | + * "paths": { |
| 48 | + * "vendor/module": "/public/web/path/" |
| 49 | + * }, |
| 50 | + * "packages": { |
| 51 | + * [ |
| 52 | + * "name": "module", |
| 53 | + * ... |
| 54 | + * ] |
| 55 | + * } |
| 56 | + * } |
| 57 | + * |
| 58 | + * Parameter name either could be the module name ("vendor/module") or a |
| 59 | + * module path ("vendor/module/component") belonging to a module. |
| 60 | + * |
| 61 | + * @param ServerRequestInterface $request |
| 62 | + * @return ResponseInterface |
| 63 | + */ |
| 64 | + public function retrieveConfiguration(ServerRequestInterface $request): ResponseInterface |
| 65 | + { |
| 66 | + $name = $request->getQueryParams()['name'] ?? null; |
| 67 | + if (empty($name) || !is_string($name)) { |
| 68 | + return new JsonResponse(null, 404); |
| 69 | + } |
| 70 | + $configuration = $this->findConfiguration($name); |
| 71 | + return new JsonResponse($configuration, !empty($configuration) ? 200 : 404); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $name |
| 76 | + * @return array |
| 77 | + */ |
| 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 | +} |
0 commit comments