Passed
Push — master ( 669ab6...e096b1 )
by
unknown
14:08
created

createViewHelperInstanceFromClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 21
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Fluid\Core\ViewHelper;
17
18
use Psr\Container\ContainerInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21
use TYPO3\CMS\Core\DependencyInjection\FailsafeContainer;
22
use TYPO3\CMS\Core\Http\ApplicationType;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
26
27
/**
28
 * Class ViewHelperResolver
29
 *
30
 * Class whose purpose is dedicated to resolving classes which
31
 * can be used as ViewHelpers and ExpressionNodes in Fluid.
32
 *
33
 * This CMS-specific version of the ViewHelperResolver works
34
 * almost exactly like the one from Fluid itself, with the main
35
 * differences being that this one supports a legacy mode flag
36
 * which when toggled on makes the Fluid parser behave exactly
37
 * like it did in the legacy CMS Fluid package.
38
 *
39
 * In addition to modifying the behavior or the parser when
40
 * legacy mode is requested, this ViewHelperResolver is also
41
 * made capable of "mixing" two different ViewHelper namespaces
42
 * to effectively create aliases for the Fluid core ViewHelpers
43
 * to be loaded in the TYPO3\CMS\ViewHelpers scope as well.
44
 *
45
 * Default ViewHelper namespaces are read TYPO3 configuration at:
46
 *
47
 * $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']
48
 *
49
 * Extending this array allows third party ViewHelper providers
50
 * to automatically add or extend namespaces which then become
51
 * available in every Fluid template file without having to
52
 * register the namespace.
53
 *
54
 * @internal This is a helper class which is not considered part of TYPO3's Public API.
55
 */
56
class ViewHelperResolver extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver
57
{
58
    protected ContainerInterface $container;
59
60
    /**
61
     * @deprecated since v11, will be removed with 12
62
     */
63
    protected ObjectManagerInterface $objectManager;
64
65
    /**
66
     * ViewHelperResolver constructor
67
     *
68
     * Loads namespaces defined in global TYPO3 configuration. Overlays `f:`
69
     * with `f:debug:` when Fluid debugging is enabled in the admin panel,
70
     * causing debugging-specific ViewHelpers to be resolved in that case.
71
     *
72
     * @internal constructor, use `ViewHelperResolverFactory->create()` instead
73
     */
74
    public function __construct(ContainerInterface $container, ObjectManagerInterface $objectManager, array $namespaces)
75
    {
76
        $this->container = $container;
77
        // @deprecated since v11, will be removed with 12. Drop argument in ViewHelperResolverFactory
78
        $this->objectManager = $objectManager;
79
        $this->namespaces = $namespaces;
80
        if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
81
            && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()
82
            && $this->getBackendUser() instanceof BackendUserAuthentication
83
        ) {
84
            if ($this->getBackendUser()->uc['AdminPanel']['preview_showFluidDebug'] ?? false) {
85
                $this->namespaces['f'][] = 'TYPO3\\CMS\\Fluid\\ViewHelpers\\Debug';
86
            }
87
        }
88
    }
89
90
    /**
91
     * @param string $viewHelperClassName
92
     * @return ViewHelperInterface
93
     */
94
    public function createViewHelperInstanceFromClassName($viewHelperClassName): ViewHelperInterface
95
    {
96
        if ($this->container instanceof FailsafeContainer) {
97
            // The install tool creates VH instances using makeInstance to not rely on symfony DI here,
98
            // otherwise we'd have to have all install-tool used ones in ServiceProvider.php. However,
99
            // none of the install tool used VH's use injection.
100
            /** @var ViewHelperInterface $viewHelperInstance */
101
            $viewHelperInstance = GeneralUtility::makeInstance($viewHelperClassName);
102
            return $viewHelperInstance;
103
        }
104
105
        if ($this->container->has($viewHelperClassName)) {
106
            /** @var ViewHelperInterface $viewHelperInstance */
107
            $viewHelperInstance = $this->container->get($viewHelperClassName);
108
            return $viewHelperInstance;
109
        }
110
111
        /** @var ViewHelperInterface $viewHelperInstance */
112
        // @deprecated since v11, will be removed with 12. Fallback if extensions VH has no Services.yaml, yet.
113
        $viewHelperInstance = $this->objectManager->get($viewHelperClassName);
114
        return $viewHelperInstance;
115
    }
116
117
    protected function getBackendUser(): ?BackendUserAuthentication
118
    {
119
        return $GLOBALS['BE_USER'] ?? null;
120
    }
121
}
122