Skip to content

Commit 2f5f495

Browse files
committed
Do not register the search routes when search is disabled
The search controller services already only exist when search.enabled (services/conditional/search.xml), but the shop search routes were always registered, pointing at non-existent controllers when search was off. Load the shop routes through a custom SearchRouteLoader (tagged routing.loader) that returns them only when search is enabled. The route YAMLs now import the shop routes via this loader (type setono_sylius_meilisearch_search) instead of importing shop.yaml directly; the admin synonym routes are unaffected. Closes #129
1 parent f1d7e0c commit 2f5f495

6 files changed

Lines changed: 98 additions & 2 deletions

File tree

src/Resources/config/routes.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# The shop routes are loaded through a custom loader so they are only registered when
2+
# search is enabled (search.enabled). See Routing\SearchRouteLoader.
13
setono_sylius_meilisearch_shop:
2-
resource: "@SetonoSyliusMeilisearchPlugin/Resources/config/routes/shop.yaml"
4+
resource: .
5+
type: setono_sylius_meilisearch_search
36
prefix: /{_locale}
47
requirements:
58
_locale: ^[A-Za-z]{2,4}(_([A-Za-z]{4}|[0-9]{3}))?(_([A-Za-z]{2}|[0-9]{3}))?$

src/Resources/config/routes_no_locale.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# localized URLs (see https://docs.sylius.com/en/latest/cookbook/shop/disabling-localised-urls.html).
33
# To accommodate this in your plugin, provide a routes file for non localized stores and tell this in the README.md
44

5+
# The shop routes are loaded through a custom loader so they are only registered when
6+
# search is enabled (search.enabled). See Routing\SearchRouteLoader.
57
setono_sylius_meilisearch_shop:
6-
resource: "@SetonoSyliusMeilisearchPlugin/Resources/config/routes/shop.yaml"
8+
resource: .
9+
type: setono_sylius_meilisearch_search
710

811
setono_sylius_meilisearch_admin:
912
resource: "@SetonoSyliusMeilisearchPlugin/Resources/config/routes/admin.yaml"

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<import resource="services/normalizer.xml"/>
2020
<import resource="services/provider.xml"/>
2121
<import resource="services/resolver.xml"/>
22+
<import resource="services/routing.xml"/>
2223
<import resource="services/twig.xml"/>
2324
<import resource="services/url_generator.xml"/>
2425
</imports>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
3+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<service id="Setono\SyliusMeilisearchPlugin\Routing\SearchRouteLoader">
6+
<argument>%setono_sylius_meilisearch.search.enabled%</argument>
7+
<argument>%kernel.environment%</argument>
8+
9+
<tag name="routing.loader"/>
10+
</service>
11+
</services>
12+
</container>

src/Routing/SearchRouteLoader.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusMeilisearchPlugin\Routing;
6+
7+
use Symfony\Component\Config\Loader\Loader;
8+
use Symfony\Component\Routing\RouteCollection;
9+
10+
/**
11+
* Loads the shop search routes only when search is enabled. When search is disabled the search
12+
* controller services are not registered either (see services/conditional/search.xml), so the
13+
* routes would otherwise point at non-existent controllers.
14+
*/
15+
final class SearchRouteLoader extends Loader
16+
{
17+
public function __construct(private readonly bool $enabled, ?string $env = null)
18+
{
19+
parent::__construct($env);
20+
}
21+
22+
public function load(mixed $resource, ?string $type = null): RouteCollection
23+
{
24+
$routes = new RouteCollection();
25+
26+
if ($this->enabled) {
27+
/** @var RouteCollection $imported */
28+
$imported = $this->import('@SetonoSyliusMeilisearchPlugin/Resources/config/routes/shop.yaml');
29+
$routes->addCollection($imported);
30+
}
31+
32+
return $routes;
33+
}
34+
35+
public function supports(mixed $resource, ?string $type = null): bool
36+
{
37+
return 'setono_sylius_meilisearch_search' === $type;
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusMeilisearchPlugin\Tests\Unit\Routing;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Setono\SyliusMeilisearchPlugin\Routing\SearchRouteLoader;
9+
10+
/**
11+
* @covers \Setono\SyliusMeilisearchPlugin\Routing\SearchRouteLoader
12+
*/
13+
final class SearchRouteLoaderTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function it_registers_no_routes_when_search_is_disabled(): void
19+
{
20+
$loader = new SearchRouteLoader(enabled: false);
21+
22+
$routes = $loader->load('.', 'setono_sylius_meilisearch_search');
23+
24+
self::assertCount(0, $routes);
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function it_supports_only_its_own_type(): void
31+
{
32+
$loader = new SearchRouteLoader(enabled: true);
33+
34+
self::assertTrue($loader->supports('.', 'setono_sylius_meilisearch_search'));
35+
self::assertFalse($loader->supports('.', 'yaml'));
36+
self::assertFalse($loader->supports('.', null));
37+
}
38+
}

0 commit comments

Comments
 (0)