Skip to content

Commit

Permalink
[TASK] cleanup pageview layer
Browse files Browse the repository at this point in the history
  • Loading branch information
achimfritz committed Jan 19, 2025
1 parent 580f871 commit 58a0f68
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 176 deletions.
4 changes: 0 additions & 4 deletions Build/phpstan-baseline-13.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Class TYPO3\\\\CMS\\\\Core\\\\Database\\\\Query\\\\Restriction\\\\FrontendWorkspaceRestriction not found\\.$#"
count: 1
path: ../Classes/Domain/Factory/Database.php

-
message: "#^Constant LF not found\\.$#"
Expand Down
34 changes: 4 additions & 30 deletions Classes/Domain/Factory/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@
* of the License, or any later version.
*/

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendWorkspaceRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -48,33 +42,13 @@ public function __construct(Context $context)
protected function getQueryBuilder(): QueryBuilder
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
if ($this->getServerRequest() instanceof ServerRequestInterface
&& ApplicationType::fromRequest($this->getServerRequest())->isFrontend()
) {
$queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
if ((GeneralUtility::makeInstance(Typo3Version::class))->getMajorVersion() < 12) {
// do not use FrontendWorkspaceRestriction
$queryBuilder->getRestrictions()
->removeByType(FrontendWorkspaceRestriction::class)
->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->workspaceId));
}
if ($this->workspaceId > 0) {
$queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
}
} else {
$queryBuilder->getRestrictions()
->removeAll()
->add(GeneralUtility::makeInstance(DeletedRestriction::class))
->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->workspaceId));
}
$queryBuilder->getRestrictions()
->removeAll()
->add(GeneralUtility::makeInstance(DeletedRestriction::class))
->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->workspaceId));
return $queryBuilder;
}

protected function getServerRequest(): ?ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'] ?? null;
}

public function fetchRecordsByPidAndLanguage(int $pid, int $language): array
{
$queryBuilder = $this->getQueryBuilder();
Expand Down
34 changes: 33 additions & 1 deletion Classes/Domain/Factory/PageView/Backend/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use B13\Container\Tca\Registry;
use TYPO3\CMS\Core\Context\Context;

class ContainerFactory extends \B13\Container\Domain\Factory\PageView\ContainerFactory
class ContainerFactory extends \B13\Container\Domain\Factory\ContainerFactory
{
/**
* @var ContentStorage
Expand All @@ -32,4 +32,36 @@ public function __construct(
parent::__construct($database, $tcaRegistry, $context);
$this->contentStorage = $contentStorage;
}

protected function children(array $containerRecord, int $language): array
{
return $this->contentStorage->getContainerChildren($containerRecord, $language);
}

protected function localizedRecordsByDefaultRecords(array $defaultRecords, int $language): array
{
$childRecords = parent::localizedRecordsByDefaultRecords($defaultRecords, $language);
return $this->contentStorage->workspaceOverlay($childRecords);
}

protected function containerByUid(int $uid): ?array
{
$record = $this->database->fetchOneRecord($uid);
if ($record === null) {
return null;
}
return $this->contentStorage->containerRecordWorkspaceOverlay($record);
}

protected function defaultContainer(array $localizedContainer): ?array
{
if (isset($localizedContainer['_ORIG_uid'])) {
$localizedContainer = $this->database->fetchOneRecord((int)$localizedContainer['uid']);
}
$defaultRecord = $this->database->fetchOneDefaultRecord($localizedContainer);
if ($defaultRecord === null) {
return null;
}
return $this->contentStorage->containerRecordWorkspaceOverlay($defaultRecord);
}
}
65 changes: 64 additions & 1 deletion Classes/Domain/Factory/PageView/Backend/ContentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,74 @@
* of the License, or any later version.
*/

use B13\Container\Domain\Factory\Database;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Versioning\VersionState;

class ContentStorage extends \B13\Container\Domain\Factory\PageView\ContentStorage
class ContentStorage
{
/**
* @var ?mixed[]
*/
protected $records;

/**
* @var Database
*/
protected $database;

/**
* @var int
*/
protected $workspaceId = 0;

public function __construct(Database $database, Context $context)
{
$this->database = $database;
$this->workspaceId = (int)$context->getPropertyFromAspect('workspace', 'id');
}

protected function buildRecords(int $pid, int $language): array
{
$records = $this->database->fetchRecordsByPidAndLanguage($pid, $language);
$records = $this->workspaceOverlay($records);
$records = $this->recordsByContainer($records);
return $records;
}

protected function recordsByContainer(array $records): array
{
$recordsByContainer = [];
foreach ($records as $record) {
if ($record['tx_container_parent'] > 0) {
if (empty($recordsByContainer[$record['tx_container_parent']])) {
$recordsByContainer[$record['tx_container_parent']] = [];
}
$recordsByContainer[$record['tx_container_parent']][] = $record;
}
}
return $recordsByContainer;
}

public function getContainerChildren(array $containerRecord, int $language): array
{
$pid = (int)$containerRecord['pid'];
if (isset($containerRecord['t3ver_oid']) && $containerRecord['t3ver_oid'] > 0) {
$defaultContainerRecord = $this->database->fetchOneRecord((int)$containerRecord['t3ver_oid']);
$uid = (int)$defaultContainerRecord['uid'];
} else {
$uid = (int)$containerRecord['uid'];
}
if (!isset($this->records[$pid][$language])) {
$this->records[$pid][$language] = $this->buildRecords($pid, $language);
}
if (empty($this->records[$pid][$language][$uid])) {
return [];
}
return $this->records[$pid][$language][$uid];
}

public function workspaceOverlay(array $records): array
{
$filtered = [];
Expand Down
53 changes: 0 additions & 53 deletions Classes/Domain/Factory/PageView/ContainerFactory.php

This file was deleted.

84 changes: 0 additions & 84 deletions Classes/Domain/Factory/PageView/ContentStorage.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace B13\Container\Tests\Unit\Domain\Factory\PageView;
namespace B13\Container\Tests\Unit\Domain\Factory\PageView\Backend;

/*
* This file is part of TYPO3 CMS-based extension "container" by b13.
Expand All @@ -13,7 +13,8 @@
*/

use B13\Container\Domain\Factory\Database;
use B13\Container\Domain\Factory\PageView\ContainerFactory;
use B13\Container\Domain\Factory\PageView\Backend\ContainerFactory;
use B13\Container\Domain\Factory\PageView\Backend\ContentStorage;
use B13\Container\Tca\Registry;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
Expand All @@ -34,8 +35,9 @@ public function containerByUidReturnsNullIfNoRecordInDatabaseIsFound(): void
$database->expects(self::once())->method('fetchOneRecord')->with(1)->willReturn(null);
$tcaRegistry = $this->getMockBuilder(Registry::class)->disableOriginalConstructor()->getMock();
$context = $this->getMockBuilder(Context::class)->getMock();
$contentStorage = $this->getMockBuilder(ContentStorage::class)->disableOriginalConstructor()->getMock();
$containerFactory = $this->getMockBuilder($this->buildAccessibleProxy(ContainerFactory::class))
->setConstructorArgs(['database' => $database, 'tcaRegistry' => $tcaRegistry, 'context' => $context])
->setConstructorArgs(['database' => $database, 'tcaRegistry' => $tcaRegistry, 'context' => $context, 'contentStorage' => $contentStorage])
->onlyMethods([])
->getMock();
self::assertNull($containerFactory->_call('containerByUid', 1));
Expand Down

0 comments on commit 58a0f68

Please sign in to comment.