Skip to content

Commit

Permalink
[TASK] Move getPageConfiguration from FluxService to PageService
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Aug 12, 2023
1 parent ff1326a commit ebf1e5d
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 346 deletions.
110 changes: 1 addition & 109 deletions Classes/Service/FluxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,25 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Content\TypeDefinition\FluidFileBased\DropInContentTypeDefinition;
use FluidTYPO3\Flux\Core;
use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Form\Transformation\FormDataTransformer;
use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility;
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\TemplatePaths;

/**
* Flux FlexForm integration Service
*
* Main API Service for interacting with Flux-based FlexForms
*/
class FluxService implements SingletonInterface, LoggerAwareInterface
class FluxService implements SingletonInterface
{
use LoggerAwareTrait;

protected ServerRequest $serverRequest;
protected ResourceFactory $resourceFactory;
protected FormDataTransformer $transformer;
protected FlexFormService $flexFormService;

public function __construct(
ServerRequest $serverRequest,
ResourceFactory $resourceFactory,
FormDataTransformer $transformer,
FlexFormService $flexFormService
) {
$this->serverRequest = $serverRequest;
$this->resourceFactory = $resourceFactory;
$this->transformer = $transformer;
$this->flexFormService = $flexFormService;
}
Expand Down Expand Up @@ -90,92 +70,4 @@ public function convertFlexFormContentToArray(
}
return $settings;
}

public function getPageConfiguration(?string $extensionName = null): array
{
if (null !== $extensionName && true === empty($extensionName)) {
// Note: a NULL extensionName means "fetch ALL defined collections" whereas
// an empty value that is not null indicates an incorrect caller. Instead
// of returning ALL paths here, an empty array is the proper return value.
// However, dispatch a debug message to inform integrators of the problem.
if ($this->logger instanceof LoggerInterface) {
$this->logger->log(
'notice',
'Template paths have been attempted fetched using an empty value that is NOT NULL in ' .
get_class($this) . '. This indicates a potential problem with your TypoScript configuration - a ' .
'value which is expected to be an array may be defined as a string. This error is not fatal but ' .
'may prevent the affected collection (which cannot be identified here) from showing up'
);
}
return [];
}

$plugAndPlayEnabled = ExtensionConfigurationUtility::getOption(
ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY
);
$plugAndPlayDirectory = ExtensionConfigurationUtility::getOption(
ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY
);
if (!is_scalar($plugAndPlayDirectory)) {
return [];
}
$plugAndPlayTemplatesDirectory = trim((string) $plugAndPlayDirectory, '/.') . '/';
if ($plugAndPlayEnabled && $extensionName === 'Flux') {
return [
TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [
$plugAndPlayTemplatesDirectory
. DropInContentTypeDefinition::TEMPLATES_DIRECTORY
. DropInContentTypeDefinition::PAGE_DIRECTORY
],
TemplatePaths::CONFIG_PARTIALROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY
],
TemplatePaths::CONFIG_LAYOUTROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY
],
];
}
if (null !== $extensionName) {
$templatePaths = $this->createTemplatePaths($extensionName);
return $templatePaths->toArray();
}
$configurations = [];
$registeredExtensionKeys = Core::getRegisteredProviderExtensionKeys('Page');
foreach ($registeredExtensionKeys as $registeredExtensionKey) {
$templatePaths = $this->createTemplatePaths($registeredExtensionKey);
$configurations[$registeredExtensionKey] = $templatePaths->toArray();
}
if ($plugAndPlayEnabled) {
$configurations['FluidTYPO3.Flux'] = array_replace(
$configurations['FluidTYPO3.Flux'] ?? [],
[
TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [
$plugAndPlayTemplatesDirectory
. DropInContentTypeDefinition::TEMPLATES_DIRECTORY
. DropInContentTypeDefinition::PAGE_DIRECTORY
],
TemplatePaths::CONFIG_PARTIALROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY
],
TemplatePaths::CONFIG_LAYOUTROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY
],
]
);
}
return $configurations;
}

/**
* @codeCoverageIgnore
*/
protected function createTemplatePaths(string $registeredExtensionKey): TemplatePaths
{
/** @var TemplatePaths $templatePaths */
$templatePaths = GeneralUtility::makeInstance(
TemplatePaths::class,
ExtensionNamingUtility::getExtensionKey($registeredExtensionKey)
);
return $templatePaths;
}
}
86 changes: 79 additions & 7 deletions Classes/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Content\TypeDefinition\FluidFileBased\DropInContentTypeDefinition;
use FluidTYPO3\Flux\Core;
use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Provider\PageProvider;
use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility;
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
use FluidTYPO3\Flux\ViewHelpers\FormViewHelper;
use Psr\Log\LoggerAwareInterface;
Expand Down Expand Up @@ -37,19 +40,13 @@ class PageService implements SingletonInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

protected ConfigurationManagerInterface $configurationManager;
protected FluxService $configurationService;
protected WorkspacesAwareRecordService $workspacesAwareRecordService;
protected FrontendInterface $runtimeCache;

public function __construct(
ConfigurationManagerInterface $configurationManager,
FluxService $configurationService,
WorkspacesAwareRecordService $recordService,
CacheManager $cacheManager
) {
$this->configurationManager = $configurationManager;
$this->configurationService = $configurationService;
$this->workspacesAwareRecordService = $recordService;
$this->runtimeCache = $cacheManager->getCache('runtime');
}
Expand Down Expand Up @@ -137,6 +134,81 @@ public function getPageFlexFormSource(int $pageUid): ?string
return $page['tx_fed_page_flexform'] ?? null;
}

