[FEATURE] Place global Fluid ViewHelper namespaces in TYPO3 configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / ViewHelper / ViewHelperResolver.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
22 /**
23 * Class ViewHelperResolver
24 *
25 * Class whose purpose is dedicated to resolving classes which
26 * can be used as ViewHelpers and ExpressionNodes in Fluid.
27 *
28 * This CMS-specific version of the ViewHelperResolver works
29 * almost exactly like the one from Fluid itself, with the main
30 * differences being that this one supports a legacy mode flag
31 * which when toggled on makes the Fluid parser behave exactly
32 * like it did in the legacy CMS Fluid package.
33 *
34 * In addition to modifying the behavior or the parser when
35 * legacy mode is requested, this ViewHelperResolver is also
36 * made capable of "mixing" two different ViewHelper namespaces
37 * to effectively create aliases for the Fluid core ViewHelpers
38 * to be loaded in the TYPO3\CMS\ViewHelpers scope as well.
39 *
40 * Default ViewHelper namespaces are read TYPO3 configuration at:
41 *
42 * $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']
43 *
44 * Extending this array allows third party ViewHelper providers
45 * to automatically add or extend namespaces which then become
46 * available in every Fluid template file without having to
47 * register the namespace.
48 */
49 class ViewHelperResolver extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver
50 {
51 /**
52 * ViewHelperResolver constructor
53 *
54 * Loads namespaces defined in global TYPO3 configuration. Overlays `f:`
55 * with `f:debug:` when Fluid debugging is enabled in the admin panel,
56 * causing debugging-specific ViewHelpers to be resolved in that case.
57 */
58 public function __construct()
59 {
60 $this->namespaces = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces'];
61 $configuration = $this->getBackendUser()->uc['TSFE_adminConfig'];
62 if (TYPO3_MODE === 'FE'
63 && isset($configuration['preview_showFluidDebug'])
64 && $configuration['preview_showFluidDebug']
65 ) {
66 $this->namespaces['f'][] = 'TYPO3\\CMS\\Fluid\\ViewHelpers\\Debug';
67 }
68 }
69
70 /**
71 * @param string $viewHelperClassName
72 * @return \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface
73 */
74 public function createViewHelperInstanceFromClassName($viewHelperClassName)
75 {
76 return $this->getObjectManager()->get($viewHelperClassName);
77 }
78
79 /**
80 * @return ObjectManagerInterface
81 */
82 protected function getObjectManager()
83 {
84 return GeneralUtility::makeInstance(ObjectManager::class);
85 }
86
87 /**
88 * @return BackendUserAuthentication
89 */
90 protected function getBackendUser()
91 {
92 return $GLOBALS['BE_USER'];
93 }
94 }