cd4891ccdcad934a2d16a41d175b4c154abe46de
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / View / StandaloneView.php
1 <?php
2 namespace TYPO3\CMS\Fluid\View;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
20 use TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
21 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
23 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
24 use TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException;
25 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
26
27 /**
28 * A standalone template view.
29 * Should be used as view if you want to use Fluid without Extbase extensions
30 *
31 * @api
32 */
33 class StandaloneView extends AbstractTemplateView
34 {
35 /**
36 * @var ObjectManager|null
37 */
38 protected $objectManager = null;
39
40 /**
41 * Constructor
42 *
43 * @param ContentObjectRenderer $contentObject The current cObject. If NULL a new instance will be created
44 * @throws \InvalidArgumentException
45 * @throws \UnexpectedValueException
46 */
47 public function __construct(ContentObjectRenderer $contentObject = null)
48 {
49 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
50
51 /** @var ConfigurationManagerInterface $configurationManager */
52 $configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
53 if ($contentObject === null) {
54 /** @var ContentObjectRenderer $contentObject */
55 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
56 }
57 $configurationManager->setContentObject($contentObject);
58
59 /** @var WebRequest $request */
60 $request = $this->objectManager->get(WebRequest::class);
61 $request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
62 $request->setBaseUri(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
63 /** @var UriBuilder $uriBuilder */
64 $uriBuilder = $this->objectManager->get(UriBuilder::class);
65 $uriBuilder->setRequest($request);
66 /** @var ControllerContext $controllerContext */
67 $controllerContext = $this->objectManager->get(ControllerContext::class);
68 $controllerContext->setRequest($request);
69 $controllerContext->setUriBuilder($uriBuilder);
70 $renderingContext = $this->objectManager->get(RenderingContext::class, $this);
71 $renderingContext->setControllerContext($controllerContext);
72 parent::__construct($renderingContext);
73 }
74
75 /**
76 * @param string $templateName
77 */
78 public function setTemplate($templateName)
79 {
80 $this->baseRenderingContext->setControllerAction($templateName);
81 }
82
83 /**
84 * Sets the format of the current request (default format is "html")
85 *
86 * @param string $format
87 * @throws \RuntimeException
88 * @api
89 */
90 public function setFormat($format)
91 {
92 if ($this->baseRenderingContext instanceof RenderingContext) {
93 $this->baseRenderingContext->getControllerContext()->getRequest()->setFormat($format);
94 } else {
95 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251886);
96 }
97 }
98
99 /**
100 * Returns the format of the current request (defaults is "html")
101 *
102 * @return string $format
103 * @throws \RuntimeException
104 * @api
105 */
106 public function getFormat()
107 {
108 if ($this->baseRenderingContext instanceof RenderingContext) {
109 return $this->baseRenderingContext->getControllerContext()->getRequest()->getFormat();
110 }
111 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251887);
112 }
113
114 /**
115 * Returns the current request object
116 *
117 * @return WebRequest
118 * @throws \RuntimeException
119 */
120 public function getRequest()
121 {
122 if ($this->baseRenderingContext instanceof RenderingContext) {
123 return $this->baseRenderingContext->getControllerContext()->getRequest();
124 }
125 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251888);
126 }
127
128 /**
129 * Sets the absolute path to a Fluid template file
130 *
131 * @param string $templatePathAndFilename Fluid template path
132 * @api
133 */
134 public function setTemplatePathAndFilename($templatePathAndFilename)
135 {
136 $this->baseRenderingContext->getTemplatePaths()->setTemplatePathAndFilename($templatePathAndFilename);
137 }
138
139 /**
140 * Returns the absolute path to a Fluid template file if it was specified with setTemplatePathAndFilename() before
141 *
142 * @return string Fluid template path
143 * @throws \RuntimeException
144 * @api
145 */
146 public function getTemplatePathAndFilename()
147 {
148 $templatePaths = $this->baseRenderingContext->getTemplatePaths();
149 if ($templatePaths instanceof TemplatePaths) {
150 return $templatePaths->getTemplatePathAndFilename();
151 }
152 throw new \RuntimeException('The template paths storage must be of type ' . TemplatePaths::class, 1482251889);
153 }
154
155 /**
156 * Sets the Fluid template source
157 * You can use setTemplatePathAndFilename() alternatively if you only want to specify the template path
158 *
159 * @param string $templateSource Fluid template source code
160 * @api
161 */
162 public function setTemplateSource($templateSource)
163 {
164 $this->baseRenderingContext->getTemplatePaths()->setTemplateSource($templateSource);
165 }
166
167 /**
168 * Set the root path(s) to the templates.
169 *
170 * @param string[] $templateRootPaths Root paths to the templates.
171 * @api
172 */
173 public function setTemplateRootPaths(array $templateRootPaths)
174 {
175 $this->baseRenderingContext->getTemplatePaths()->setTemplateRootPaths($templateRootPaths);
176 }
177
178 /**
179 * Set the root path(s) to the layouts.
180 *
181 * @param string[] $layoutRootPaths Root path to the layouts
182 * @api
183 */
184 public function setLayoutRootPaths(array $layoutRootPaths)
185 {
186 $this->baseRenderingContext->getTemplatePaths()->setLayoutRootPaths($layoutRootPaths);
187 }
188
189 /**
190 * Resolves the layout root to be used inside other paths.
191 *
192 * @return string Fluid layout root path
193 * @throws InvalidTemplateResourceException
194 * @api
195 */
196 public function getLayoutRootPaths()
197 {
198 return $this->baseRenderingContext->getTemplatePaths()->getLayoutRootPaths();
199 }
200
201 /**
202 * Set the root path(s) to the partials.
203 * If set, overrides the one determined from $this->partialRootPathPattern
204 *
205 * @param string[] $partialRootPaths Root paths to the partials. If set, overrides the one determined from $this->partialRootPathPattern
206 * @api
207 */
208 public function setPartialRootPaths(array $partialRootPaths)
209 {
210 $this->baseRenderingContext->getTemplatePaths()->setPartialRootPaths($partialRootPaths);
211 }
212
213 /**
214 * Returns the absolute path to the folder that contains Fluid partial files
215 *
216 * @return string Fluid partial root path
217 * @throws InvalidTemplateResourceException
218 * @api
219 */
220 public function getPartialRootPaths()
221 {
222 return $this->baseRenderingContext->getTemplatePaths()->getPartialRootPaths();
223 }
224
225 /**
226 * Checks whether a template can be resolved for the current request
227 *
228 * @return bool
229 * @api
230 */
231 public function hasTemplate()
232 {
233 try {
234 $this->baseRenderingContext->getTemplatePaths()->getTemplateSource(
235 $this->baseRenderingContext->getControllerName(),
236 $this->baseRenderingContext->getControllerAction()
237 );
238 return true;
239 } catch (InvalidTemplateResourceException $e) {
240 return false;
241 }
242 }
243 }