TYPO3 API  SVNRelease
StandaloneView.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * A standalone template view.
00025  * Should be used as view if you want to use Fluid without Extbase extensions
00026  *
00027  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00028  * @api
00029  */
00030 class Tx_Fluid_View_StandaloneView extends Tx_Fluid_View_AbstractTemplateView {
00031 
00032     /**
00033      * Source code of the Fluid template
00034      * @var string
00035      */
00036     protected $templateSource = NULL;
00037 
00038     /**
00039      * absolute path of the Fluid template
00040      * @var string
00041      */
00042     protected $templatePathAndFilename = NULL;
00043 
00044     /**
00045      * absolute root path of the folder that contains Fluid layouts
00046      * @var string
00047      */
00048     protected $layoutRootPath = NULL;
00049 
00050     /**
00051      * absolute root path of the folder that contains Fluid partials
00052      * @var string
00053      */
00054     protected $partialRootPath = NULL;
00055 
00056     /**
00057      * Constructor
00058      *
00059      * @param tslib_cObj $contentObject The current cObject. If NULL a new instance will be created
00060      */
00061     public function __construct(tslib_cObj $contentObject = NULL) {
00062         if (!t3lib_extMgm::isLoaded('extbase')) {
00063             return 'In the current version you still need to have Extbase installed in order to use the Fluid Standalone view!';
00064         }
00065         $this->initializeAutoloader();
00066         $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
00067 
00068         $configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
00069         if ($contentObject === NULL) {
00070             $contentObject = t3lib_div::makeInstance('tslib_cObj');
00071         }
00072         $configurationManager->setContentObject($contentObject);
00073 
00074         $this->templateParser = $this->objectManager->get('Tx_Fluid_Core_Parser_TemplateParser');
00075         $this->setRenderingContext($this->objectManager->create('Tx_Fluid_Core_Rendering_RenderingContext'));
00076 
00077         $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
00078         $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
00079         $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
00080 
00081         $uriBuilder = $this->objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
00082         $uriBuilder->setRequest($request);
00083 
00084         $controllerContext = $this->objectManager->create('Tx_Extbase_MVC_Controller_ControllerContext');
00085         $controllerContext->setRequest($request);
00086         $controllerContext->setUriBuilder($uriBuilder);
00087         $flashMessageContainer = $this->objectManager->get('Tx_Extbase_MVC_Controller_FlashMessages'); // singleton
00088         $controllerContext->setFlashMessageContainer($flashMessageContainer);
00089         $this->setControllerContext($controllerContext);
00090     }
00091 
00092     /**
00093      * Initializes the Extbase autoloader if it wasn't registered before
00094      *
00095      * @return void
00096      * @see Extbase_Dispatcher::initializeClassLoader()
00097      */
00098     protected function initializeAutoloader() {
00099         if (!class_exists('Tx_Extbase_Utility_ClassLoader', FALSE)) {
00100             $classLoader = t3lib_div::makeInstance('Tx_Extbase_Utility_ClassLoader');
00101             spl_autoload_register(array($classLoader, 'loadClass'));
00102         }
00103     }
00104 
00105     /**
00106      * Sets the format of the current request (default format is "html")
00107      *
00108      * @param string $format
00109      * @return void
00110      * @api
00111      */
00112     public function setFormat($format) {
00113         $this->getRequest()->setFormat($format);
00114     }
00115 
00116     /**
00117      * Returns the format of the current request (defaults is "html")
00118      *
00119      * @return string $format
00120      * @api
00121      */
00122     public function getFormat() {
00123         return $this->getRequest()->getFormat();
00124     }
00125 
00126     /**
00127      * Returns the current request object
00128      *
00129      * @return Tx_Extbase_MVC_Web_Request
00130      */
00131     public function getRequest() {
00132         return $this->controllerContext->getRequest();
00133     }
00134 
00135     /**
00136      * Sets the absolute path to a Fluid template file
00137      *
00138      * @param string $templatePathAndFilename Fluid template path
00139      * @return void
00140      * @api
00141      */
00142     public function setTemplatePathAndFilename($templatePathAndFilename) {
00143         $this->templatePathAndFilename = $templatePathAndFilename;
00144     }
00145 
00146     /**
00147      * Returns the absolute path to a Fluid template file if it was specified with setTemplatePathAndFilename() before
00148      *
00149      * @return string Fluid template path
00150      * @api
00151      */
00152     public function getTemplatePathAndFilename() {
00153         return $this->templatePathAndFilename;
00154     }
00155 
00156     /**
00157      * Sets the Fluid template source
00158      * You can use setTemplatePathAndFilename() alternatively if you only want to specify the template path
00159      *
00160      * @param string $templateSource Fluid template source code
00161      * @return void
00162      * @api
00163      */
00164     public function setTemplateSource($templateSource) {
00165         $this->templateSource = $templateSource;
00166     }
00167 
00168     /**
00169      * Sets the absolute path to the folder that contains Fluid layout files
00170      *
00171      * @param string $layoutRootPath Fluid layout root path
00172      * @return void
00173      * @api
00174      */
00175     public function setLayoutRootPath($layoutRootPath) {
00176         $this->layoutRootPath = $layoutRootPath;
00177     }
00178 
00179     /**
00180      * Returns the absolute path to the folder that contains Fluid layout files
00181      *
00182      * @return string Fluid layout root path
00183      * @api
00184      */
00185     public function getLayoutRootPath() {
00186         if ($this->layoutRootPath === NULL && $this->templatePathAndFilename === NULL) {
00187             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('No layout root path has been specified. Use setLayoutRootPath().', 1288091419);
00188         }
00189         if ($this->layoutRootPath === NULL) {
00190             $this->layoutRootPath = dirname($this->templatePathAndFilename) . '/Layouts';
00191         }
00192         return $this->layoutRootPath;
00193     }
00194 
00195     /**
00196      * Sets the absolute path to the folder that contains Fluid partial files.
00197      *
00198      * @param string $partialRootPath Fluid partial root path
00199      * @return void
00200      * @api
00201      */
00202     public function setPartialRootPath($partialRootPath) {
00203         $this->partialRootPath = $partialRootPath;
00204     }
00205 
00206     /**
00207      * Returns the absolute path to the folder that contains Fluid partial files
00208      *
00209      * @return string Fluid partial root path
00210      * @api
00211      */
00212     public function getPartialRootPath() {
00213         if ($this->partialRootPath === NULL && $this->templatePathAndFilename === NULL) {
00214             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('No partial root path has been specified. Use setPartialRootPath().', 1288094511);
00215         }
00216         if ($this->partialRootPath === NULL) {
00217             $this->partialRootPath = dirname($this->templatePathAndFilename) . '/Partials';
00218         }
00219         return $this->partialRootPath;
00220     }
00221 
00222     /**
00223      * Checks whether a template can be resolved for the current request
00224      *
00225      * @return boolean
00226      * @api
00227      */
00228     public function hasTemplate() {
00229         try {
00230             $this->getTemplateSource();
00231             return TRUE;
00232         } catch (Tx_Fluid_View_Exception_InvalidTemplateResourceException $e) {
00233             return FALSE;
00234         }
00235     }
00236 
00237     /**
00238      * Returns the Fluid template source code
00239      *
00240      * @param string $actionName Name of the action. This argument is not used in this view!
00241      * @return string Fluid template source
00242      * @throws Tx_Fluid_View_Exception_InvalidTemplateResourceException
00243      */
00244     protected function getTemplateSource($actionName = NULL) {
00245         if ($this->templateSource === NULL && $this->templatePathAndFilename === NULL) {
00246             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('No template has been specified. Use either setTemplateSource() or setTemplatePathAndFilename().', 1288085266);
00247         }
00248         if ($this->templateSource === NULL) {
00249             if (!file_exists($this->templatePathAndFilename)) {
00250                 throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Template could not be found at "' . $this->templatePathAndFilename . '".', 1288087061);
00251             }
00252             $this->templateSource = file_get_contents($this->templatePathAndFilename);
00253         }
00254         return $this->templateSource;
00255     }
00256 
00257     /**
00258      * Resolves the path and file name of the layout file, based on
00259      * $this->getLayoutRootPath() and request format and returns the file contents
00260      *
00261      * @param string $layoutName Name of the layout to use. If none given, use "default"
00262      * @return string contents of the layout file if it was found
00263      * @throws Tx_Fluid_View_Exception_InvalidTemplateResourceException
00264      */
00265     protected function getLayoutSource($layoutName = 'default') {
00266         $layoutRootPath = $this->getLayoutRootPath();
00267         if (!is_dir($layoutRootPath)) {
00268             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Layout root path "' . $layoutRootPath . '" does not exist.', 1288092521);
00269         }
00270         $possibleLayoutPaths = array();
00271         $possibleLayoutPaths[] = t3lib_div::fixWindowsFilePath($layoutRootPath . '/' . $layoutName . '.' . $this->getRequest()->getFormat());
00272         $possibleLayoutPaths[] = t3lib_div::fixWindowsFilePath($layoutRootPath . '/' . $layoutName);
00273         $found = FALSE;
00274         foreach($possibleLayoutPaths as $layoutPathAndFilename) {
00275             if (file_exists($layoutPathAndFilename)) {
00276                 $found = TRUE;
00277                 break;
00278             }
00279         }
00280         if ($found !== TRUE) {
00281             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Could not load layout file. Tried following paths: "' . implode('", "', $possibleLayoutPaths) . '".', 1288092555);
00282         }
00283         return file_get_contents($layoutPathAndFilename);
00284     }
00285 
00286     /**
00287      * Resolves the path and file name of the partial file, based on
00288      * $this->getPartialRootPath() and request format and returns the file contents
00289      *
00290      * @param string $partialName The name of the partial
00291      * @return string contents of the layout file if it was found
00292      * @throws Tx_Fluid_View_Exception_InvalidTemplateResourceException
00293      * @author Bastian Waidelich <bastian@typo3.org>
00294      */
00295     protected function getPartialSource($partialName) {
00296         $partialRootPath = $this->getPartialRootPath();
00297         if (!is_dir($partialRootPath)) {
00298             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Partial root path "' . $partialRootPath . '" does not exist.', 1288094648);
00299         }
00300         $possiblePartialPaths = array();
00301         $possiblePartialPaths[] = t3lib_div::fixWindowsFilePath($partialRootPath . '/' . $partialName . '.' . $this->getRequest()->getFormat());
00302         $possiblePartialPaths[] = t3lib_div::fixWindowsFilePath($partialRootPath . '/' . $partialName);
00303         $found = FALSE;
00304         foreach($possiblePartialPaths as $partialPathAndFilename) {
00305             if (file_exists($partialPathAndFilename)) {
00306                 $found = TRUE;
00307                 break;
00308             }
00309         }
00310         if ($found !== TRUE) {
00311             throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Could not load partial file. Tried following paths: "' . implode('", "', $possiblePartialPaths) . '".', 1288092555);
00312         }
00313         return file_get_contents($partialPathAndFilename);
00314     }
00315 
00316 }
00317 
00318 ?>