[TASK] Streamline phpdoc annotations in EXT:fluid
[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 class StandaloneView extends AbstractTemplateView
32 {
33 /**
34 * @var ObjectManager|null
35 */
36 protected $objectManager;
37
38 /**
39 * Constructor
40 *
41 * @param ContentObjectRenderer $contentObject The current cObject. If NULL a new instance will be created
42 * @throws \InvalidArgumentException
43 * @throws \UnexpectedValueException
44 */
45 public function __construct(ContentObjectRenderer $contentObject = null)
46 {
47 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
48
49 /** @var ConfigurationManagerInterface $configurationManager */
50 $configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
51 if ($contentObject === null) {
52 /** @var ContentObjectRenderer $contentObject */
53 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
54 }
55 $configurationManager->setContentObject($contentObject);
56
57 /** @var WebRequest $request */
58 $request = $this->objectManager->get(WebRequest::class);
59 $request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
60 $request->setBaseUri(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
61 /** @var UriBuilder $uriBuilder */
62 $uriBuilder = $this->objectManager->get(UriBuilder::class);
63 $uriBuilder->setRequest($request);
64 /** @var ControllerContext $controllerContext */
65 $controllerContext = $this->objectManager->get(ControllerContext::class);
66 $controllerContext->setRequest($request);
67 $controllerContext->setUriBuilder($uriBuilder);
68 $renderingContext = $this->objectManager->get(RenderingContext::class, $this);
69 $renderingContext->setControllerContext($controllerContext);
70 parent::__construct($renderingContext);
71 }
72
73 /**
74 * Sets the format of the current request (default format is "html")
75 *
76 * @param string $format
77 * @throws \RuntimeException
78 */
79 public function setFormat($format)
80 {
81 if ($this->baseRenderingContext instanceof RenderingContext) {
82 $this->baseRenderingContext->getControllerContext()->getRequest()->setFormat($format);
83 $this->baseRenderingContext->getTemplatePaths()->setFormat($format);
84 } else {
85 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251886);
86 }
87 }
88
89 /**
90 * Returns the format of the current request (defaults is "html")
91 *
92 * @return string $format
93 * @throws \RuntimeException
94 */
95 public function getFormat()
96 {
97 if ($this->baseRenderingContext instanceof RenderingContext) {
98 return $this->baseRenderingContext->getControllerContext()->getRequest()->getFormat();
99 }
100 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251887);
101 }
102
103 /**
104 * Returns the current request object
105 *
106 * @return WebRequest
107 * @throws \RuntimeException
108 * @internal
109 */
110 public function getRequest()
111 {
112 if ($this->baseRenderingContext instanceof RenderingContext) {
113 return $this->baseRenderingContext->getControllerContext()->getRequest();
114 }
115 throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251888);
116 }
117
118 /**
119 * Returns the absolute path to a Fluid template file if it was specified with setTemplatePathAndFilename() before.
120 * If the template filename was never specified, Fluid attempts to resolve the file based on controller and action.
121 *
122 * NB: If TemplatePaths was previously told to use the specific template path and filename it will short-circuit
123 * and return that template path and filename directly, instead of attempting to resolve it.
124 *
125 * @return string Fluid template path
126 */
127 public function getTemplatePathAndFilename()
128 {
129 $templatePaths = $this->baseRenderingContext->getTemplatePaths();
130 return $templatePaths->resolveTemplateFileForControllerAndActionAndFormat(
131 $this->baseRenderingContext->getControllerName(),
132 $this->baseRenderingContext->getControllerAction(),
133 $templatePaths->getFormat()
134 );
135 }
136
137 /**
138 * Sets the Fluid template source
139 * You can use setTemplatePathAndFilename() alternatively if you only want to specify the template path
140 *
141 * @param string $templateSource Fluid template source code
142 */
143 public function setTemplateSource($templateSource)
144 {
145 $this->baseRenderingContext->getTemplatePaths()->setTemplateSource($templateSource);
146 }
147
148 /**
149 * Checks whether a template can be resolved for the current request
150 *
151 * @return bool
152 */
153 public function hasTemplate()
154 {
155 try {
156 $this->baseRenderingContext->getTemplatePaths()->getTemplateSource(
157 $this->baseRenderingContext->getControllerName(),
158 $this->baseRenderingContext->getControllerAction()
159 );
160 return true;
161 } catch (InvalidTemplateResourceException $e) {
162 return false;
163 }
164 }
165 }