Skip to content

Commit

Permalink
Add env support / improve settings load
Browse files Browse the repository at this point in the history
Closes #241
  • Loading branch information
Tam committed Feb 27, 2020
1 parent b342e77 commit 58c1c5e
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 93 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.8.1 - 2020-02-27
### Added
- Add env support to settings (Closes #241)

### Improved
- Improve settings page appearance during load

## 3.8.0 - 2020-02-25
### Added
- Add What3Words support (Closes #236)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/simplemap",
"description": "A beautifully simple Map field type for Craft CMS 3",
"version": "3.8.0",
"version": "3.8.1",
"type": "craft-plugin",
"license": "proprietary",
"minimum-stability": "dev",
Expand Down
5 changes: 4 additions & 1 deletion src/SimpleMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function createSettingsModel ()
}

/**
* @return Settings
* @return bool|\craft\base\Model|Settings
*/
public function getSettings ()
{
Expand Down Expand Up @@ -230,6 +230,9 @@ public function onRegisterGqlTypes (RegisterGqlTypesEvent $event)
$event->types[] = MapPartsType::class;
}

/**
* @throws \Exception
*/
public function onApplicationInit ()
{
if ($this->getSettings()->geoLocationAutoRedirect)
Expand Down
14 changes: 7 additions & 7 deletions src/fields/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,13 @@ private function _renderMap ($value, $isSettings = false)

'mapTiles' => $settings->mapTiles,
'mapToken' => GeoService::getToken(
$settings->mapToken,
$settings->getMapToken(),
$settings->mapTiles
),

'geoService' => $settings->geoService,
'geoToken' => GeoService::getToken(
$settings->geoToken,
$settings->getGeoToken(),
$settings->geoService
),

Expand Down Expand Up @@ -549,11 +549,11 @@ private function _renderMap ($value, $isSettings = false)

if (strpos($settings->mapTiles, 'google') !== false)
{
if ($settings->mapToken !== $settings->geoToken)
if ($settings->getMapToken() !== $settings->getGeoToken())
{
$view->registerJsFile(
'https://maps.googleapis.com/maps/api/js?key=' .
$settings->mapToken
$settings->getMapToken()
);
}
}
Expand All @@ -571,7 +571,7 @@ private function _renderMap ($value, $isSettings = false)
{
$view->registerJsFile(
'https://maps.googleapis.com/maps/api/js?libraries=places&key=' .
$settings->geoToken
$settings->getGeoToken()
);
}
elseif ($settings->geoService === GeoEnum::AppleMapKit)
Expand All @@ -584,11 +584,11 @@ private function _renderMap ($value, $isSettings = false)
// what3words
// ---------------------------------------------------------------------

if ($settings->w3wEnabled && !empty($settings->w3wToken))
if ($settings->w3wEnabled && !empty($settings->getW3WToken()))
{
$view->registerJsFile(
'https://assets.what3words.com/sdk/v3/what3words.js?key=' .
$settings->w3wToken
$settings->getW3WToken()
);
}

Expand Down
36 changes: 36 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,40 @@ public function isW3WEnabled ()
return $this->w3wEnabled && SimpleMap::v(SimpleMap::EDITION_PRO);
}

// Getters
// =========================================================================

public function getMapToken ()
{
return $this->_parseEnv($this->mapToken);
}

public function getGeoToken ()
{
return $this->_parseEnv($this->geoToken);
}

public function getW3WToken ()
{
return $this->_parseEnv($this->w3wToken);
}

public function getGeoLocationToken ()
{
return $this->_parseEnv($this->geoLocationToken);
}

// Helpers
// =========================================================================

private function _parseEnv ($value)
{
if (is_string($value))
return Craft::parseEnv($value);

return array_map(function ($v) {
return Craft::parseEnv($v);
}, $value);
}

}
18 changes: 9 additions & 9 deletions src/services/EmbedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function _embedGoogle (EmbedOptions $options, Settings $settings)
);

$params = http_build_query([
'key' => $settings->mapToken,
'key' => $settings->getMapToken(),
'callback' => $callbackName,
]);

