Rework element id generation.

This commit is contained in:
David Molineus
2015-01-09 13:41:09 +01:00
parent 37851110b0
commit efb36256c7
12 changed files with 128 additions and 88 deletions

View File

@@ -151,9 +151,9 @@ abstract class AbstractMapper implements Mapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null) public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null)
{ {
$definition = $this->createInstance($model, $mapper, $bounds); $definition = $this->createInstance($model, $mapper, $bounds, $elementId);
$this->buildOptions($definition, $model); $this->buildOptions($definition, $model);
$this->buildConditionals($definition, $model); $this->buildConditionals($definition, $model);
@@ -202,16 +202,21 @@ abstract class AbstractMapper implements Mapper
/** /**
* Create a new definition instance. * Create a new definition instance.
* *
* @param \Model $model The model. * @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper. * @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in. * @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param string|null $elementId Optional element id.
* *
* @return Definition * @return Definition
*/ */
protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function createInstance(
{ \Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$reflector = new \ReflectionClass($this->getClassName($model, $mapper, $bounds)); $reflector = new \ReflectionClass($this->getClassName($model, $mapper, $bounds));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds)); $instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds, $elementId));
return $instance; return $instance;
} }
@@ -222,13 +227,18 @@ abstract class AbstractMapper implements Mapper
* @param \Model $model The model. * @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper. * @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in. * @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param string|null $elementId Optional element id.
* *
* @return array * @return array
*/ */
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array( return array(
$model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id) $this->getElementId($model, $elementId)
); );
} }
@@ -346,4 +356,21 @@ abstract class AbstractMapper implements Mapper
{ {
return static::$definitionClass; return static::$definitionClass;
} }
/**
* Create element id for the model.
*
* @param \Model $model The model being passed.
* @param string|null $elementId Optional forced id.
*
* @return string
*/
protected function getElementId(\Model $model, $elementId = null)
{
if ($elementId) {
return $elementId;
}
return $model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id);
}
} }

View File

@@ -50,8 +50,12 @@ class AttributionControlMapper extends AbstractControlMapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null) protected function build(
{ Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
if (!$definition instanceof Attribution) { if (!$definition instanceof Attribution) {
return; return;
} }

View File

@@ -32,9 +32,16 @@ class LayersControlMapper extends AbstractControlMapper
*/ */
protected static $type = 'layers'; protected static $type = 'layers';
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) /**
{ * {@inheritdoc}
$arguments = parent::buildConstructArguments($model, $mapper, $bounds); */
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[1] = array(); $arguments[1] = array();
$arguments[2] = array(); $arguments[2] = array();

View File

@@ -39,13 +39,6 @@ class DefinitionMapper
*/ */
private $eventDispatcher; private $eventDispatcher;
/**
* Map id of the current built map.
*
* @var string
*/
private $mapId;
/** /**
* @var array * @var array
*/ */
@@ -73,21 +66,11 @@ class DefinitionMapper
{ {
$this->builders[$priority][] = $builder; $this->builders[$priority][] = $builder;
ksort($this->builders); krsort($this->builders);
return $this; return $this;
} }
/**
* Get the map id of the current built map.
*
* @return string
*/
public function getMapId()
{
return $this->mapId;
}
/** /**
* Build a model. * Build a model.
* *
@@ -105,15 +88,15 @@ class DefinitionMapper
return $this->mapped[$hash]; return $this->mapped[$hash];
} }
$this->mapId = $elementId ?: ($model->alias ?: ('map_' . $model->id));
foreach ($this->builders as $builders) { foreach ($this->builders as $builders) {
foreach($builders as $builder) { foreach($builders as $builder) {
if ($builder->match($model)) { if ($builder->match($model)) {
$definition = $builder->handle($model, $this, $bounds); $definition = $builder->handle($model, $this, $bounds, $elementId);
$event = new BuildDefinitionEvent($definition, $model, $bounds); if ($definition) {
$this->eventDispatcher->dispatch($event::NAME, $event); $event = new BuildDefinitionEvent($definition, $model, $bounds);
$this->eventDispatcher->dispatch($event::NAME, $event);
}
$this->mapped[$hash] = $definition; $this->mapped[$hash] = $definition;

View File

@@ -68,9 +68,11 @@ class GroupLayerMapper extends AbstractLayerMapper
if ($collection) { if ($collection) {
foreach ($collection as $layerModel) { foreach ($collection as $layerModel) {
/** @var Layer $layer */
$layer = $mapper->handle($layerModel); $layer = $mapper->handle($layerModel);
$definition->addLayer($layer);
if ($layer instanceof Layer) {
$definition->addLayer($layer);
}
} }
} }
} }

View File

