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

RouteCollector: Detect Duplicates Config #72

Merged
merged 1 commit into from
Jul 18, 2024
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to squint a few times, because adding a constant for this felt a bit excessive, but then it makes sense :-)


/**
* @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'] ?? [];
Comment on lines -53 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to adjust any docs for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes #62 is tracking this - but I think all the docs are in mezzio - I'll write some decent release notes on the 4.0 milestone here so that we can refer to them when updating docs for the next mezzio major 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added mezzio/mezzio#183 to mezzio


$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