From cbacecc8a2c978993bf264f548bd9657ca7128ce Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Thu, 7 Nov 2024 20:06:43 +0100 Subject: [PATCH] [Map] Introduce `ux_map.google_maps.default_map_id` configuration --- src/Map/CHANGELOG.md | 5 +++++ src/Map/config/twig_component.php | 2 -- src/Map/src/Bridge/Google/CHANGELOG.md | 6 ++++++ .../src/Bridge/Google/src/GoogleOptions.php | 5 +++++ .../Google/src/Renderer/GoogleRenderer.php | 16 +++++++++++++++- .../src/Renderer/GoogleRendererFactory.php | 9 +++++++++ .../Bridge/Google/tests/GoogleRendererTest.php | 18 ++++++++++++++++++ src/Map/src/Renderer/AbstractRenderer.php | 14 ++++++++++++++ src/Map/src/UXMapBundle.php | 16 +++++++++++++++- 9 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/Map/CHANGELOG.md b/src/Map/CHANGELOG.md index 3883fe057d4..ef718fcb7ca 100644 --- a/src/Map/CHANGELOG.md +++ b/src/Map/CHANGELOG.md @@ -1,4 +1,9 @@ # CHANGELOG + +## 2.22 + +- Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map. +- Add `ux_map.google_maps.default_map_id` configuration to set the Google ``Map ID`` ## 2.20 diff --git a/src/Map/config/twig_component.php b/src/Map/config/twig_component.php index 096eec4aea8..0a3ad404ad5 100644 --- a/src/Map/config/twig_component.php +++ b/src/Map/config/twig_component.php @@ -12,8 +12,6 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Symfony\UX\Map\Twig\UXMapComponent; -use Symfony\UX\Map\Twig\UXMapComponentListener; -use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent; return static function (ContainerConfigurator $container): void { $container->services() diff --git a/src/Map/src/Bridge/Google/CHANGELOG.md b/src/Map/src/Bridge/Google/CHANGELOG.md index 46f7d49c2f0..48478b9806b 100644 --- a/src/Map/src/Bridge/Google/CHANGELOG.md +++ b/src/Map/src/Bridge/Google/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## 2.22 + +- Add support for configuring a default Map ID +- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRendererFactory` constructor +- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRenderer` constructor + ## 2.20 ### BC Breaks diff --git a/src/Map/src/Bridge/Google/src/GoogleOptions.php b/src/Map/src/Bridge/Google/src/GoogleOptions.php index 8b26efcfba9..a241daa80e6 100644 --- a/src/Map/src/Bridge/Google/src/GoogleOptions.php +++ b/src/Map/src/Bridge/Google/src/GoogleOptions.php @@ -46,6 +46,11 @@ public function mapId(?string $mapId): self return $this; } + public function hasMapId(): bool + { + return null !== $this->mapId; + } + public function gestureHandling(GestureHandling $gestureHandling): self { $this->gestureHandling = $gestureHandling; diff --git a/src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php b/src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php index 808278abbf3..cb325ba0171 100644 --- a/src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php +++ b/src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php @@ -41,6 +41,7 @@ public function __construct( * @var array<'core'|'maps'|'places'|'geocoding'|'routes'|'marker'|'geometry'|'elevation'|'streetView'|'journeySharing'|'drawing'|'visualization'> */ private array $libraries = [], + private ?string $defaultMapId = null, ) { parent::__construct($stimulusHelper); } @@ -66,7 +67,20 @@ protected function getProviderOptions(): array protected function getDefaultMapOptions(): MapOptionsInterface { - return new GoogleOptions(); + return new GoogleOptions(mapId: $this->defaultMapId); + } + + protected function tapOptions(MapOptionsInterface $options): MapOptionsInterface + { + if (!$options instanceof GoogleOptions) { + throw new \InvalidArgumentException(\sprintf('The options must be an instance of "%s", got "%s" instead.', GoogleOptions::class, get_debug_type($options))); + } + + if (!$options->hasMapId()) { + $options->mapId($this->defaultMapId); + } + + return $options; } public function __toString(): string diff --git a/src/Map/src/Bridge/Google/src/Renderer/GoogleRendererFactory.php b/src/Map/src/Bridge/Google/src/Renderer/GoogleRendererFactory.php index a391f676152..e34956349ad 100644 --- a/src/Map/src/Bridge/Google/src/Renderer/GoogleRendererFactory.php +++ b/src/Map/src/Bridge/Google/src/Renderer/GoogleRendererFactory.php @@ -17,12 +17,20 @@ use Symfony\UX\Map\Renderer\Dsn; use Symfony\UX\Map\Renderer\RendererFactoryInterface; use Symfony\UX\Map\Renderer\RendererInterface; +use Symfony\UX\StimulusBundle\Helper\StimulusHelper; /** * @author Hugo Alliaume */ final class GoogleRendererFactory extends AbstractRendererFactory implements RendererFactoryInterface { + public function __construct( + StimulusHelper $stimulus, + private ?string $defaultMapId = null, + ) { + parent::__construct($stimulus); + } + public function create(Dsn $dsn): RendererInterface { if (!$this->supports($dsn)) { @@ -42,6 +50,7 @@ public function create(Dsn $dsn): RendererInterface url: $dsn->getOption('url'), version: $dsn->getOption('version', 'weekly'), libraries: ['maps', 'marker', ...$dsn->getOption('libraries', [])], + defaultMapId: $this->defaultMapId, ); } diff --git a/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php b/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php index e688d0c89f3..358eb2fa353 100644 --- a/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php +++ b/src/Map/src/Bridge/Google/tests/GoogleRendererTest.php @@ -78,5 +78,23 @@ public function provideTestRenderMap(): iterable fullscreenControl: false, )), ]; + + yield 'with default map id' => [ + 'expected_renderer' => '
', + 'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'), + 'map' => (clone $map), + ]; + yield 'with default map id, when passing options (except the "mapId")' => [ + 'expected_renderer' => '
', + 'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'), + 'map' => (clone $map) + ->options(new GoogleOptions()), + ]; + yield 'with default map id overridden by option "mapId"' => [ + 'expected_renderer' => '
', + 'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'), + 'map' => (clone $map) + ->options(new GoogleOptions(mapId: 'CustomMapId')), + ]; } } diff --git a/src/Map/src/Renderer/AbstractRenderer.php b/src/Map/src/Renderer/AbstractRenderer.php index aa229009fd9..4471f9be36a 100644 --- a/src/Map/src/Renderer/AbstractRenderer.php +++ b/src/Map/src/Renderer/AbstractRenderer.php @@ -31,6 +31,18 @@ abstract protected function getProviderOptions(): array; abstract protected function getDefaultMapOptions(): MapOptionsInterface; + /** + * @template T of MapOptionsInterface + * + * @param T $options + * + * @return T + */ + protected function tapOptions(MapOptionsInterface $options): MapOptionsInterface + { + return $options; + } + final public function renderMap(Map $map, array $attributes = []): string { if (!$map->hasOptions()) { @@ -39,6 +51,8 @@ final public function renderMap(Map $map, array $attributes = []): string $map->options($defaultMapOptions); } + $map->options($this->tapOptions($map->getOptions())); + $controllers = []; if ($attributes['data-controller'] ?? null) { $controllers[$attributes['data-controller']] = []; diff --git a/src/Map/src/UXMapBundle.php b/src/Map/src/UXMapBundle.php index 1392926131f..2f4bb35a837 100644 --- a/src/Map/src/UXMapBundle.php +++ b/src/Map/src/UXMapBundle.php @@ -44,6 +44,12 @@ public function configure(DefinitionConfigurator $definition): void $rootNode ->children() ->scalarNode('renderer')->defaultNull()->end() + ->arrayNode('google_maps') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('default_map_id')->defaultNull()->end() + ->end() + ->end() ->end() ; } @@ -75,9 +81,17 @@ public function loadExtension(array $config, ContainerConfigurator $container, C foreach (self::$bridges as $name => $bridge) { if (ContainerBuilder::willBeAvailable('symfony/ux-'.$name.'-map', $bridge['renderer_factory'], ['symfony/ux-map'])) { $container->services() - ->set('ux_map.renderer_factory.'.$name, $bridge['renderer_factory']) + ->set($rendererFactoryName = 'ux_map.renderer_factory.'.$name, $bridge['renderer_factory']) ->parent('ux_map.renderer_factory.abstract') ->tag('ux_map.renderer_factory'); + + if ('google' === $name) { + $container->services() + ->get($rendererFactoryName) + ->args([ + '$defaultMapId' => $config['google_maps']['default_map_id'], + ]); + } } } }