@@ -65,8 +65,12 @@ class ProviderLayerMapper extends AbstractLayerMapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null) protected function build(
{ Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
if (!empty($this->providers[$model->tile_provider]['options'])) { if (!empty($this->providers[$model->tile_provider]['options'])) {
$this->applyOptions( $this->applyOptions(
$this->providers[$model->tile_provider]['options'], $this->providers[$model->tile_provider]['options'],
@@ -79,8 +83,12 @@ class ProviderLayerMapper extends AbstractLayerMapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array( return array(
$model->alias ?: ('layer_' . $model->id), $model->alias ?: ('layer_' . $model->id),
$model->tile_provider, $model->tile_provider,

View File

@@ -15,9 +15,11 @@ use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Control;
use Netzmacht\LeafletPHP\Definition\Layer;
use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds; use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Plugins\LeafletProviders\Provider;
class MapMapper extends AbstractMapper class MapMapper extends AbstractMapper
{ {
@@ -63,11 +65,15 @@ class MapMapper extends AbstractMapper
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array( return array(
$mapper->getMapId(), $this->getElementId($model, $elementId),
$mapper->getMapId() $this->getElementId($model, $elementId)
); );
} }
@@ -96,15 +102,16 @@ class MapMapper extends AbstractMapper
*/ */
private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{ {
$collection = ControlModel::findBy( $collection = ControlModel::findActiveBy('pid', $model->id, array('order' => 'sorting'));
array('pid=?', 'active=1'),
array($model->id),
array('order' => 'sorting')
);
if ($collection) { if (!$collection) {
foreach ($collection as $control) { return;
$control = $mapper->handle($control, $bounds); }
foreach ($collection as $control) {
$control = $mapper->handle($control, $bounds);
if ($control instanceof Control) {
$map->addControl($control); $map->addControl($control);
} }
} }
@@ -130,9 +137,9 @@ class MapMapper extends AbstractMapper
} }
$layer = $mapper->handle($layer, $bounds); $layer = $mapper->handle($layer, $bounds);
if ($layer instanceof Layer) {
/** @var Provider $layer */ $map->addLayer($layer);
$map->addLayer($layer); }
} }
} }
} }

View File

@@ -19,13 +19,14 @@ interface Mapper
/** /**
* Map model to the definition. * Map model to the definition.
* *
* @param \Model|mixed $model The model being built. Usually a contao model. but can be anything * @param \Model|mixed $model The model being built. Usually a contao model. but can be anything
* @param DefinitionMapper $mapper The definition builder. * @param DefinitionMapper $mapper The definition builder.
* @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.
* *
* @return Definition * @return Definition
*/ */
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null); public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null);
/** /**
* Check if mapper is responsible for the model. * Check if mapper is responsible for the model.

View File

@@ -37,9 +37,13 @@ class ImageIconMapper extends AbstractIconMapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
$arguments = parent::buildConstructArguments($model, $mapper, $bounds); DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
if ($model->iconImage) { if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage); $file = \FilesModel::findByUuid($model->iconImage);

View File

@@ -39,9 +39,13 @@ class MarkerMapper extends AbstractMapper
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
$arguments = parent::buildConstructArguments($model, $mapper, $bounds); DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[] = $model->coordinates ?: null; $arguments[] = $model->coordinates ?: null;
return $arguments; return $arguments;

View File

@@ -30,20 +30,9 @@ class AbstractVectorMapper extends AbstractTypeMapper
*/ */
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel'; protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel';
protected function initialize() /**
{ * {@inheritdoc}
parent::initialize(); */
// $this
// ->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className')
// ->addConditionalOption('color')
// ->addConditionalOption('lineCap')
// ->addConditionalOption('lineJoin')
// ->addConditionalOption('dashArray')
// ->addConditionalOptions('fill', array('fill', 'fillColor', 'fillOpacity'))
// ;
}
protected function build( protected function build(
Definition $definition, Definition $definition,
\Model $model, \Model $model,

View File

@@ -40,8 +40,12 @@ class RectangleMapper extends AbstractVectorMapper
parent::initialize(); parent::initialize();
} }
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null) protected function buildConstructArguments(
{ \Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$latLngs = array_map( $latLngs = array_map(
function($latLng) { function($latLng) {
return LatLng::fromString($latLng); return LatLng::fromString($latLng);
@@ -49,7 +53,7 @@ class RectangleMapper extends AbstractVectorMapper
deserialize($model->bounds, true) deserialize($model->bounds, true)
); );
$arguments = parent::buildConstructArguments($model, $mapper, $bounds); $arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[] = new LatLngBounds($latLngs[0], $latLngs[1]); $arguments[] = new LatLngBounds($latLngs[0], $latLngs[1]);
return $arguments; return $arguments;