e016b925dcff3112efc83f5bd48b2f3e51ce2462
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / ExternalViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
14 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
16
17 /**
18 * A view helper for creating URIs to external targets.
19 * Currently the specified URI is simply passed through.
20 *
21 * = Examples =
22 *
23 * <code>
24 * <f:uri.external uri="http://www.typo3.org" />
25 * </code>
26 * <output>
27 * http://www.typo3.org
28 * </output>
29 *
30 * <code title="custom default scheme">
31 * <f:uri.external uri="typo3.org" defaultScheme="ftp" />
32 * </code>
33 * <output>
34 * ftp://typo3.org
35 * </output>
36 *
37 * @api
38 */
39 class ExternalViewHelper extends AbstractViewHelper implements CompilableInterface {
40
41 /**
42 * @param string $uri target URI
43 * @param string $defaultScheme scheme the href attribute will be prefixed with if specified $uri does not contain a scheme already
44 * @return string Rendered URI
45 * @api
46 */
47 public function render($uri, $defaultScheme = 'http') {
48 return self::renderStatic(
49 array(
50 'uri' => $uri,
51 'defaultScheme' => $defaultScheme
52 ),
53 $this->buildRenderChildrenClosure(),
54 $this->renderingContext
55 );
56 }
57
58 /**
59 * @param array $arguments
60 * @param callable $renderChildrenClosure
61 * @param RenderingContextInterface $renderingContext
62 *
63 * @return string
64 */
65 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
66 $uri = $arguments['uri'];
67 $defaultScheme = $arguments['defaultScheme'];
68
69 $scheme = parse_url($uri, PHP_URL_SCHEME);
70 if ($scheme === NULL && $defaultScheme !== '') {
71 $uri = $defaultScheme . '://' . $uri;
72 }
73 return $uri;
74 }
75 }