[BUGFIX] Add missing getTemplatePathAndFilename() in TemplatePaths
[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 * @return void
88 * @throws \RuntimeException
89 * @api
90 */
91 public function setFormat($format)
92 {
93 if ($this->baseRenderingContext instanceof RenderingContext) {
94 $this->baseRenderingContext->getControllerContext()->getRequest()->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 * @return void
134 * @api
135 */
136 public function setTemplatePathAndFilename($templatePathAndFilename)
137 {
138 $this->baseRenderingContext->getTemplatePaths()->setTemplatePathAndFilename($templatePathAndFilename);
139 }
140
141 /**
142 * Returns the absolute path to a Fluid template file if it was specified with setTemplatePathAndFilename() before
143 *
144 * @return string Fluid template path
145 * @throws \RuntimeException
146 * @api
147 */
148 public function getTemplatePathAndFilename()
149 {
150 $templatePaths = $this->baseRenderingContext->getTemplatePaths();
151 if ($templatePaths instanceof TemplatePaths) {
152 return $templatePaths->getTemplatePathAndFilename();
153 }
154 throw new \RuntimeException('The template paths storage must be of type ' . TemplatePaths::class, 1482251889);
155 }
156
157 /**
158 * Sets the Fluid template source
159 * You can use setTemplatePathAndFilename() alternatively if you only want to specify the template path
160 *
161 * @param string $templateSource Fluid template source code
162 * @return void
163 * @api
164 */
165 public function setTemplateSource($templateSource)
166 {
167 $this->baseRenderingContext->getTemplatePaths()->setTemplateSource($templateSource);
168 }
169
170 /**
171 * Set the root path(s) to the templates.
172 *
173 * @param string[] $templateRootPaths Root paths to the templates.
174 * @return void
175 * @api
176 */
177 public function setTemplateRootPaths(array $templateRootPaths)
178 {
179 $this->baseRenderingContext->getTemplatePaths()->setTemplateRootPaths($templateRootPaths);
180 }
181
182 /**
183 * Set the root path(s) to the layouts.
184 *
185 * @param string[] $layoutRootPaths Root path to the layouts
186 * @return void
187 * @api
188 */
189 public function setLayoutRootPaths(array $layoutRootPaths)
190 {
191 $this->baseRenderingContext->getTemplatePaths()->setLayoutRootPaths($layoutRootPaths);
192 }
193
194 /**
195 * Resolves the layout root to be used inside other paths.
196 *
197 * @return string Fluid layout root path
198 * @throws InvalidTemplateResourceException
199 * @api
200 */
201 public function getLayoutRootPaths()
202 {
203 return $this->baseRenderingContext->getTemplatePaths()->getLayoutRootPaths();
204 }
205
206 /**
207 * Set the root path(s) to the partials.
208 * If set, overrides the one determined from $this->partialRootPathPattern
209 *
210 * @param string[] $partialRootPaths Root paths to the partials. If set, overrides the one determined from $this->partialRootPathPattern
211 * @return void
212 * @api
213 */
214 public function setPartialRootPaths(array $partialRootPaths)
215 {
216 $this->baseRenderingContext->getTemplatePaths()->setPartialRootPaths($partialRootPaths);
217 }
218
219 /**
220 * Returns the absolute path to the folder that contains Fluid partial files
221 *
222 * @return string Fluid partial root path
223 * @throws InvalidTemplateResourceException
224 * @api
225 */
226 public function getPartialRootPaths()
227 {
228 return $this->baseRenderingContext->getTemplatePaths()->getPartialRootPaths();
229 }
230
231 /**
232 * Checks whether a template can be resolved for the current request
233 *
234 * @return bool
235 * @api
236 */
237 public function hasTemplate()
238 {
239 try {
240 $this->baseRenderingContext->getTemplatePaths()->getTemplateSource(
241 $this->baseRenderingContext->getControllerName(),
242 $this->baseRenderingContext->getControllerAction()
243 );
244 return true;
245 } catch (InvalidTemplateResourceException $e) {
246 return false;
247 }
248 }
249 }