Skip to content

Commit

Permalink
RouteCollector: Detect Duplicates Config
Browse files Browse the repository at this point in the history
Renames configuration option for detect duplicates from `[RouteCollector::class]['detect_duplicates']` to `['router']['detect_duplicates']`

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jul 18, 2024
1 parent ed003a2 commit 0e6b4d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/RouteCollectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Container\ContainerInterface;

use function is_array;
use function is_bool;
use function sprintf;

/**
Expand All @@ -21,6 +22,8 @@
*/
final class RouteCollectorFactory
{
private const DETECT_DUPLICATES_BY_DEFAULT = true;

/**
* @throws Exception\MissingDependencyException If the RouterInterface service is missing.
*/
Expand All @@ -42,24 +45,26 @@ public function __invoke(ContainerInterface $container): RouteCollector
private function getDetectDuplicatesFlag(ContainerInterface $container): bool
{
if (! $container->has('config')) {
return true;
return self::DETECT_DUPLICATES_BY_DEFAULT;
}

$config = $container->get('config');
if (! is_array($config) && ! $config instanceof ArrayAccess) {
throw new LogicException(sprintf('Config must be an array or implement %s.', ArrayAccess::class));
}

if (! isset($config[RouteCollector::class])) {
return true;
}
$options = $config['router'] ?? [];

$collectorOptions = $config[RouteCollector::class] ?? [];
if (! is_array($options)) {
return self::DETECT_DUPLICATES_BY_DEFAULT;
}

if (! is_array($collectorOptions) || ! isset($collectorOptions['detect_duplicates'])) {
return true;
if (! isset($options['detect_duplicates'])) {
return self::DETECT_DUPLICATES_BY_DEFAULT;
}

return (bool) $collectorOptions['detect_duplicates'];
return is_bool($options['detect_duplicates'])
? $options['detect_duplicates']
: self::DETECT_DUPLICATES_BY_DEFAULT;
}
}
6 changes: 3 additions & 3 deletions test/RouteCollectorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testFactoryProducesRouteCollectorWhenAllDependenciesPresent(): v
public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromArrayConfig(): void
{
$this->testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromConfig([
RouteCollector::class => [
'router' => [
'detect_duplicates' => false,
],
]);
Expand All @@ -59,7 +59,7 @@ public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromAr
public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromArrayObjectConfig(): void
{
$this->testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromConfig(new ArrayObject([
RouteCollector::class => [
'router' => [
'detect_duplicates' => false,
],
]));
Expand All @@ -68,7 +68,7 @@ public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromAr
public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromArrayIteratorConfig(): void
{
$this->testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromConfig(new ArrayIterator([
RouteCollector::class => [
'router' => [
'detect_duplicates' => false,
],
]));
Expand Down

0 comments on commit 0e6b4d8

Please sign in to comment.