Skip to content

Commit

Permalink
Merge pull request #392 from CPS-IT/task/1.x/finalize
Browse files Browse the repository at this point in the history
[!!!][TASK] Mark several classes as `final (readonly)`
  • Loading branch information
eliashaeussler authored Jan 8, 2025
2 parents 35b3ce7 + 2c4a886 commit 3b07803
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 166 deletions.
13 changes: 6 additions & 7 deletions Classes/Cache/HandlebarsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@

namespace Fr\Typo3Handlebars\Cache;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use Symfony\Component\DependencyInjection;
use TYPO3\CMS\Core;

/**
* HandlebarsCache
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[AsAlias('handlebars.cache')]
class HandlebarsCache implements Cache
#[DependencyInjection\Attribute\AsAlias('handlebars.cache')]
final readonly class HandlebarsCache implements Cache
{
public function __construct(
#[Autowire('@cache.handlebars')]
protected readonly FrontendInterface $cache,
#[DependencyInjection\Attribute\Autowire('@cache.handlebars')]
private Core\Cache\Frontend\FrontendInterface $cache,
) {}

public function get(string $template): ?string
Expand Down
2 changes: 1 addition & 1 deletion Classes/Cache/NullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
final class NullCache implements Cache
final readonly class NullCache implements Cache
{
public function get(string $template): ?string
{
Expand Down
17 changes: 8 additions & 9 deletions Classes/Compatibility/View/ExtbaseViewAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@

namespace Fr\Typo3Handlebars\Compatibility\View;

use Fr\Typo3Handlebars\DataProcessing\DataProcessor;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\View\AbstractTemplateView;
use Fr\Typo3Handlebars\DataProcessing;
use TYPO3\CMS\Extbase;
use TYPO3\CMS\Fluid;

/**
* ExtbaseViewAdapter
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
class ExtbaseViewAdapter extends AbstractTemplateView
final class ExtbaseViewAdapter extends Fluid\View\AbstractTemplateView
{
/**
* @var array<string, mixed>
*/
protected array $renderData = [];
private array $renderData = [];

public function __construct(
protected readonly DataProcessor $processor,
private readonly DataProcessing\DataProcessor $processor,
) {
parent::__construct();
}
Expand Down Expand Up @@ -71,11 +70,11 @@ public function render($actionName = null): string
$controller = null;
$request = null;

if ($renderingContext instanceof RenderingContext) {
if ($renderingContext instanceof Fluid\Core\Rendering\RenderingContext) {
$request = $renderingContext->getRequest();
$actionName ??= $renderingContext->getControllerAction();
}
if ($request instanceof Request) {
if ($request instanceof Extbase\Mvc\Request) {
$controller = $request->getControllerObjectName();
}

Expand Down
34 changes: 17 additions & 17 deletions Classes/Compatibility/View/HandlebarsViewResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@

namespace Fr\Typo3Handlebars\Compatibility\View;

use Fr\Typo3Handlebars\DataProcessing\DataProcessor;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\GenericViewResolver;
use TYPO3Fluid\Fluid\View\ViewInterface;
use Fr\Typo3Handlebars\DataProcessing;
use Psr\Http\Message;
use Symfony\Component\DependencyInjection;
use TYPO3\CMS\Core;
use TYPO3\CMS\Extbase;
use TYPO3Fluid\Fluid;

/**
* HandlebarsViewResolver
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[Autoconfigure(public: true)]
class HandlebarsViewResolver extends GenericViewResolver
#[DependencyInjection\Attribute\Autoconfigure(public: true)]
final class HandlebarsViewResolver extends Extbase\Mvc\View\GenericViewResolver
{
/**
* @var array<class-string, array<string, DataProcessor>>
* @var array<class-string, array<string, DataProcessing\DataProcessor>>
*/
protected array $processorMap = [];
private array $processorMap = [];

public function resolve(string $controllerObjectName, string $actionName, string $format): ViewInterface
public function resolve(string $controllerObjectName, string $actionName, string $format): Fluid\View\ViewInterface
{
if (!$this->hasProcessor($controllerObjectName, $actionName)) {
return parent::resolve($controllerObjectName, $actionName, $format);
Expand All @@ -53,19 +53,19 @@ public function resolve(string $controllerObjectName, string $actionName, string
return $this->buildView($controllerObjectName, $actionName);
}

protected function buildView(string $controllerClassName, string $actionName): ExtbaseViewAdapter
private function buildView(string $controllerClassName, string $actionName): ExtbaseViewAdapter
{
$processor = $this->getProcessor($controllerClassName, $actionName);

return GeneralUtility::makeInstance(ExtbaseViewAdapter::class, $processor);
return Core\Utility\GeneralUtility::makeInstance(ExtbaseViewAdapter::class, $processor);
}

protected function hasProcessor(string $controllerClassName, string $actionName): bool
private function hasProcessor(string $controllerClassName, string $actionName): bool
{
return $this->getProcessor($controllerClassName, $actionName) !== null;
}

protected function getProcessor(string $controllerClassName, string $actionName): ?DataProcessor
private function getProcessor(string $controllerClassName, string $actionName): ?DataProcessing\DataProcessor
{
if (!\array_key_exists($controllerClassName, $this->processorMap)) {
return null;
Expand All @@ -87,15 +87,15 @@ protected function getProcessor(string $controllerClassName, string $actionName)
}

/**
* @param array<class-string, array<string, DataProcessor>> $processorMap
* @param array<class-string, array<string, DataProcessing\DataProcessor>> $processorMap
*/
public function setProcessorMap(array $processorMap): self
{
$this->processorMap = $processorMap;
return $this;
}

protected function getRequest(): ServerRequestInterface
private function getRequest(): Message\ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
Expand Down
11 changes: 5 additions & 6 deletions Classes/Configuration/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

namespace Fr\Typo3Handlebars\Configuration;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core;

/**
* Extension
Expand All @@ -33,7 +32,7 @@
* @license GPL-2.0-or-later
* @codeCoverageIgnore
*/
final class Extension
final readonly class Extension
{
public const KEY = 'handlebars';
public const NAME = 'Handlebars';
Expand Down Expand Up @@ -61,12 +60,12 @@ public static function registerCaches(): void
public static function loadVendorLibraries(): void
{
// Vendor libraries are already available in Composer mode
if (Environment::isComposerMode()) {
if (Core\Core\Environment::isComposerMode()) {
return;
}

$vendorPharFile = GeneralUtility::getFileAbsFileName('EXT:handlebars/Resources/Private/Libs/vendors.phar');
if (file_exists($vendorPharFile)) {
$vendorPharFile = Core\Utility\GeneralUtility::getFileAbsFileName('EXT:handlebars/Resources/Private/Libs/vendors.phar');
if (is_file($vendorPharFile)) {
require_once 'phar://' . $vendorPharFile . '/vendor/autoload.php';
}
}
Expand Down
4 changes: 1 addition & 3 deletions Classes/Data/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

namespace Fr\Typo3Handlebars\Data;

use Fr\Typo3Handlebars\Data\Response\ProviderResponse;

/**
* DataProvider
*
Expand All @@ -36,5 +34,5 @@ interface DataProvider
/**
* @param array<string, mixed> $data
*/
public function get(array $data): ProviderResponse;
public function get(array $data): Response\ProviderResponse;
}
32 changes: 5 additions & 27 deletions Classes/Data/Response/SimpleProviderResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,20 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*
* @implements \ArrayAccess<string, mixed>
* @extends \ArrayObject<string, mixed>
*/
class SimpleProviderResponse implements ProviderResponse, \ArrayAccess
class SimpleProviderResponse extends \ArrayObject implements ProviderResponse
{
/**
* @param array<string, mixed> $data
*/
public function __construct(
protected array $data = [],
) {}

public function offsetExists($offset): bool
{
return \array_key_exists($offset, $this->data);
}

public function offsetGet($offset): mixed
{
if ($this->offsetExists($offset)) {
return $this->data[$offset];
}
return null;
}

public function offsetSet($offset, mixed $value): void
{
$this->data[$offset] = $value;
}

public function offsetUnset($offset): void
public function __construct(array $data = [])
{
unset($this->data[$offset]);
parent::__construct($data);
}

public function toArray(): array
{
return $this->data;
return $this->getArrayCopy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@

namespace Fr\Typo3Handlebars\DependencyInjection\Compatibility;

use Fr\Typo3Handlebars\Compatibility\View\HandlebarsViewResolver;
use Fr\Typo3Handlebars\DataProcessing\DataProcessor;
use Fr\Typo3Handlebars\Exception\InvalidClassException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use Fr\Typo3Handlebars\Compatibility;
use Fr\Typo3Handlebars\DataProcessing;
use Fr\Typo3Handlebars\Exception;
use Symfony\Component\DependencyInjection;
use TYPO3\CMS\Core;
use TYPO3\CMS\Extbase;

/**
* ExtbaseControllerCompatibilityLayer
Expand All @@ -43,13 +41,13 @@
{
public const TYPE = 'extbase_controller';

private Definition $viewResolverDefinition;
private DependencyInjection\Definition $viewResolverDefinition;

public function __construct(
private ContainerBuilder $container,
private DependencyInjection\ContainerBuilder $container,
) {
$this->viewResolverDefinition = $this->container->getDefinition(HandlebarsViewResolver::class);
$this->validateService(HandlebarsViewResolver::class);
$this->viewResolverDefinition = $this->container->getDefinition(Compatibility\View\HandlebarsViewResolver::class);
$this->validateService(Compatibility\View\HandlebarsViewResolver::class);
}

public function provide(string $processorServiceId, array $configuration): bool
Expand All @@ -64,8 +62,8 @@ public function provide(string $processorServiceId, array $configuration): bool
// Validate controller class name
$this->validateService($controller);

$actions = GeneralUtility::trimExplode(',', $configuration['actions'] ?? '_all', true);
$actionMap = array_fill_keys($actions, new Reference($processorServiceId));
$actions = Core\Utility\GeneralUtility::trimExplode(',', $configuration['actions'] ?? '_all', true);
$actionMap = array_fill_keys($actions, new DependencyInjection\Reference($processorServiceId));

// Merge and apply processor map
$processorMap = $this->buildProcessorMap($controllerClassName, $actionMap);
Expand All @@ -76,15 +74,15 @@ public function provide(string $processorServiceId, array $configuration): bool

// Apply processor map and register method call
$controllerDefinition->removeMethodCall('injectViewResolver');
$controllerDefinition->addMethodCall('injectViewResolver', [new Reference($viewResolverClassName)]);
$controllerDefinition->addMethodCall('injectViewResolver', [new DependencyInjection\Reference($viewResolverClassName)]);

return true;
}

/**
* @param class-string $controllerClassName
* @param array<string, Reference> $actionMap
* @return array<string, array<string, DataProcessor>>
* @param array<string, DependencyInjection\Reference> $actionMap
* @return array<string, array<string, DataProcessing\DataProcessor>>
*/
private function buildProcessorMap(string $controllerClassName, array $actionMap): array
{
Expand Down Expand Up @@ -124,9 +122,9 @@ private function validateConfiguration(array $configuration): void
1632814520
);
}
if (!\in_array(ActionController::class, class_parents($this->container->getDefinition($configuration['controller'])->getClass()) ?: [])) {
if (!\in_array(Extbase\Mvc\Controller\ActionController::class, class_parents($this->container->getDefinition($configuration['controller'])->getClass()) ?: [])) {
throw new \InvalidArgumentException(
\sprintf('Only extbase controllers extending from "%s" are supported, found in: %s', ActionController::class, $configuration['controller']),
\sprintf('Only extbase controllers extending from "%s" are supported, found in: %s', Extbase\Mvc\Controller\ActionController::class, $configuration['controller']),
1632814592
);
}
Expand All @@ -145,10 +143,10 @@ private function validateService(string $serviceId): void
$className = $definition->getClass();

if ($className === null) {
throw InvalidClassException::forService($serviceId);
throw Exception\InvalidClassException::forService($serviceId);
}
if (!class_exists($className)) {
throw InvalidClassException::create($className);
throw Exception\InvalidClassException::create($className);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

namespace Fr\Typo3Handlebars\DependencyInjection\Compatibility;

use Fr\Typo3Handlebars\Exception\UnsupportedTypeException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Fr\Typo3Handlebars\Exception;
use Symfony\Component\DependencyInjection;

/**
* ProcessorCompatibility
Expand All @@ -41,7 +41,7 @@
public function __construct(
private string $serviceId,
private array $tagAttributes,
private ContainerBuilder $container,
private DependencyInjection\ContainerBuilder $container,
) {
$this->validate();
}
Expand All @@ -57,13 +57,13 @@ public function provideCompatibility(): void
}

/**
* @throws UnsupportedTypeException
* @throws Exception\UnsupportedTypeException
*/
private function buildLayerForType(string $type): CompatibilityLayer
{
return match ($type) {
ExtbaseControllerCompatibilityLayer::TYPE => new ExtbaseControllerCompatibilityLayer($this->container),
default => throw UnsupportedTypeException::create($type),
default => throw Exception\UnsupportedTypeException::create($type),
};
}

Expand Down
Loading

0 comments on commit 3b07803

Please sign in to comment.