Passed
Push — master ( c14a0b...566b5b )
by
unknown
27:12 queued 10:19
created

ResourceViewHelper::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\ViewHelpers\Uri;
17
18
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Core\Http\ApplicationType;
20
use TYPO3\CMS\Core\Http\NormalizedParams;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\PathUtility;
23
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
26
27
/**
28
 * A ViewHelper for creating URIs to resources.
29
 *
30
 * Examples
31
 * ========
32
 *
33
 * Defaults
34
 * --------
35
 *
36
 * ::
37
 *
38
 *    <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
39
 *
40
 * Output::
41
 *
42
 *    <link href="typo3conf/ext/example_extension/Resources/Public/css/stylesheet.css" rel="stylesheet" />
43
 *
44
 * Depending on current extension.
45
 *
46
 * With extension name
47
 * -------------------
48
 *
49
 * ::
50
 *
51
 *    <link href="{f:uri.resource(path:'css/stylesheet.css', extensionName: 'AnotherExtension')}" rel="stylesheet" />
52
 *
53
 * Output::
54
 *
55
 *    <link href="typo3conf/ext/another_extension/Resources/Public/css/stylesheet.css" rel="stylesheet" />
56
 */
57
class ResourceViewHelper extends AbstractViewHelper
58
{
59
    use CompileWithRenderStatic;
60
61
    /**
62
     * Initialize arguments
63
     */
64
    public function initializeArguments()
65
    {
66
        $this->registerArgument('path', 'string', 'The path and filename of the resource (relative to Public resource directory of the extension).', true);
67
        $this->registerArgument('extensionName', 'string', 'Target extension name. If not set, the current extension name will be used');
68
        $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
69
    }
70
71
    /**
72
     * Render the URI to the resource. The filename is used from child content.
73
     *
74
     * @param array $arguments
75
     * @param \Closure $renderChildrenClosure
76
     * @param RenderingContextInterface $renderingContext
77
     * @return string The URI to the resource
78
     */
79
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
80
    {
81
        $path = $arguments['path'];
82
        $extensionName = $arguments['extensionName'];
83
        $absolute = $arguments['absolute'];
84
85
        if ($extensionName === null) {
86
            $extensionName = $renderingContext->getRequest()->getControllerExtensionName();
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\Core\Re...nderingContextInterface such as TYPO3\CMS\Fluid\Core\Rendering\RenderingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            $extensionName = $renderingContext->/** @scrutinizer ignore-call */ getRequest()->getControllerExtensionName();
Loading history...
87
        }
88
        $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $path;
89
        $uri = GeneralUtility::getFileAbsFileName($uri);
90
        if ($absolute === false && $uri !== false) {
91
            $uri = PathUtility::getAbsoluteWebPath($uri);
92
        }
93
        if ($absolute === true) {
94
            $request = static::getRequest();
95
            /** @var NormalizedParams $normalizedParams */
96
            $normalizedParams = $request->getAttribute('normalizedParams');
97
            $baseUri = $normalizedParams->getSiteUrl();
98
            if (ApplicationType::fromRequest($request)->isBackend()) {
99
                $baseUri .= TYPO3_mainDir;
100
            }
101
            $uri = PathUtility::stripPathSitePrefix($uri);
102
            $uri = $baseUri . $uri;
103
        }
104
        return $uri;
105
    }
106
107
    /**
108
     * @todo Drop this when $renderingContext->getRequest() returns an implementation of ServerRequestInterface
109
     */
110
    protected static function getRequest(): ServerRequestInterface
111
    {
112
        return $GLOBALS['TYPO3_REQUEST'];
113
    }
114
}
115