Ongoing development.

This commit is contained in:
David Molineus
2015-01-06 14:55:53 +01:00
parent dfb558b655
commit e9d1ec7081
32 changed files with 1050 additions and 95 deletions

View File

@@ -12,6 +12,7 @@
namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
/**
* Class AbstractBuilder.
@@ -148,13 +149,13 @@ abstract class AbstractMapper implements Mapper
/**
* {@inheritdoc}
*/
public function handle(\Model $model, DefinitionMapper $builder)
public function handle(\Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
$definition = $this->createInstance($model, $builder);
$definition = $this->createInstance($model, $builder, $bounds);
$this->buildOptions($definition, $model);
$this->buildConditionals($definition, $model);
$this->doBuild($definition, $model, $builder);
$this->doBuild($definition, $model, $builder, $bounds);
return $definition;
}
@@ -182,14 +183,19 @@ abstract class AbstractMapper implements Mapper
/**
* Use for specific build methods.
*
* @param Definition $definition The definition being built.
* @param \Model $model The model.
* @param DefinitionMapper $builder The definition builder.
* @param Definition $definition The definition being built.
* @param \Model $model The model.
* @param DefinitionMapper $builder The definition builder.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
*
* @return void
*/
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder)
{
protected function doBuild(
Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
}
/**
@@ -197,13 +203,14 @@ abstract class AbstractMapper implements Mapper
*
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
*
* @return Definition
*/
protected function createInstance(\Model $model, DefinitionMapper $mapper)
protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$reflector = new \ReflectionClass(static::$definitionClass);
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds));
return $instance;
}
@@ -213,10 +220,11 @@ abstract class AbstractMapper implements Mapper
*
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
*
* @return array
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
return array(
$model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id)

View File

@@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Control;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Control\Attribution;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
/**
* AttributionControlMapper maps the the attribution database definition to the definition class.
@@ -49,7 +50,7 @@ class AttributionControlMapper extends AbstractControlMapper
/**
* {@inheritdoc}
*/
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder)
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
if (!$definition instanceof Attribution) {
return;

View File

@@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Control;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
class LayersControlMapper extends AbstractControlMapper
{
@@ -31,9 +32,9 @@ class LayersControlMapper extends AbstractControlMapper
*/
protected static $type = 'layers';
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper);
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
$arguments[1] = array();
$arguments[2] = array();
@@ -44,7 +45,7 @@ class LayersControlMapper extends AbstractControlMapper
foreach ($collection as $layer) {
$argument = ($definition[$layer->id] === 'overlay') ? 2 : 1;
$arguments[$argument][] = $mapper->handle($layer);
$arguments[$argument][] = $mapper->handle($layer, $bounds);
}
}

View File

@@ -13,6 +13,9 @@ namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
/**
@@ -70,6 +73,8 @@ class DefinitionMapper
{
$this->builders[$priority][] = $builder;
ksort($this->builders);
return $this;
}
@@ -86,12 +91,13 @@ class DefinitionMapper
/**
* Build a model.
*
* @param \Model $model The definition model.
* @param string $elementId Optional element id. If none given the mapId or alias is used.
* @param \Model $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, $elementId = null)
public function handle(\Model $model, LatLngBounds $bounds = null, $elementId = null)
{
$hash = $model->getTable() . '.' . $model->{$model->getPk()};
@@ -99,16 +105,14 @@ class DefinitionMapper
return $this->mapped[$hash];
}
krsort($this->builders);
$this->mapId = $elementId ?: ($model->alias ?: ('map_' . $model->id));
foreach ($this->builders as $builders) {
foreach($builders as $builder) {
if ($builder->match($model)) {
$definition = $builder->handle($model, $this);
$definition = $builder->handle($model, $this, $bounds);
$event = new BuildDefinitionEvent($definition, $model);
$event = new BuildDefinitionEvent($definition, $model, $bounds);
$this->eventDispatcher->dispatch($event::NAME, $event);
$this->mapped[$hash] = $definition;
@@ -126,4 +130,43 @@ class DefinitionMapper
)
);
}
/**
* Build a model.
*
* @param \Model $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)
{
foreach ($this->builders as $builders) {
foreach ($builders as $builder) {
if (!$builder->match($model)) {
continue;
}
if ($builder instanceof GeoJsonMapper) {
return $builder->handleGeoJson($model, $this, $bounds);
}
throw new \RuntimeException(
sprintf(
'Builder for model "%s::%s" is not a GeoJsonMapper',
$model->getTable(),
$model->{$model->getPk()}
)
);
}
}
throw new \RuntimeException(
sprintf(
'Could not build geo json of model "%s::%s". No matching builders found.',
$model->getTable(),
$model->{$model->getPk()}
)
);
}
}

View File

@@ -0,0 +1,26 @@
<?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\Mapper;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
interface GeoJsonMapper
{
/**
* @param \Model $model
* @param DefinitionMapper $mapper
* @param LatLngBounds $bounds
*
* @return mixed
*/
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null);
}

View File

@@ -0,0 +1,24 @@
<?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\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
class AbstractLayerMapper extends AbstractTypeMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\LayerModel';
}

View File

