[BUGFIX] Properly set format of StandaloneView
[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 $this->baseRenderingContext->getTemplatePaths()->setFormat($format);
95 } else {
96 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251886);
97 }
98 }
99
100 /**
101 * Returns the format of the current request (defaults is "html")
102 *
103 * @return string $format
104 * @throws \RuntimeException
105 * @api
106 */
107 public function getFormat()
108 {
109 if ($this->baseRenderingContext instanceof RenderingContext) {
110 return $this->baseRenderingContext->getControllerContext()->getRequest()->getFormat();
111 }
112 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251887);
113 }
114
115 /**
116 * Returns the current request object
117 *
118 * @return WebRequest
119 * @throws \RuntimeException
120 */
121 public function getRequest()
122 {
123 if ($this->baseRenderingContext instanceof RenderingContext) {
124 return $this->baseRenderingContext->getControllerContext()->getRequest();
125 }
126 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251888);
127 }
128
129 /**
130 * Sets the absolute path to a Fluid template file
131 *
132 * @param string $templatePathAndFilename Fluid template path
133 * @api
134 */
135 public function setTemplatePathAndFilename($templatePathAndFilename)
136 {
137 $this->baseRenderingContext->getTemplatePaths()->setTemplatePathAndFilename($templatePathAndFilename);
138 }
139
140 /**
141 * Returns the absolute path to a Fluid template file if it was specified with setTemplatePathAndFilename() before
142 *
143 * @return string Fluid template path
144 * @throws \RuntimeException
145 * @api
146 */
147 public function getTemplatePathAndFilename()
148 {
149 $templatePaths = $this->baseRenderingContext->getTemplatePaths();
150 if ($templatePaths instanceof TemplatePaths) {
151 return $templatePaths->getTemplatePathAndFilename();
152 }
153 throw new \RuntimeException('The template paths storage must be of type ' . TemplatePaths::class, 1482251889);
154 }
155
156 /**
157 * Sets the Fluid template source
158 * You can use setTemplatePathAndFilename() alternatively if you only want to specify the template path
159 *
160 * @param string $templateSource Fluid template source code
161 * @api
162 */
163 public function setTemplateSource($templateSource)
164 {
165 $this->baseRenderingContext->getTemplatePaths()->setTemplateSource($templateSource);
166 }
167
168 /**
169 * Set the root path(s) to the templates.
170 *
171 * @param string[] $templateRootPaths Root paths to the templates.
172 * @api
173 */
174 public function setTemplateRootPaths(array $templateRootPaths)
175 {
176 $this->baseRenderingContext->getTemplatePaths()->setTemplateRootPaths($templateRootPaths);
177 }
178
179 /**
180 * Set the root path(s) to the layouts.
181 *
182 * @param string[] $layoutRootPaths Root path to the layouts
183 * @api
184 */
185 public function setLayoutRootPaths(array $layoutRootPaths)
186 {
187 $this->baseRenderingContext->getTemplatePaths()->setLayoutRootPaths($layoutRootPaths);
188 }
189
190 /**
191 * Resolves the layout root to be used inside other paths.
192 *
193 * @return array Fluid layout root paths
194 * @throws InvalidTemplateResourceException
195 * @api
196 */
197 public function getLayoutRootPaths()
198 {
199 return $this->baseRenderingContext->getTemplatePaths()->getLayoutRootPaths();
200 }
201
202 /**
203 * Set the root path(s) to the partials.
204 * If set, overrides the one determined from $this->partialRootPathPattern
205 *
206 * @param string[] $partialRootPaths Root paths to the partials. If set, overrides the one determined from $this->partialRootPathPattern
207 * @api
208 */
209 public function setPartialRootPaths(array $partialRootPaths)
210 {
211 $this->baseRenderingContext->getTemplatePaths()->setPartialRootPaths($partialRootPaths);
212 }
213
214 /**
215 * Returns the absolute path to the folder that contains Fluid partial files
216 *
217 * @return array Fluid partial root paths
218 * @throws InvalidTemplateResourceException
219 * @api
220 */
221 public function getPartialRootPaths()
222 {
223 return $this->baseRenderingContext->getTemplatePaths()->getPartialRootPaths();
224 }
225
226 /**
227 * Checks whether a template can be resolved for the current request
228 *
229 * @return bool
230 * @api
231 */
232 public function hasTemplate()
233 {
234 try {
235 $this->baseRenderingContext->getTemplatePaths()->getTemplateSource(
236 $this->baseRenderingContext->getControllerName(),
237 $this->baseRenderingContext->getControllerAction()
238 );
239 return true;
240 } catch (InvalidTemplateResourceException $e) {
241 return false;
242 }
243 }
244 }