Skip to content

Commit

Permalink
Making the new flat config registry available in the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
baumannsven committed Apr 16, 2021
1 parent d57a4e3 commit 9af17e5
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Contao/Dca/Populator/HardCodedPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
29 changes: 28 additions & 1 deletion src/DefaultEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,7 @@
/**
* Default implementation of an environment.
*/
class DefaultEnvironment implements EnvironmentInterface
class DefaultEnvironment implements EnvironmentInterface, EnvironmentFlatConfigRegistryInterface
{
/**
* The controller.
Expand Down Expand Up @@ -91,6 +92,13 @@ class DefaultEnvironment implements EnvironmentInterface
*/
protected $baseConfigRegistry;

/**
* The attached flat config registry.
*
* @var BaseConfigRegistryInterface
*/
protected $flatConfigRegistry;

/**
* The registered data providers.
*
Expand Down Expand Up @@ -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}
*/
Expand Down
48 changes: 48 additions & 0 deletions src/EnvironmentFlatConfigRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2021 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Sven Baumann <[email protected]>
* @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;
}
3 changes: 2 additions & 1 deletion src/EnvironmentPopulator/EnvironmentPopulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace ContaoCommunityAlliance\DcGeneral\EnvironmentPopulator;

use ContaoCommunityAlliance\DcGeneral\EnvironmentFlatConfigRegistryInterface;
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;

/**
Expand All @@ -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
*/
Expand Down
103 changes: 103 additions & 0 deletions tests/Config/FlatConfigRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2021 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Sven Baumann <[email protected]>
* @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());
}
}

0 comments on commit 9af17e5

Please sign in to comment.