[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / CacheHashEnforcer.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Mvc\Web;
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 TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20 use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
21
22 /**
23 * Enforces cHash argument if it is required for a given request
24 * @internal only to be used within Extbase, not part of TYPO3 Core API.
25 */
26 class CacheHashEnforcer implements SingletonInterface
27 {
28 /**
29 * @var CacheHashCalculator
30 */
31 protected $cacheHashCalculator;
32
33 /**
34 * @var TypoScriptFrontendController
35 */
36 protected $typoScriptFrontendController;
37
38 /**
39 * CacheHashEnforcer constructor.
40 *
41 * @param CacheHashCalculator $cacheHashCalculator
42 * @param TypoScriptFrontendController|null $typoScriptFrontendController
43 */
44 public function __construct(
45 CacheHashCalculator $cacheHashCalculator,
46 TypoScriptFrontendController $typoScriptFrontendController = null
47 ) {
48 $this->cacheHashCalculator = $cacheHashCalculator;
49 $this->typoScriptFrontendController = $typoScriptFrontendController ?: $GLOBALS['TSFE'];
50 }
51
52 /**
53 * Checks if cHash is required for the current request and calls
54 * TypoScriptFrontendController::reqCHash() if so.
55 * This call will trigger a PageNotFoundException if arguments are required and cHash is not present.
56 *
57 * @param Request $request
58 * @param string $pluginNamespace
59 */
60 public function enforceForRequest(Request $request, string $pluginNamespace)
61 {
62 $arguments = $request->getArguments();
63 if (is_array($arguments) && count($arguments) > 0) {
64 $parameters = [$pluginNamespace => $arguments];
65 $parameters['id'] = $this->typoScriptFrontendController->id;
66 $relevantParameters = $this->cacheHashCalculator->getRelevantParameters(
67 http_build_query($parameters, '', '&', PHP_QUERY_RFC3986)
68 );
69 if (count($relevantParameters) > 0) {
70 $this->typoScriptFrontendController->reqCHash();
71 }
72 }
73 }
74 }