Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE][PoC] Introduce way to skip fluid templates #552

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Classes/XClass/TemplateView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\XClass;

use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException;

use function extract;
use function ob_end_clean;
use function ob_get_clean;
use function ob_start;

class TemplateView extends \TYPO3\CMS\Fluid\View\TemplateView
{
public function render($actionName = null)
{
if (!ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
return parent::render($actionName);
}

$renderingContext = $this->getCurrentRenderingContext();

if ((int)($renderingContext->getVariableProvider()->get('settings')['phpTemplate'] ?? 0) !== 1) {
return parent::render($actionName);
}

$templatePaths = $renderingContext->getTemplatePaths();
if ($actionName) {
$actionName = ucfirst($actionName);
$renderingContext->setControllerAction($actionName);
}

$templateFile = $templatePaths->resolveTemplateFileForControllerAndActionAndFormat($renderingContext->getControllerName(), $renderingContext->getControllerAction(), 'php');

if ($templateFile === null) {
throw new InvalidTemplateResourceException('Template is not found');
}

return $this->loadTemplate($templateFile, $renderingContext);
}

private function loadTemplate(string $templateFile, RenderingContextInterface $renderingContext): string
{
$__jsonContent = '';

try {
extract($renderingContext->getVariableProvider()->getAll());

ob_start();
include $templateFile;
$__jsonContent = ob_get_clean();
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
}

return $__jsonContent;
}
}
8 changes: 8 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use FriendsOfTYPO3\Headless\Utility\HeadlessFrontendUrlInterface;
use FriendsOfTYPO3\Headless\Utility\UrlUtility;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Form\Controller\FormFrontendController;
use TYPO3\CMS\FrontendLogin\Controller\LoginController;

Expand Down Expand Up @@ -44,4 +46,10 @@

$services->set(HeadlessFrontendUrlInterface::class, UrlUtility::class)->autowire(false);
$services->set(XmlSitemapRenderer::class)->public()->share(false);

$features = GeneralUtility::makeInstance(Features::class);

if ($features->isFeatureEnabled('headless.overrideFluidTemplates')) {
$services->alias(\TYPO3\CMS\Fluid\View\TemplateView::class, \FriendsOfTYPO3\Headless\XClass\TemplateView::class);
}
};
12 changes: 12 additions & 0 deletions Tests/Unit/XClass/Fixtures/Templates/Default/Default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

echo json_encode([
'testKey' => $testValue,
]);
72 changes: 72 additions & 0 deletions Tests/Unit/XClass/TemplateViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Tests\Unit\XClass;

use FriendsOfTYPO3\Headless\XClass\TemplateView;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
use TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException;
use TYPO3Fluid\Fluid\View\TemplatePaths;

use function json_encode;

class TemplateViewTest extends UnitTestCase
{
public function testTemplateNotFoundOrPHPTemplatesDisabledRender(): void
{
$this->expectException(InvalidTemplateResourceException::class);

$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())->withAttribute('applicationType', 1); // fe request

$context = new RenderingContext($this->createMock(ViewHelperResolver::class), $this->createMock(FluidCacheInterface::class), [], []);

$variableProvider = new StandardVariableProvider();
$variableProvider->add('settings', ['phpTemplate' => 1]);

$context->setVariableProvider($variableProvider);
$view = new TemplateView($context);
$view->render();

$variableProvider = new StandardVariableProvider();
$variableProvider->add('settings', ['phpTemplate' => 0]);

$context->setVariableProvider($variableProvider);
$view = new TemplateView($context);
$view->render();
}

public function testTemplateRender(): void
{
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())->withAttribute('applicationType', 1); // fe request

$context = new RenderingContext($this->createMock(ViewHelperResolver::class), $this->createMock(FluidCacheInterface::class), [], []);

$variableProvider = new StandardVariableProvider();
$variableProvider->add('settings', ['phpTemplate' => 1]);
$variableProvider->add('testValue', 'TestingJsonValue');

$context->setVariableProvider($variableProvider);

$templatePaths = $this->createMock(TemplatePaths::class);
$templatePaths->method('resolveTemplateFileForControllerAndActionAndFormat')->willReturn(__DIR__ . '/Fixtures/Templates/Default/Default.php');

$context->setTemplatePaths($templatePaths);

$view = new TemplateView($context);

self::assertSame(json_encode(['testKey' => 'TestingJsonValue']), $view->render());
}
}