Passed
Push — master ( af49bb...0a24d1 )
by
unknown
15: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 TYPO3\CMS\Core\Http\ApplicationType;
19
use TYPO3\CMS\Core\Http\NormalizedParams;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Utility\PathUtility;
22
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25
26
/**
27
 * A ViewHelper for creating URIs to resources.
28
 *
29
 * Examples
30
 * ========
31
 *
32
 * Defaults
33
 * --------
34
 *
35
 * ::
36
 *
37
 *    <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
38
 *
39
 * Output::
40
 *
41
 *    <link href="typo3conf/ext/example_extension/Resources/Public/css/stylesheet.css" rel="stylesheet" />
42
 *
43
 * Depending on current extension.
44
 *
45
 * With extension name
46
 * -------------------
47
 *
48
 * ::
49
 *
50
 *    <link href="{f:uri.resource(path:'css/stylesheet.css', extensionName: 'AnotherExtension')}" rel="stylesheet" />
51
 *
52
 * Output::
53
 *
54
 *    <link href="typo3conf/ext/another_extension/Resources/Public/css/stylesheet.css" rel="stylesheet" />
55
 */
56
class ResourceViewHelper extends AbstractViewHelper
57
{
58
    use CompileWithRenderStatic;
59
60
    /**
61
     * Initialize arguments
62
     */
63
    public function initializeArguments()
64
    {
65
        $this->registerArgument('path', 'string', 'The path and filename of the resource (relative to Public resource directory of the extension).', true);
66
        $this->registerArgument('extensionName', 'string', 'Target extension name. If not set, the current extension name will be used');
67
        $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
68
    }
69
70
    /**
71
     * Render the URI to the resource. The filename is used from child content.
72
     *
73
     * @param array $arguments
74
     * @param \Closure $renderChildrenClosure
75
     * @param RenderingContextInterface $renderingContext
76
     * @return string The URI to the resource
77
     */
78
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
79
    {
80
        $path = $arguments['path'];
81
        $extensionName = $arguments['extensionName'];
82
        $absolute = $arguments['absolute'];
83
84
        if ($extensionName === null) {
85
            $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

85
            $extensionName = $renderingContext->/** @scrutinizer ignore-call */ getRequest()->getControllerExtensionName();
Loading history...
86
        }
87
        $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $path;
88
        $uri = GeneralUtility::getFileAbsFileName($uri);
89
        if ($absolute === false && $uri !== false) {
90
            $uri = PathUtility::getAbsoluteWebPath($uri);
91
        }
92
        if ($absolute === true) {
93
            $request = $renderingContext->getRequest();
94
            /** @var NormalizedParams $normalizedParams */
95
            $normalizedParams = $request->getAttribute('normalizedParams');
96
            $baseUri = $normalizedParams->getSiteUrl();
97
            if (ApplicationType::fromRequest($request)->isBackend()) {
98
                $baseUri .= TYPO3_mainDir;
99
            }
100
            $uri = PathUtility::stripPathSitePrefix($uri);
101
            $uri = $baseUri . $uri;
102
        }
103
        return $uri;
104
    }
105
}
106