Expand All @@ -159,7 +159,7 @@ private function _embedGoogle (EmbedOptions $options, Settings $settings)
function {$callbackName} () {
{$options->id} = new google.maps.Map(document.getElementById('{$options->id}'), $formattedOptions);
{$options->id}._markers = [];
{$formattedMarkers}.forEach(function (marker) {
marker.map = {$options->id};
Expand Down Expand Up @@ -189,7 +189,7 @@ private function _embedApple (EmbedOptions $options, Settings $settings)
$view = Craft::$app->getView();

$token = GeoService::getToken(
$settings->mapToken,
$settings->getMapToken(),
$settings->mapTiles
);
$latLng = implode(', ', array_values($options->getCenter()));
Expand Down Expand Up @@ -258,7 +258,7 @@ private function _embedApple (EmbedOptions $options, Settings $settings)
{$formattedMarkers}.forEach(function (marker) {
marker.position.unshift(null);
const m = new mapkit.MarkerAnnotation(
new (mapkit.Coordinate.bind.apply(mapkit.Coordinate, marker.position)),
new (mapkit.Coordinate.bind.apply(mapkit.Coordinate, marker.position)),
{
glyphText: marker.label || '',
color: marker.color || '',
Expand Down Expand Up @@ -335,7 +335,7 @@ private function _embedMapbox (EmbedOptions $options, Settings $settings)
);

$initJs = <<<JS
mapboxgl.accessToken = '{$settings->mapToken}';
mapboxgl.accessToken = '{$settings->getMapToken()}';
JS;


Expand Down Expand Up @@ -371,7 +371,7 @@ private function _embedMapbox (EmbedOptions $options, Settings $settings)
*/
private function _embedHere (EmbedOptions $options, Settings $settings)
{
if (!array_key_exists('apiKey', $settings->mapToken) || !$settings->mapToken['apiKey'])
if (!array_key_exists('apiKey', $settings->getMapToken()) || !$settings->getMapToken()['apiKey'])
throw new InvalidConfigException('Missing HERE API Key');

$view = Craft::$app->getView();
Expand Down Expand Up @@ -433,7 +433,7 @@ private function _embedHere (EmbedOptions $options, Settings $settings)
);

$initJs = <<<JS
const HERE_platform = new H.service.Platform({ apikey: '{$settings->mapToken['apiKey']}' });
const HERE_platform = new H.service.Platform({ apikey: '{$settings->getMapToken()['apiKey']}' });
window.HERE_defaultLayers = HERE_platform.createDefaultLayers();
JS;

Expand All @@ -450,7 +450,7 @@ private function _embedHere (EmbedOptions $options, Settings $settings)
{$formattedMarkers}.forEach(function (marker) {
const m = new H.map.Marker(
marker.position,
marker.position,
{
icon: new H.map.Icon('{$markerIcon}'.replace('##FILL##', marker.color).replace('##LABEL##', marker.label)),
}
Expand Down Expand Up @@ -549,7 +549,7 @@ className: '',
{$options->id}._markers = [];
{$formattedMarkers}.forEach(function (marker) {
const m = L.marker(
marker.position,
marker.position,
{ icon: window.LMapMarkerIcon(marker) }
);
{$options->id}._markers.push(m);
Expand Down
4 changes: 2 additions & 2 deletions src/services/GeoLocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public function lookup ($ip = null)
switch ($settings->geoLocationService)
{
case self::IpStack:
$userLocation = $this->_lookup_IpStack($settings->geoLocationToken, $ip);
$userLocation = $this->_lookup_IpStack($settings->getGeoLocationToken(), $ip);
break;
case self::MaxMind:
$userLocation = $this->_lookup_MaxMind($settings->geoLocationToken, $ip);
$userLocation = $this->_lookup_MaxMind($settings->getGeoLocationToken(), $ip);
break;
case self::MaxMindLite:
$userLocation = $this->_lookup_MaxMindLite($ip);
Expand Down
4 changes: 2 additions & 2 deletions src/services/GeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public static function latLngFromAddress ($address, $country = null)
{
/** @var Settings $settings */
$settings = SimpleMap::getInstance()->getSettings();
$token = static::getToken($settings->geoToken, $settings->geoService);
$token = static::getToken($settings->getGeoToken(), $settings->geoService);

switch ($settings->geoService)
{
Expand Down Expand Up @@ -626,7 +626,7 @@ public static function addressFromLatLng ($lat, $lng)
/** @var Settings $settings */
$settings = SimpleMap::getInstance()->getSettings();
$token =
static::getToken($settings->geoToken, $settings->geoService);
static::getToken($settings->getGeoToken(), $settings->geoService);

switch ($settings->geoService)
{
Expand Down
14 changes: 7 additions & 7 deletions src/services/StaticService.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function _generateGoogle ($options, $settings)
'language' => Craft::$app->getLocale()->getLanguageID(),
'region' => $this->_getTld(),
'key' => GeoService::getToken(
$settings->mapToken,
$settings->getMapToken(),
$settings->mapTiles
),
];
Expand Down Expand Up @@ -152,8 +152,8 @@ private function _generateApple ($options, $settings)
'size' => $options->getSize(),
'scale' => $options->scale,
'lang' => Craft::$app->getLocale()->getLanguageID(),
'teamId' => $settings->mapToken['teamId'],
'keyId' => $settings->mapToken['keyId'],
'teamId' => $settings->getMapToken()['teamId'],
'keyId' => $settings->getMapToken()['keyId'],
];

if (!empty($options->markers))
Expand Down Expand Up @@ -189,7 +189,7 @@ private function _generateApple ($options, $settings)
}

$path = '/api/v1/snapshot?' . http_build_query($params);
openssl_sign($path, $signature, $settings->mapToken['privateKey'], OPENSSL_ALGO_SHA256);
openssl_sign($path, $signature, $settings->getMapToken()['privateKey'], OPENSSL_ALGO_SHA256);
$signature = $this->_encode($signature);

return 'https://snapshot.apple-mapkit.com' . $path . '&signature=' . $signature;
Expand Down Expand Up @@ -251,7 +251,7 @@ private function _generateMapbox ($options, $settings)
if ($options->scale > 1)
$url .= '@2x';

return $url . '?access_token=' . $settings->mapToken;
return $url . '?access_token=' . $settings->getMapToken();
}

/**
Expand All @@ -264,8 +264,8 @@ private function _generateMapbox ($options, $settings)
private function _generateHere ($options, $settings)
{
$params = [
'app_id' => $settings->mapToken['appId'],
'app_code' => $settings->mapToken['appCode'],
'app_id' => $settings->getMapToken()['appId'],
'app_code' => $settings->getMapToken()['appCode'],
'nodot' => true,
'c' => implode(',', $options->getCenter()),
'z' => $options->zoom,
Expand Down
2 changes: 1 addition & 1 deletion src/services/What3WordsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static function _geocoder ()
if ($geocoder)
return $geocoder;

return $geocoder = new Geocoder(SimpleMap::getInstance()->getSettings()->w3wToken);
return $geocoder = new Geocoder(SimpleMap::getInstance()->getSettings()->getW3WToken());
}

}
Loading

0 comments on commit 58c1c5e

Please sign in to comment.