|
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(); |
|
|
|
|
|
|
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
|
|
|
|