File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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.
13setono_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}))?$
Original file line number Diff line number Diff line change 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.
57setono_sylius_meilisearch_shop :
6- resource : " @SetonoSyliusMeilisearchPlugin/Resources/config/routes/shop.yaml"
8+ resource : .
9+ type : setono_sylius_meilisearch_search
710
811setono_sylius_meilisearch_admin :
912 resource : " @SetonoSyliusMeilisearchPlugin/Resources/config/routes/admin.yaml"
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments