diff --git a/src/Contao/Dca/Populator/HardCodedPopulator.php b/src/Contao/Dca/Populator/HardCodedPopulator.php index fff23e39a..0846e586f 100644 --- a/src/Contao/Dca/Populator/HardCodedPopulator.php +++ b/src/Contao/Dca/Populator/HardCodedPopulator.php @@ -23,8 +23,10 @@ use ContaoCommunityAlliance\DcGeneral\BaseConfigRegistry; use ContaoCommunityAlliance\DcGeneral\Clipboard\Clipboard; +use ContaoCommunityAlliance\DcGeneral\Config\FlatConfigRegistry; use ContaoCommunityAlliance\DcGeneral\Contao\InputProvider; use ContaoCommunityAlliance\DcGeneral\Controller\DefaultController; +use ContaoCommunityAlliance\DcGeneral\EnvironmentFlatConfigRegistryInterface; use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface; use ContaoCommunityAlliance\DcGeneral\EnvironmentPopulator\AbstractEventDrivenEnvironmentPopulator; @@ -99,6 +101,17 @@ public function populate(EnvironmentInterface $environment) // @codingStandardsIgnoreEnd } + if (($environment instanceof EnvironmentFlatConfigRegistryInterface) + && (!$environment->getFlatConfigRegistry()) + ) { + $flatConfigRegistry = new FlatConfigRegistry(); + $flatConfigRegistry->setEnvironment($environment); + $environment->setFlatConfigRegistry($flatConfigRegistry); + // @codingStandardsIgnoreStart + @\trigger_error('Fallback populator in use - implement a proper populator!', E_USER_DEPRECATED); + // @codingStandardsIgnoreEnd + } + $this->populateController($environment); } } diff --git a/src/DefaultEnvironment.php b/src/DefaultEnvironment.php index d88ade2ed..9a13d2173 100644 --- a/src/DefaultEnvironment.php +++ b/src/DefaultEnvironment.php @@ -22,6 +22,7 @@ namespace ContaoCommunityAlliance\DcGeneral; use ContaoCommunityAlliance\DcGeneral\Clipboard\ClipboardInterface; +use ContaoCommunityAlliance\DcGeneral\Config\BaseConfigRegistryInterface; use ContaoCommunityAlliance\DcGeneral\Controller\ControllerInterface; use ContaoCommunityAlliance\DcGeneral\Data\DataProviderInterface; use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface; @@ -33,7 +34,7 @@ /** * Default implementation of an environment. */ -class DefaultEnvironment implements EnvironmentInterface +class DefaultEnvironment implements EnvironmentInterface, EnvironmentFlatConfigRegistryInterface { /** * The controller. @@ -91,6 +92,13 @@ class DefaultEnvironment implements EnvironmentInterface */ protected $baseConfigRegistry; + /** + * The attached flat config registry. + * + * @var BaseConfigRegistryInterface + */ + protected $flatConfigRegistry; + /** * The registered data providers. * @@ -263,6 +271,25 @@ public function getBaseConfigRegistry() return $this->baseConfigRegistry; } + /** + * {@inheritdoc} + */ + public function setFlatConfigRegistry( + BaseConfigRegistryInterface $flatConfigRegistry + ): EnvironmentFlatConfigRegistryInterface { + $this->flatConfigRegistry = $flatConfigRegistry; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getFlatConfigRegistry(): ?BaseConfigRegistryInterface + { + return $this->flatConfigRegistry; + } + /** * {@inheritdoc} */ diff --git a/src/EnvironmentFlatConfigRegistryInterface.php b/src/EnvironmentFlatConfigRegistryInterface.php new file mode 100644 index 000000000..61ee802f2 --- /dev/null +++ b/src/EnvironmentFlatConfigRegistryInterface.php @@ -0,0 +1,48 @@ + + * @copyright 2013-2021 Contao Community Alliance. + * @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later + * @filesource + */ + +declare(strict_types=1); + +namespace ContaoCommunityAlliance\DcGeneral; + +use ContaoCommunityAlliance\DcGeneral\Config\BaseConfigRegistryInterface; + +/** + * The environment interface for the flat config registry. + */ +interface EnvironmentFlatConfigRegistryInterface extends EnvironmentInterface +{ + /** + * Set the base config registry to use. + * + * @param BaseConfigRegistryInterface $baseConfigRegistry The input provider to use. + * + * @return EnvironmentFlatConfigRegistryInterface + */ + public function setFlatConfigRegistry( + BaseConfigRegistryInterface $baseConfigRegistry + ): EnvironmentFlatConfigRegistryInterface; + + /** + * Retrieve the base config registry. + * + * @return BaseConfigRegistryInterface + */ + public function getFlatConfigRegistry(): ?BaseConfigRegistryInterface; +} diff --git a/src/EnvironmentPopulator/EnvironmentPopulatorInterface.php b/src/EnvironmentPopulator/EnvironmentPopulatorInterface.php index 5fc90a26f..c5948eefd 100644 --- a/src/EnvironmentPopulator/EnvironmentPopulatorInterface.php +++ b/src/EnvironmentPopulator/EnvironmentPopulatorInterface.php @@ -20,6 +20,7 @@ namespace ContaoCommunityAlliance\DcGeneral\EnvironmentPopulator; +use ContaoCommunityAlliance\DcGeneral\EnvironmentFlatConfigRegistryInterface; use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface; /** @@ -33,7 +34,7 @@ interface EnvironmentPopulatorInterface /** * Create all needed objects the populator knows to create and put them into the environment. * - * @param EnvironmentInterface $environment The environment to populate. + * @param EnvironmentInterface|EnvironmentFlatConfigRegistryInterface $environment The environment to populate. * * @return void */ diff --git a/tests/Config/FlatConfigRegistryTest.php b/tests/Config/FlatConfigRegistryTest.php new file mode 100644 index 000000000..4b95627d3 --- /dev/null +++ b/tests/Config/FlatConfigRegistryTest.php @@ -0,0 +1,103 @@ + + * @copyright 2013-2021 Contao Community Alliance. + * @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later + * @filesource + */ + +namespace ContaoCommunityAlliance\DcGeneral\Test\Config; + +use ContaoCommunityAlliance\DcGeneral\Config\BaseConfigRegistryInterface; +use ContaoCommunityAlliance\DcGeneral\Config\FlatConfigRegistry; +use ContaoCommunityAlliance\DcGeneral\Contao\DataDefinition\Definition\Contao2BackendViewDefinitionInterface; +use ContaoCommunityAlliance\DcGeneral\Data\ConfigInterface; +use ContaoCommunityAlliance\DcGeneral\Data\DefaultConfig; +use ContaoCommunityAlliance\DcGeneral\Data\DefaultDataProvider; +use ContaoCommunityAlliance\DcGeneral\DataDefinition\DefaultContainer; +use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\DefaultBasicDefinition; +use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\ModelRelationshipDefinitionInterface; +use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\View\ListingConfigInterface; +use ContaoCommunityAlliance\DcGeneral\DefaultEnvironment; +use ContaoCommunityAlliance\DcGeneral\EnvironmentFlatConfigRegistryInterface; +use PHPUnit\Framework\TestCase; + +/** + * @covers \ContaoCommunityAlliance\DcGeneral\Config\FlatConfigRegistry + */ +class FlatConfigRegistryTest extends TestCase +{ + public function testSetterAndGetter(): void + { + $environment = $this->getMockForAbstractClass(EnvironmentFlatConfigRegistryInterface::class); + + $configRegistry = new FlatConfigRegistry(); + + self::assertNull($configRegistry->getEnvironment()); + self::assertInstanceOf(BaseConfigRegistryInterface::class, $configRegistry->setEnvironment($environment)); + self::assertInstanceOf(EnvironmentFlatConfigRegistryInterface::class, $configRegistry->getEnvironment()); + self::assertSame($environment, $configRegistry->getEnvironment()); + } + + public function testGetBaseConfig(): void + { + // Common test settings. + $basicDefinition = + $this->getMockBuilder(DefaultBasicDefinition::class)->enableProxyingToOriginalMethods()->getMock(); + $dataDefinition = + $this->getMockBuilder(DefaultContainer::class)->disableOriginalConstructor()->getMock(); + $environment = + $this->getMockBuilder(DefaultEnvironment::class)->setMethods(['getDataDefinition'])->getMock(); + $viewDefinition = $this->createMock(Contao2BackendViewDefinitionInterface::class); + $listingConfig = $this->getMockBuilder(ListingConfigInterface::class)->getMock(); + $modelRelationShip = $this->createMock(ModelRelationshipDefinitionInterface::class); + + $viewDefinition->method('getListingConfig')->willReturn($listingConfig); + + $definition = [ + Contao2BackendViewDefinitionInterface::NAME => $viewDefinition + ]; + $dataDefinition->method('hasDefinition')->willReturnCallback( + function ($definitionName) use ($definition) { + return array_key_exists($definitionName, $definition); + } + ); + $dataDefinition->method('getDefinition')->willReturnCallback( + function ($definitionName) use ($definition) { + return $definition[$definitionName]; + } + ); + $dataDefinition->method('getModelRelationshipDefinition')->willReturn($modelRelationShip); + + $environment->method('getDataDefinition')->willReturn($dataDefinition); + + $configRegistry = new FlatConfigRegistry(); + $configRegistry->setEnvironment($environment); + + // Single data provider test settings. + $dataDefinition->method('getBasicDefinition')->willReturn($basicDefinition); + $singleDataProvider = $this->createMock(DefaultDataProvider::class); + $singleDataProviderConfig = DefaultConfig::init(); + $singleDataProvider->method('getEmptyConfig')->willReturn($singleDataProviderConfig); + $singleAdditionalFilter = ['single' => 'foo']; + + // Single data provider tests. + $environment->addDataProvider('single', $singleDataProvider); + $basicDefinition->setDataProvider('single'); + $basicDefinition->setAdditionalFilter('single', ['single' => 'foo']); + self::assertInstanceOf(ConfigInterface::class, $configRegistry->getBaseConfig()); + self::assertIsArray($singleDataProviderConfig->getFilter()); + self::assertSame($singleAdditionalFilter, $singleDataProviderConfig->getFilter()); + } +}