public function getPageConfiguration(?string $extensionName = null): array
{
if (null !== $extensionName && true === empty($extensionName)) {
// Note: a NULL extensionName means "fetch ALL defined collections" whereas
// an empty value that is not null indicates an incorrect caller. Instead
// of returning ALL paths here, an empty array is the proper return value.
// However, dispatch a debug message to inform integrators of the problem.
if ($this->logger instanceof LoggerInterface) {
$this->logger->log(
'notice',
'Template paths have been attempted fetched using an empty value that is NOT NULL in ' .
get_class($this) . '. This indicates a potential problem with your TypoScript configuration - a ' .
'value which is expected to be an array may be defined as a string. This error is not fatal but ' .
'may prevent the affected collection (which cannot be identified here) from showing up'
);
}
return [];
}

$plugAndPlayEnabled = ExtensionConfigurationUtility::getOption(
ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY
);
$plugAndPlayDirectory = ExtensionConfigurationUtility::getOption(
ExtensionConfigurationUtility::OPTION_PLUG_AND_PLAY_DIRECTORY
);
if (!is_scalar($plugAndPlayDirectory)) {
return [];
}
$plugAndPlayTemplatesDirectory = trim((string) $plugAndPlayDirectory, '/.') . '/';
if ($plugAndPlayEnabled && $extensionName === 'Flux') {
return [
TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [
$plugAndPlayTemplatesDirectory
. DropInContentTypeDefinition::TEMPLATES_DIRECTORY
. DropInContentTypeDefinition::PAGE_DIRECTORY
],
TemplatePaths::CONFIG_PARTIALROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY
],
TemplatePaths::CONFIG_LAYOUTROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY
],
];
}
if (null !== $extensionName) {
$templatePaths = $this->createTemplatePaths($extensionName);
return $templatePaths->toArray();
}
$configurations = [];
$registeredExtensionKeys = Core::getRegisteredProviderExtensionKeys('Page');
foreach ($registeredExtensionKeys as $registeredExtensionKey) {
$templatePaths = $this->createTemplatePaths($registeredExtensionKey);
$configurations[$registeredExtensionKey] = $templatePaths->toArray();
}
if ($plugAndPlayEnabled) {
$configurations['FluidTYPO3.Flux'] = array_replace(
$configurations['FluidTYPO3.Flux'] ?? [],
[
TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [
$plugAndPlayTemplatesDirectory
. DropInContentTypeDefinition::TEMPLATES_DIRECTORY
. DropInContentTypeDefinition::PAGE_DIRECTORY
],
TemplatePaths::CONFIG_PARTIALROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::PARTIALS_DIRECTORY
],
TemplatePaths::CONFIG_LAYOUTROOTPATHS => [
$plugAndPlayTemplatesDirectory . DropInContentTypeDefinition::LAYOUTS_DIRECTORY
],
]
);
}
return $configurations;
}

/**
* Gets a list of usable Page Templates from defined page template TypoScript.
* Returns a list of Form instances indexed by the path ot the template file.
Expand All @@ -152,7 +224,7 @@ public function getAvailablePageTemplateFiles(): array
if ($fromCache) {
return $fromCache;
}
$typoScript = $this->configurationService->getPageConfiguration();
$typoScript = $this->getPageConfiguration();
$output = [];

/** @var TemplateView $view */
Expand Down
21 changes: 0 additions & 21 deletions Tests/Fixtures/Classes/DummyFluxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,20 @@
namespace FluidTYPO3\Flux\Tests\Fixtures\Classes;

use FluidTYPO3\Flux\Form\Transformation\FormDataTransformer;
use FluidTYPO3\Flux\Provider\ProviderResolver;
use FluidTYPO3\Flux\Service\FluxService;
use PHPUnit\Framework\MockObject\Generator;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Service\FlexFormService;

class DummyFluxService extends FluxService
{
public function __construct()
{
$this->serverRequest = $this->createMock(ServerRequest::class);
$this->resourceFactory = $this->createMock(ResourceFactory::class);
$this->providerResolver = $this->createMock(ProviderResolver::class);
$this->transformer = $this->createMock(FormDataTransformer::class);
$this->flexFormService = $this->createMock(FlexFormService::class);
$this->logger = $this->createMock(LoggerInterface::class);
}

public function setServerRequest(ServerRequest $serverRequest): void
{
$this->serverRequest = $serverRequest;
}

public function setResourceFactory(ResourceFactory $resourceFactory): void
{
$this->resourceFactory = $resourceFactory;
}

public function setProviderResolver(ProviderResolver $providerResolver): void
{
$this->providerResolver = $providerResolver;
}

public function setFormDataTransformer(FormDataTransformer $transformer): void
{
$this->transformer = $transformer;
Expand Down
12 changes: 0 additions & 12 deletions Tests/Fixtures/Classes/DummyPageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@ class DummyPageService extends PageService
{
public function __construct()
{
$this->configurationManager = $this->createMock(ConfigurationManagerInterface::class);
$this->configurationService = new DummyFluxService();
$this->workspacesAwareRecordService = $this->createMock(WorkspacesAwareRecordService::class);
$this->runtimeCache = $this->createMock(FrontendInterface::class);
}

public function setConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
}

public function setConfigurationService(DummyFluxService $configurationService): void
{
$this->configurationService = $configurationService;
}

public function setWorkspacesAwareRecordService(WorkspacesAwareRecordService $workspacesAwareRecordService): void
{
$this->workspacesAwareRecordService = $workspacesAwareRecordService;
Expand Down
Loading

0 comments on commit ebf1e5d

Please sign in to comment.