@@ -0,0 +1,90 @@
<?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\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper;
use Netzmacht\Contao\Leaflet\Model\MarkerModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\Group\LayerGroup;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Definition\UI\Marker;
class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
{
/**
* Class of the definition being created.
*
* @var string
*/
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Group\GeoJson';
/**
* Layer type.
*
* @var string
*/
protected static $type = 'markers';
protected function doBuild(
Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
if ($definition instanceof LayerGroup) {
$collection = MarkerModel::findBy(
array('active=1', 'pid=?'),
array($model->id)
);
if ($collection) {
foreach ($collection as $item) {
$marker = new Marker('marker_' . $item->id, $item->coordinates);
$marker->setTitle($item->tooltip);
$definition->addLayer($marker);
}
}
}
}
/**
* @param \Model $model
* @param DefinitionMapper $mapper
* @param LatLngBounds $bounds
*
* @return mixed
*/
public function handleGeoJson(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$feature = new FeatureCollection();
$collection = MarkerModel::findBy(
array('active=1', 'pid=?'),
array($model->id)
);
if ($collection) {
foreach ($collection as $item) {
$marker = new Marker('marker_' . $item->id, $item->coordinates);
$marker->setTitle($item->tooltip);
$feature->addFeature($marker->getFeature());
}
}
return $feature;
}
}

View File

@@ -11,20 +11,12 @@
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Netzmacht\Contao\Leaflet\Mapper\AbstractTypeMapper;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
class ProviderLayerMapper extends AbstractTypeMapper
class ProviderLayerMapper extends AbstractLayerMapper
{
/**
* Class of the model being build.
*
* @var string
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\LayerModel';
/**
* Class of the definition being created.
*
@@ -61,7 +53,7 @@ class ProviderLayerMapper extends AbstractTypeMapper
/**
* {@inheritdoc}
*/
protected function createInstance(\Model $model, DefinitionMapper $mapper)
protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
if (isset($this->providers[$model->tile_provider]['class'])) {
$class = $this->providers[$model->tile_provider]['class'];
@@ -70,7 +62,7 @@ class ProviderLayerMapper extends AbstractTypeMapper
}
$reflector = new \ReflectionClass($class);
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds));
return $instance;
}
@@ -78,7 +70,7 @@ class ProviderLayerMapper extends AbstractTypeMapper
/**
* {@inheritdoc}
*/
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder)
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
if (!empty($this->providers[$model->tile_provider]['options'])) {
$this->applyOptions(
@@ -92,7 +84,7 @@ class ProviderLayerMapper extends AbstractTypeMapper
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
return array(
$model->alias ?: ('layer_' . $model->id),

View File

@@ -16,6 +16,7 @@ use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Plugins\LeafletProviders\Provider;
class MapMapper extends AbstractMapper
@@ -50,19 +51,19 @@ class MapMapper extends AbstractMapper
/**
* @inheritdoc
*/
protected function doBuild(Definition $map, \Model $model, DefinitionMapper $builder)
protected function doBuild(Definition $map, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
if ($map instanceof Map && $model instanceof MapModel) {
$this->buildCustomOptions($map, $model);
$this->buildControls($map, $model, $builder);
$this->buildLayers($map, $model, $builder);
$this->buildControls($map, $model, $builder, $bounds);
$this->buildLayers($map, $model, $builder, $bounds);
}
}
/**
* @inheritdoc
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
return array(
$mapper->getMapId(),
@@ -91,8 +92,9 @@ class MapMapper extends AbstractMapper
* @param Map $map The map being built.
* @param MapModel $model The map model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds.
*/
private function buildControls(Map $map, MapModel$model, DefinitionMapper $mapper)
private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$collection = ControlModel::findBy(
array('pid=?', 'active=1'),
@@ -102,7 +104,7 @@ class MapMapper extends AbstractMapper
if ($collection) {
foreach ($collection as $control) {
$control = $mapper->handle($control);
$control = $mapper->handle($control, $bounds);
$map->addControl($control);
}
}
@@ -114,16 +116,22 @@ class MapMapper extends AbstractMapper
* @param Map $map The map being built.
* @param MapModel $model The map model.
* @param DefinitionMapper $mapper Definition mapper.
* @param LatLngBounds $bounds Optional bounds.
*/
private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper)
private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$ids = deserialize($model->layers, true);
$collection = LayerModel::findMultipleByIds($ids);
if ($collection) {
foreach ($collection as $layer) {
if (!$layer->active) {
continue;
}
$layer = $mapper->handle($layer, $bounds);
/** @var Provider $layer */
$layer = $mapper->handle($layer);
$map->addLayer($layer);
}
}

View File

@@ -12,21 +12,23 @@
namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
interface Mapper
{
/**
* Map model to the definition.
*
* @param \Model $model The model being built.
* @param DefinitionMapper $builder The definition builder.
* @param \Model $model The model being built.
* @param DefinitionMapper $mapper The definition builder.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
*
* @return Definition
*/
public function handle(\Model $model, DefinitionMapper $builder);
public function handle(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null);
/**
* Check if builder is responsible for the model.
* Check if mapper is responsible for the model.
*
* @param \Model $model The model being build.
*