114 lines
3.3 KiB
PHP
114 lines
3.3 KiB
PHP
<?php
|
|
namespace FluidTYPO3\Vhs\ViewHelpers\Render;
|
|
|
|
/*
|
|
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.md file that was distributed with this source code.
|
|
*/
|
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
|
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
|
|
|
|
/**
|
|
* ### Base class for all rendering ViewHelpers.
|
|
*
|
|
* If errors occur they can be graciously ignored and
|
|
* replaced by a small error message or the error itself.
|
|
*
|
|
* @author Claus Due <claus@namelesscoder.net>
|
|
* @package Vhs
|
|
* @subpackage ViewHelpers\Render
|
|
*/
|
|
abstract class AbstractRenderViewHelper extends AbstractViewHelper {
|
|
|
|
/**
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
|
|
*/
|
|
protected $objectManager;
|
|
|
|
/**
|
|
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
|
|
*/
|
|
protected $configurationManager;
|
|
|
|
/**
|
|
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
|
|
* @return void
|
|
*/
|
|
public function injectObjectManager(ObjectManagerInterface $objectManager) {
|
|
$this->objectManager = $objectManager;
|
|
}
|
|
|
|
/**
|
|
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
|
|
* @return void
|
|
*/
|
|
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) {
|
|
$this->configurationManager = $configurationManager;
|
|
}
|
|
|
|
/**
|
|
* Initialize arguments
|
|
*
|
|
* @return void
|
|
*/
|
|
public function initializeArguments() {
|
|
$this->registerArgument('onError', 'string', 'Optional error message to display if error occur while rendering. If NULL, lets the error Exception pass trough (and break rendering)', FALSE, NULL);
|
|
$this->registerArgument('graceful', 'boolean', 'If forced to FALSE, errors are not caught but rather "transmitted" as every other error would be', FALSE, FALSE);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getPreparedNamespaces() {
|
|
$namespaces = array();
|
|
foreach ((array) $this->arguments['namespaces'] as $namespaceIdentifier => $namespace) {
|
|
$addedOverriddenNamespace = '{namespace ' . $namespaceIdentifier . '=' . $namespace . '}';
|
|
array_push($namespaces, $addedOverriddenNamespace);
|
|
}
|
|
return $namespaces;
|
|
}
|
|
|
|
/**
|
|
* @return \TYPO3\CMS\Fluid\View\StandaloneView
|
|
*/
|
|
protected function getPreparedClonedView() {
|
|
$view = $this->getPreparedView();
|
|
$view->setControllerContext(clone $this->controllerContext);
|
|
$view->setFormat($this->controllerContext->getRequest()->getFormat());
|
|
$view->assignMultiple($this->templateVariableContainer->getAll());
|
|
return $view;
|
|
}
|
|
|
|
/**
|
|
* @return \TYPO3\CMS\Fluid\View\StandaloneView
|
|
*/
|
|
protected function getPreparedView() {
|
|
/** @var $view \TYPO3\CMS\Fluid\View\StandaloneView */
|
|
$view = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
|
|
return $view;
|
|
}
|
|
|
|
/**
|
|
* @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view
|
|
* @throws \Exception
|
|
* @return string
|
|
*/
|
|
protected function renderView(ViewInterface $view) {
|
|
try {
|
|
$content = $view->render();
|
|
} catch (\Exception $error) {
|
|
if (!$this->arguments['graceful']) {
|
|
throw $error;
|
|
}
|
|
$content = $error->getMessage() . ' (' . $error->getCode() . ')';
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
}
|