From 9a37da12e671f8a8518be9712750ee58261afcd6 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Mon, 12 Jan 2015 09:55:59 +0100 Subject: [PATCH] Make definition mapper independent of the data format. --- module/config/event_subscribers.php | 3 +- .../Contao/Leaflet/Event/GetHashEvent.php | 82 +++++++++++++++++++ .../Leaflet/Mapper/DefinitionMapper.php | 33 ++++++-- .../Leaflet/Subscriber/HashSubscriber.php | 66 +++++++++++++++ 4 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 src/Netzmacht/Contao/Leaflet/Event/GetHashEvent.php create mode 100644 src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php diff --git a/module/config/event_subscribers.php b/module/config/event_subscribers.php index 89e77f4..ad5cde2 100644 --- a/module/config/event_subscribers.php +++ b/module/config/event_subscribers.php @@ -1,5 +1,6 @@ + * @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; + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php index c822f60..4d5e760 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php @@ -12,6 +12,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper; use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent; +use Netzmacht\Contao\Leaflet\Event\GetHashEvent; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\GeoJson\Feature; use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; @@ -74,15 +75,15 @@ class DefinitionMapper /** * 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 string $elementId Optional element id. If none given the mapId or alias is used. * * @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])) { return $this->mapped[$hash]; @@ -117,12 +118,12 @@ class DefinitionMapper /** * 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. * * @return FeatureCollection|Feature */ - public function handleGeoJson(\Model $model, LatLngBounds $bounds = null) + public function handleGeoJson($model, LatLngBounds $bounds = null) { foreach ($this->builders as $builders) { 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; + } } diff --git a/src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php b/src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php new file mode 100644 index 0000000..c8c0436 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Subscriber/HashSubscriber.php @@ -0,0 +1,66 @@ + + * @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))); + } + } +}