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

BaseViewHelper::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;
17
18
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Core\Http\ApplicationType;
20
use TYPO3\CMS\Core\Http\NormalizedParams;
21
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24
25
/**
26
 * ViewHelper which creates a :html:`<base href="..."></base>` tag.
27
 *
28
 * The Base URI is taken from the current request.
29
 *
30
 * Examples
31
 * ========
32
 *
33
 * Example::
34
 *
35
 *    <f:base />
36
 *
37
 * Output::
38
 *
39
 *    <base href="http://yourdomain.tld/" />
40
 *
41
 * Depending on your domain.
42
 *
43
 * @todo Deprecate this VH: In FE a base tag is set via TS config.baseURL to head, this
44
 *       VH would most likely add it as useless body tag, and BE does not need this VH.
45
 */
46
class BaseViewHelper extends AbstractViewHelper
47
{
48
    use CompileWithRenderStatic;
49
50
    /**
51
     * As this ViewHelper renders HTML, the output must not be escaped.
52
     *
53
     * @var bool
54
     */
55
    protected $escapeOutput = false;
56
57
    /**
58
     * Render the "Base" tag by outputting site URL
59
     *
60
     * Note: renders as <base></base>, because IE6 will else refuse to display
61
     * the page...
62
     *
63
     * @param array $arguments
64
     * @param \Closure $renderChildrenClosure
65
     * @param RenderingContextInterface $renderingContext
66
     *
67
     * @return string "base"-Tag.
68
     */
69
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
70
    {
71
        $request = static::getRequest();
72
        /** @var NormalizedParams $normalizedParams */
73
        $normalizedParams = $request->getAttribute('normalizedParams');
74
        $baseUri = $normalizedParams->getSiteUrl();
75
        if (ApplicationType::fromRequest($request)->isBackend()) {
76
            $baseUri .= TYPO3_mainDir;
77
        }
78
        return '<base href="' . htmlspecialchars($baseUri) . '" />';
79
    }
80
81
    /**
82
     * @todo Drop this when $renderingContext->getRequest() returns an implementation of ServerRequestInterface
83
     */
84
    protected static function getRequest(): ServerRequestInterface
85
    {
86
        return $GLOBALS['TYPO3_REQUEST'];
87
    }
88
}
89