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

Fix duplicate identifiers #9

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 36 additions & 12 deletions Classes/Domain/Repository/AbstractDatabaseResourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class AbstractDatabaseResourceRepository implements ApiResourceReposito

public const ALLOWED_FILTER_OPERATORS = ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'in'];

private const DUPLICATE_IDENTIFIER_SEPARATOR = '#';

/** @var string */
protected $identifier = self::DEFAULT_IDENTIFIER;

Expand Down Expand Up @@ -87,7 +89,8 @@ public function findByFiltersWithCursor(array $filters, int $limit, ?string $cur

$result = $query->execute()->fetchAllAssociative();

$result = $this->resolveOverlay($context, $result);
$result = \array_map($this->createDeduplicator(), $result);
$result = \array_map($this->createOverlayMapper($context), $result);

$nextCursor = !empty($result) ? \end($result)[$this->identifier] : null;

Expand All @@ -103,7 +106,7 @@ public function findOneByIdentifier($identifier, $context = null): ?array

$result = $query->execute()->fetchAssociative() ?: [];

$result = $this->resolveOverlay($context, $result);
$result = $this->createOverlayMapper($context)($result);

if ($result) {
return $this->createMetaMapper()($result);
Expand All @@ -119,7 +122,9 @@ public function findByIdentifiers(array $identifiers, $context = null, array $fi

$result = $query->execute()->fetchAllAssociative();

$result = $this->resolveOverlay($context, $result);
$result = \array_map($this->createDeduplicator(), $result);

$result = \array_map($this->createOverlayMapper($context), $result);

return \array_map($this->createMetaMapper(), $result);
}
Expand All @@ -133,9 +138,26 @@ public function findByPageIdentifier($pageIdentifier): array

// TODO: add overlay?

$result = \array_map($this->createDeduplicator(), $result);

return \array_map($this->createMetaMapper(), $result);
}

protected function createDeduplicator(): \Closure
{
$countPerId = [];
return function (array $resource) use (&$countPerId): array {
$identifier = $resource[$this->identifier];
$countPerId[$identifier] = isset($countPerId[$identifier]) ? $countPerId[$identifier] + 1 : 0;

if ($countPerId[$identifier] > 0) {
$resource[$this->identifier] = $identifier . self::DUPLICATE_IDENTIFIER_SEPARATOR . $countPerId[$identifier];
}

return $resource;
};
}

protected function createMetaMapper(): \Closure
{
$tableName = $this->tableName;
Expand All @@ -155,15 +177,17 @@ protected function createMetaMapper(): \Closure
};
}

protected function resolveOverlay(?Context $context, array $result): array
protected function createOverlayMapper(?Context $context): \Closure
{
if (null !== $context && $result) {
$pageRepository = GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Domain\Repository\PageRepository::class,
$context
);
$overlayResult = $pageRepository->getLanguageOverlay($this->tableName, $result);
}
return $overlayResult ?? $result;
return function (array $resource) use ($context): array {
if (null !== $context && $resource) {
$pageRepository = GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Domain\Repository\PageRepository::class,
$context
);
$overlayResult = $pageRepository->getLanguageOverlay($this->tableName, $resource);
}
return $overlayResult ?? $resource;
};
}
}
2 changes: 0 additions & 2 deletions Classes/Domain/Transformer/FileReferenceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

class FileReferenceTransformer extends TransformerAbstract
{
protected $defaultIncludes = [];

/** @var string */
protected $identifier;

Expand Down
3 changes: 2 additions & 1 deletion Classes/IncludeHandler/PageRelationIncludeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DFAU\ToujouApi\Configuration\ConfigurationManager;
use DFAU\ToujouApi\Domain\Repository\PageRelationRepository;
use DFAU\ToujouApi\Domain\Repository\PageRepository;
use DFAU\ToujouApi\Transformer\ResourceTransformerInterface;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Scope;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function handleInclude(Scope $scope, string $includeName, $data, callable
throw new \InvalidArgumentException('The given repository "' . \get_class($repository) . '" has to implement the "' . \DFAU\ToujouApi\Domain\Repository\PageRelationRepository::class . '".', 1563210118);
}

/** @var ResourceInterface $transformer */
/** @var ResourceTransformerInterface $transformer */
$transformer = $cascader->create($resourceDefinition['transformer'][\Cascader\Cascader::ARGUMENT_CLASS], $resourceDefinition['transformer']);

return new Collection(
Expand Down
3 changes: 2 additions & 1 deletion Classes/IncludeHandler/TcaResourceIncludeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cascader\Cascader;
use DFAU\ToujouApi\Configuration\ConfigurationManager;
use DFAU\ToujouApi\Transformer\ResourceTransformerInterface;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\ResourceInterface;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function handleInclude(Scope $scope, string $includeName, $data, callable

$repository = $cascader->create($resourceDefinition['repository'][\Cascader\Cascader::ARGUMENT_CLASS], $resourceDefinition['repository']);

/** @var ResourceInterface $transformer */
/** @var ResourceTransformerInterface $transformer */
$transformer = $cascader->create($resourceDefinition['transformer'][\Cascader\Cascader::ARGUMENT_CLASS], $resourceDefinition['transformer']);

if (Item::class === $resourceType) {
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
'depends' => [],
'conflicts' => [],
'suggests' => [],
]
],
];
23 changes: 8 additions & 15 deletions ext_tables.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<?php

defined('TYPO3_MODE') or die();
\defined('TYPO3_MODE') or die();

$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['toujouApiTcaResource'] = [
\TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexProcess::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexProcess::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class =>
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexProcess::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexProcess::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class],
\TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class => $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class],
\DFAU\ToujouApi\Form\DatabaseRowDateTimeFields::class => [
'depends' => [\TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class],
],
Expand Down