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

[Map] Introduce ux_map.google_maps.default_map_id configuration #2350

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/Map/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# CHANGELOG

## 2.22

- Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map.
smnandre marked this conversation as resolved.
Show resolved Hide resolved
- Add configuration `ux_map.google_maps.default_map_id` to automatically set the map id for all maps
Copy link
Member

@smnandre smnandre Nov 7, 2024

Choose a reason for hiding this comment

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

Suggested change
- Add configuration `ux_map.google_maps.default_map_id` to automatically set the map id for all maps
- Add `ux_map.google_maps.default_map_id` configuration to set the Google ``Map ID``

when using Google Maps Bridge.

## 2.20

Expand Down
2 changes: 0 additions & 2 deletions src/Map/config/twig_component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/Map/src/Bridge/Google/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.22

- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRendererFactory` constructor
- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRenderer` constructor
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

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

  • Add support for Google Map ID


## 2.20

### BC Breaks
Expand Down
5 changes: 5 additions & 0 deletions src/Map/src/Bridge/Google/src/GoogleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function mapId(?string $mapId): self
return $this;
}

public function hasMapId(): bool
{
return null !== $this->mapId;
}
smnandre marked this conversation as resolved.
Show resolved Hide resolved

public function gestureHandling(GestureHandling $gestureHandling): self
{
$this->gestureHandling = $gestureHandling;
Expand Down
16 changes: 15 additions & 1 deletion src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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)) {
Expand All @@ -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,
);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Map/src/Bridge/Google/tests/GoogleRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,23 @@ public function provideTestRenderMap(): iterable
fullscreenControl: false,
)),
];

yield 'with default map id' => [
'expected_renderer' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:&quot;DefaultMapId&quot;,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
'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' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:DefaultMapId,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
'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' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:&quot;CustomMapId&quot;,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'),
'map' => (clone $map)
->options(new GoogleOptions(mapId: 'CustomMapId')),
];
}
}
14 changes: 14 additions & 0 deletions src/Map/src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -39,6 +51,8 @@ final public function renderMap(Map $map, array $attributes = []): string
$map->options($defaultMapOptions);
}

$map->options($this->tapOptions($map->getOptions()));
smnandre marked this conversation as resolved.
Show resolved Hide resolved

$controllers = [];
if ($attributes['data-controller'] ?? null) {
$controllers[$attributes['data-controller']] = [];
Expand Down
16 changes: 15 additions & 1 deletion src/Map/src/UXMapBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
smnandre marked this conversation as resolved.
Show resolved Hide resolved
->end()
;
}
Expand Down Expand Up @@ -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)
Comment on lines +84 to +90
Copy link
Member

Choose a reason for hiding this comment

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

Not a big fan of variables in service names (just personal taste here)

->args([
'$defaultMapId' => $config['google_maps']['default_map_id'],
]);
}
}
}
}
Expand Down
Loading