forked from Snck3rs/contao-leaflet-maps
Make definition mapper independent of the data format.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber'
|
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber',
|
||||||
|
'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber',
|
||||||
);
|
);
|
||||||
|
|||||||
82
src/Netzmacht/Contao/Leaflet/Event/GetHashEvent.php
Normal file
82
src/Netzmacht/Contao/Leaflet/Event/GetHashEvent.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package dev
|
||||||
|
* @author David Molineus <david.molineus@netzmacht.de>
|
||||||
|
* @copyright 2015 netzmacht creative David Molineus
|
||||||
|
* @license LGPL 3.0
|
||||||
|
* @filesource
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Netzmacht\Contao\Leaflet\Event;
|
||||||
|
|
||||||
|
use Symfony\Component\EventDispatcher\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class GetHashEvent is emitted then a hash for a data object with an unknown type is required.
|
||||||
|
*
|
||||||
|
* @package Netzmacht\Contao\Leaflet\Event
|
||||||
|
*/
|
||||||
|
class GetHashEvent extends Event
|
||||||
|
{
|
||||||
|
const NAME = 'leaflet.get-hash';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The hash.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $hash;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct.
|
||||||
|
*
|
||||||
|
* @param mixed $data The data.
|
||||||
|
*/
|
||||||
|
public function __construct($data)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the data.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getData()
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the hash.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHash()
|
||||||
|
{
|
||||||
|
return $this->hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the generated hash.
|
||||||
|
*
|
||||||
|
* @param string $hash The generated hash.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setHash($hash)
|
||||||
|
{
|
||||||
|
$this->hash = (string) $hash;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||||
|
|
||||||
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||||
|
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
||||||
use Netzmacht\LeafletPHP\Definition;
|
use Netzmacht\LeafletPHP\Definition;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||||
@@ -74,15 +75,15 @@ class DefinitionMapper
|
|||||||
/**
|
/**
|
||||||
* Build a model.
|
* Build a model.
|
||||||
*
|
*
|
||||||
* @param \Model $model The definition model.
|
* @param mixed $model The definition model.
|
||||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||||
*
|
*
|
||||||
* @return Definition
|
* @return Definition
|
||||||
*/
|
*/
|
||||||
public function handle(\Model $model, LatLngBounds $bounds = null, $elementId = null)
|
public function handle($model, LatLngBounds $bounds = null, $elementId = null)
|
||||||
{
|
{
|
||||||
$hash = $model->getTable() . '.' . $model->{$model->getPk()} . ($elementId ? ('.' . $elementId) : '');
|
$hash = $this->getHash($model, $elementId);
|
||||||
|
|
||||||
if (isset($this->mapped[$hash])) {
|
if (isset($this->mapped[$hash])) {
|
||||||
return $this->mapped[$hash];
|
return $this->mapped[$hash];
|
||||||
@@ -117,12 +118,12 @@ class DefinitionMapper
|
|||||||
/**
|
/**
|
||||||
* Build a model.
|
* Build a model.
|
||||||
*
|
*
|
||||||
* @param \Model $model The definition model.
|
* @param mixed $model The definition model.
|
||||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||||
*
|
*
|
||||||
* @return FeatureCollection|Feature
|
* @return FeatureCollection|Feature
|
||||||
*/
|
*/
|
||||||
public function handleGeoJson(\Model $model, LatLngBounds $bounds = null)
|
public function handleGeoJson($model, LatLngBounds $bounds = null)
|
||||||
{
|
{
|
||||||
foreach ($this->builders as $builders) {
|
foreach ($this->builders as $builders) {
|
||||||
foreach ($builders as $builder) {
|
foreach ($builders as $builder) {
|
||||||
@@ -152,4 +153,26 @@ class DefinitionMapper
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $model
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getHash($model, $elementId)
|
||||||
|
{
|
||||||
|
$event = new GetHashEvent($model);
|
||||||
|
$this->eventDispatcher->dispatch($event::NAME, $event);
|
||||||
|
$hash = $event->getHash();
|
||||||
|
|
||||||
|
if (!$hash) {
|
||||||
|
throw new \RuntimeException('Could not create a hash');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($elementId) {
|
||||||
|
$hash .= '.' . $elementId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hash;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
66
src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php
Normal file
66
src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package dev
|
||||||
|
* @author David Molineus <david.molineus@netzmacht.de>
|
||||||
|
* @copyright 2015 netzmacht creative David Molineus
|
||||||
|
* @license LGPL 3.0
|
||||||
|
* @filesource
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Netzmacht\Contao\Leaflet\Subscriber;
|
||||||
|
|
||||||
|
|
||||||
|
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
|
class HashSubscriber implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
GetHashEvent::NAME => array(
|
||||||
|
array('getModelHash'),
|
||||||
|
array('getFallback', -100)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get hash for a model object.
|
||||||
|
*
|
||||||
|
* @param GetHashEvent $event
|
||||||
|
*/
|
||||||
|
public function getModelHash(GetHashEvent $event)
|
||||||
|
{
|
||||||
|
$data = $event->getData();
|
||||||
|
|
||||||
|
if ($data instanceof \Model) {
|
||||||
|
$event->setHash($data->getTable() . '.' . $data->{$data->getPk()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get hash fallback if no hash was created so far.
|
||||||
|
*
|
||||||
|
* @param GetHashEvent $event
|
||||||
|
*/
|
||||||
|
public function getFallback(GetHashEvent $event)
|
||||||
|
{
|
||||||
|
if ($event->getHash()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $event->getData();
|
||||||
|
|
||||||
|
if (is_object($data)) {
|
||||||
|
$event->setHash(spl_object_hash($data));
|
||||||
|
} else {
|
||||||
|
$event->setHash(md5(json_encode($data)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user