Commit f37a28db authored by Artus Kolanowski's avatar Artus Kolanowski Committed by Benni Mack
Browse files

[TASK] Consolidate code duplications in form data groups

Introduce a new class `OrderedProviderList` which calls providers
with dependencies given by setter.

Resolves: #79512
Releases: master
Change-Id: Icf97e2e6fa5ed88d7b76b7628bb4e9790b615a38
Reviewed-on: https://review.typo3.org/51443

Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 728afa52
......@@ -15,8 +15,6 @@ namespace TYPO3\CMS\Backend\Form\FormDataGroup;
*/
use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -33,24 +31,11 @@ class FlexFormSegment implements FormDataGroupInterface
*/
public function compile(array $result)
{
$dataProvider = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'];
$orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
$orderedDataProvider = $orderingService->orderByDependencies($dataProvider, 'before', 'depends');
$orderedProviderList = GeneralUtility::makeInstance(OrderedProviderList::class);
$orderedProviderList->setProviderList(
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment']
);
foreach ($orderedDataProvider as $providerClassName => $_) {
/** @var FormDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance($providerClassName);
if (!$provider instanceof FormDataProviderInterface) {
throw new \UnexpectedValueException(
'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
1437906441
);
}
$result = $provider->addData($result);
}
return $result;
return $orderedProviderList->compile($result);
}
}
......@@ -15,8 +15,6 @@ namespace TYPO3\CMS\Backend\Form\FormDataGroup;
*/
use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -34,24 +32,11 @@ class InlineParentRecord implements FormDataGroupInterface
*/
public function compile(array $result)
{
$dataProvider = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['inlineParentRecord'];
$orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
$orderedDataProvider = $orderingService->orderByDependencies($dataProvider, 'before', 'depends');
$orderedProviderList = GeneralUtility::makeInstance(OrderedProviderList::class);
$orderedProviderList->setProviderList(
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['inlineParentRecord']
);
foreach ($orderedDataProvider as $providerClassName => $_) {
/** @var FormDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance($providerClassName);
if (!$provider instanceof FormDataProviderInterface) {
throw new \UnexpectedValueException(
'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
1444398947
);
}
$result = $provider->addData($result);
}
return $result;
return $orderedProviderList->compile($result);
}
}
<?php
namespace TYPO3\CMS\Backend\Form\FormDataGroup;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
/**
* Calls provider with dependencies specified given by setter
*
* This group is used to call a list of providers in order by specified
* dependencies before/depends.
*/
class OrderedProviderList implements FormDataGroupInterface
{
/**
* @var array<FormDataProviderInterface>
*/
protected $providerList = [];
/**
* Compile form data
*
* @param array $result Initialized result array
* @return array Result filled with data
* @throws \UnexpectedValueException
*/
public function compile(array $result)
{
$orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
$orderedDataProvider = $orderingService->orderByDependencies($this->providerList, 'before', 'depends');
foreach ($orderedDataProvider as $providerClassName => $_) {
/** @var FormDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance($providerClassName);
if (!$provider instanceof FormDataProviderInterface) {
throw new \UnexpectedValueException(
'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
1485299408
);
}
$result = $provider->addData($result);
}
return $result;
}
/**
* Set list of providers to be called
*
* The dependencies of a provider are specified as:
*
* FormDataProvider::class => [
* 'before' => [AnotherFormDataProvider::class]
* 'depends' => [YetAnotherFormDataProvider::class]
* ]
*
* @param array $list Given list of Provider class names
* @see \TYPO3\CMS\Core\Service\DependencyOrderingService
*/
public function setProviderList(array $list)
{
$this->providerList = $list;
}
}
......@@ -15,8 +15,6 @@ namespace TYPO3\CMS\Backend\Form\FormDataGroup;
*/
use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -33,24 +31,11 @@ class TcaDatabaseRecord implements FormDataGroupInterface
*/
public function compile(array $result)
{
$dataProvider = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'];
$orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
$orderedDataProvider = $orderingService->orderByDependencies($dataProvider, 'before', 'depends');
$orderedProviderList = GeneralUtility::makeInstance(OrderedProviderList::class);
$orderedProviderList->setProviderList(
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord']
);
foreach ($orderedDataProvider as $providerClassName => $_) {
/** @var FormDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance($providerClassName);
if (!$provider instanceof FormDataProviderInterface) {
throw new \UnexpectedValueException(
'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
1437906440
);
}
$result = $provider->addData($result);
}
return $result;
return $orderedProviderList->compile($result);
}
}
......@@ -15,8 +15,6 @@ namespace TYPO3\CMS\Backend\Form\FormDataGroup;
*/
use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -33,24 +31,11 @@ class TcaInputPlaceholderRecord implements FormDataGroupInterface
*/
public function compile(array $result)
{
$dataProvider = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaInputPlaceholderRecord'];
$orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
$orderedDataProvider = $orderingService->orderByDependencies($dataProvider, 'before', 'depends');
$orderedProviderList = GeneralUtility::makeInstance(OrderedProviderList::class);
$orderedProviderList->setProviderList(
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaInputPlaceholderRecord']
);
foreach ($orderedDataProvider as $providerClassName => $_) {
/** @var FormDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance($providerClassName);
if (!$provider instanceof FormDataProviderInterface) {
throw new \UnexpectedValueException(
'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
1443986127
);
}
$result = $provider->addData($result);
}
return $result;
return $orderedProviderList->compile($result);
}
}
......@@ -93,7 +93,7 @@ class FlexFormSegmentTest extends \TYPO3\CMS\Components\TestingFramework\Core\Un
GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionCode(1437906441);
$this->expectExceptionCode(1485299408);
$this->subject->compile([]);
}
......
......@@ -93,7 +93,7 @@ class InlineParentRecordTest extends \TYPO3\CMS\Components\TestingFramework\Core
GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionCode(1444398947);
$this->expectExceptionCode(1485299408);
$this->subject->compile([]);
}
......
<?php
namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataGroup;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use TYPO3\CMS\Backend\Form\FormDataGroup\OrderedProviderList;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Test case
*/
class OrderedProviderListTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
{
/**
* @var OrderedProviderList
*/
protected $subject;
protected function setUp()
{
$this->subject = new OrderedProviderList();
}
/**
* @test
*/
public function compileReturnsIncomingData()
{
/** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
$orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
$orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
$input = ['foo'];
$this->subject->setProviderList([]);
$this->assertEquals($input, $this->subject->compile($input));
}
/**
* @test
*/
public function compileReturnsResultChangedByDataProvider()
{
/** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
$orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
$orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
/** @var FormDataProviderInterface|ObjectProphecy $formDataProviderProphecy */
$formDataProviderProphecy = $this->prophesize(FormDataProviderInterface::class);
GeneralUtility::addInstance(FormDataProviderInterface::class, $formDataProviderProphecy->reveal());
$providerResult = ['foo'];
$formDataProviderProphecy->addData(Argument::cetera())->shouldBeCalled()->willReturn($providerResult);
$this->subject->setProviderList([
FormDataProviderInterface::class => [],
]);
$this->assertEquals($providerResult, $this->subject->compile([]));
}
/**
* @test
*/
public function compileThrowsExceptionIfDataProviderDoesNotImplementInterface()
{
/** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
$orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
$orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
/** @var FormDataProviderInterface|ObjectProphecy $formDataProviderProphecy */
$formDataProviderProphecy = $this->prophesize(\stdClass::class);
GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionCode(1485299408);
$this->subject->setProviderList([
\stdClass::class => [],
]);
$this->subject->compile([]);
}
}
......@@ -93,7 +93,7 @@ class TcaDatabaseRecordTest extends \TYPO3\CMS\Components\TestingFramework\Core\
GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionCode(1437906440);
$this->expectExceptionCode(1485299408);
$this->subject->compile([]);
}
......
......@@ -93,7 +93,7 @@ class TcaInputPlaceholderRecordTest extends \TYPO3\CMS\Components\TestingFramewo
GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionCode(1443986127);
$this->expectExceptionCode(1485299408);
$this->subject->compile([]);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment