Skip to content
Browse files

initial refactor

  • Loading branch information...
1 parent 9353d6d commit 74d5615e340d264f98070d9a9c8981de46dce28f @dustin10 committed Oct 3, 2011
View
36 Adapter/AdapterInterface.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Vich\GeographicalBundle\Adapter;
+
+use Doctrine\Common\EventArgs;
+
+/**
+ * AdapterInterface.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+interface AdapterInterface
+{
+ /**
+ * Gets the mapped object from the event arguments.
+ *
+ * @param EventArgs $e The event arguments.
+ * @return object The mapped object.
+ */
+ function getObjectFromArgs(EventArgs $e);
+
+ /**
+ * Notifies the ORM layer that a recompute of the entities changeset is necessary.
+ *
+ * @param EventArgs $e The event arguments.
+ */
+ function invokeChangesetRecompute(EventArgs $e);
+
+ /**
+ * Determines if the specified object is a proxy.
+ *
+ * @param mixed $obj The object to test.
+ * @return boolean True if a proxy object, false otherwise.
+ */
+ function isProxy($obj);
+}
View
44 Adapter/ODM/MongoDB/MongoDBAdapter.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Vich\GeographicalBundle\Adapter\ORM;
+
+use Vich\GeographicalBundle\Adapter\AdapterInterface;
+use Doctrine\Common\EventArgs;
+use Doctrine\ODM\MongoDB\Proxy\Proxy;
+
+/**
+ * MongoDBAdapter.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class MongoDBAdapter implements AdapterInterface
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function getObjectFromArgs(EventArgs $e)
+ {
+ return $e->getDocument();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function invokeChangesetRecompute(EventArgs $e)
+ {
+ $obj = $this->getObjectFromArgs($e);
+
+ $dm = $args->getDocumentManager();
+ $uow = $dm->getUnitOfWork();
+ $metadata = $dm->getClassMetadata(get_class($obj));
+ $uow->recomputeSingleDocumentChangeSet($metadata, $obj);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function isProxy($obj)
+ {
+ return $obj instanceof Proxy;
+ }
+}
View
44 Adapter/ORM/DoctrineORMAdapter.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Vich\GeographicalBundle\Adapter\ORM;
+
+use Vich\GeographicalBundle\Adapter\AdapterInterface;
+use Doctrine\Common\EventArgs;
+use Doctrine\ORM\Proxy\Proxy;
+
+/**
+ * DoctrineORMAdapter.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class DoctrineORMAdapter implements AdapterInterface
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function getObjectFromArgs(EventArgs $e)
+ {
+ return $e->getEntity();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function invokeChangesetRecompute(EventArgs $e)
+ {
+ $obj = $this->getObjectFromArgs($e);
+
+ $em = $args->getEntityManager();
+ $uow = $em->getUnitOfWork();
+ $metadata = $em->getClassMetadata(get_class($obj));
+ $uow->recomputeSingleEntityChangeSet($metadata, $obj);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function isProxy($obj)
+ {
+ return $obj instanceof Proxy;
+ }
+}
View
17 DependencyInjection/VichGeographicalExtension.php
@@ -16,13 +16,16 @@
class VichGeographicalExtension extends Extension
{
/**
- * @var array $driverMap
+ * @var array $adapterMap
*/
- private $driverMap = array(
- 'orm' => 'Vich\GeographicalBundle\Listener\ORM\GeographicalListener',
- 'mongodb' => 'Vich\GeographicalBundle\Listener\ODM\MongoDB\GeographicalListener'
+ private $adapterMap = array(
+ 'orm' => 'Vich\GeographicalBundle\Adapter\ORM\DoctrineORMAdapter',
+ 'mongodb' => 'Vich\GeographicalBundle\Adapter\ODM\MongoDB\MongoDBAdapter'
);
+ /**
+ * @var array $tagMap
+ */
private $tagMap = array(
'orm' => 'doctrine.event_subscriber',
'mongodb' => 'doctrine.common.event_subscriber'
@@ -42,13 +45,13 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
- $toLoad = array('query.xml', 'map.xml', 'listener.xml', 'twig.xml');
+ $toLoad = array('query.xml', 'map.xml', 'listener.xml', 'twig.xml', 'adapter.xml', 'templating.xml');
foreach ($toLoad as $file) {
$loader->load($file);
}
$dbDriver = strtolower($config['db_driver']);
- if (!in_array($dbDriver, array_keys($this->driverMap))) {
+ if (!in_array($dbDriver, array_keys($this->tagMap))) {
throw new \InvalidArgumentException(
sprintf(
'Invalid "db_driver" configuration option specified: "%s"',
@@ -57,7 +60,7 @@ public function load(array $configs, ContainerBuilder $container)
);
}
- $container->setParameter('vich_geographical.listener.geographical.class', $this->driverMap[$dbDriver]);
+ $container->setParameter('vich_geographical.adapter.class', $this->adapterMap[$dbDriver]);
$container->getDefinition('vich_geographical.listener.geographical')->addTag($this->tagMap[$dbDriver]);
$templateEngine = strtolower($config['templating']['engine']);
View
47 Driver/AnnotationDriver.php
@@ -3,6 +3,7 @@
namespace Vich\GeographicalBundle\Driver;
use Doctrine\Common\Annotations\Reader;
+use Vich\GeographicalBundle\Adapter\AdapterInterface;
/**
* AnnotationDriver.
@@ -14,16 +15,37 @@ class AnnotationDriver
/**
* @var Reader $reader
*/
- private $reader;
+ protected $reader;
+
+ /**
+ * @var AdapterInterface $adapter
+ */
+ protected $adapter;
+
+ /**
+ * @var string $geoClass
+ */
+ protected $geoClass;
+
+ /**
+ * @var string $geoQueryClass
+ */
+ protected $geoQueryClass;
/**
* Constructs a new intsance of AnnotationDriver.
*
* @param Reader $reader The annotation reader
+ * @param AdapterInterface $apapter The adapter
+ * @param string $geoClass The geographical annotaion class name
+ * @param string $geoQueryClass The geographical query annotaion class name
*/
- public function __construct(Reader $reader)
+ public function __construct(Reader $reader, AdapterInterface $adapter, $geoClass, $geoQueryClass)
{
$this->reader = $reader;
+ $this->adapter = $adapter;
+ $this->geoClass = $geoClass;
+ $this->geoQueryClass = $geoQueryClass;
}
/**
@@ -32,15 +54,15 @@ public function __construct(Reader $reader)
* @param object $obj The object
* @return Geographical The geographical annotation
*/
- public function getGeographicalAnnotation($obj)
+ public function readGeoAnnotation($obj)
{
if (!is_object($obj)) {
- throw new \InvalidArgumentException();
+ throw new \InvalidArgumentException('The variable is not an object.');
}
$refClass = $this->resolveProxy($obj);
- return $this->reader->getClassAnnotation($refClass, 'Vich\GeographicalBundle\Annotation\Geographical');
+ return $this->reader->getClassAnnotation($refClass, $this->geoClass);
}
/**
@@ -49,7 +71,7 @@ public function getGeographicalAnnotation($obj)
* @param object $obj The object
* @return GeographicalQuery The geographical query annotation
*/
- public function getGeographicalQueryAnnotation($obj)
+ public function readGeoQueryAnnotation($obj)
{
if (!is_object($obj)) {
throw new \InvalidArgumentException();
@@ -58,7 +80,7 @@ public function getGeographicalQueryAnnotation($obj)
$refClass = new \ReflectionClass($obj);
foreach ($refClass->getMethods() as $method) {
- $annot = $this->reader->getMethodAnnotation($method, 'Vich\GeographicalBundle\Annotation\GeographicalQuery');
+ $annot = $this->reader->getMethodAnnotation($method, $this->geoQueryClass);
if (null !== $annot) {
$annot->setMethod($method->getName());
return $annot;
@@ -72,18 +94,15 @@ public function getGeographicalQueryAnnotation($obj)
* Tests an object to see if it is a proxy, if so return the \ReflectionClass
* object representing its parent.
*
- * @param type $obj The object to test
+ * @param object $obj The object to test
* @return \ReflectionClass The reflection class
*/
protected function resolveProxy($obj)
{
- // this needs to be refactored as there is a chance that 'Proxies' is not
- // the configured namespace, but will work for now...
- $refClass = new \ReflectionClass($obj);
- if (false !== strpos($refClass->getName(), 'Proxies\\')) {
- $refClass = new \ReflectionClass(get_parent_class($obj));
+ if ($this->adapter->isProxy($obj)) {
+ return new \ReflectionClass(get_parent_class($obj));
}
- return $refClass;
+ return $refClass = new \ReflectionClass($obj);
}
}
View
139 EventListener/GeographicalListener.php
@@ -0,0 +1,139 @@
+<?php
+
+namespace Vich\GeographicalBundle\EventListener;
+
+use Vich\GeographicalBundle\Driver\AnnotationDriver;
+use Vich\GeographicalBundle\QueryService\QueryServiceInterface;
+use Vich\GeographicalBundle\Adapter\AdapterInterface;
+use Doctrine\Common\EventArgs;
+use Doctrine\Common\EventSubscriber;
+
+/**
+ * GeographicalListener.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class GeographicalListener implements EventSubscriber
+{
+ /**
+ * @var QueryServiceInterface $queryService
+ */
+ protected $queryService;
+
+ /**
+ * @var AnnotationDriver $reader
+ */
+ protected $driver;
+
+ /**
+ * @var AdapterInterface $adapter
+ */
+ protected $adapter;
+
+ /**
+ * Constructs a new instance of GeographicalListener.
+ *
+ * @param QueryServiceInterface $queryService The query service.
+ * @param AnnotationDriver $driver The driver.
+ * @param AdapterInterface $adapter The database adapter.
+ */
+ function __construct(QueryServiceInterface $queryService, AnnotationDriver $driver, AdapterInterface $adapter)
+ {
+ $this->queryService = $queryService;
+ $this->driver = $driver;
+ $this->adapter = $adapter;
+ }
+
+ /**
+ * The events the listener is subscribed to.
+ *
+ * @return array An array
+ */
+ public function getSubscribedEvents()
+ {
+ return array(
+ 'prePersist',
+ 'preUpdate'
+ );
+ }
+
+ /**
+ * Checks for persisted object to update coordinates
+ *
+ * @param EventArgs $args The event arguments
+ */
+ public function prePersist(EventArgs $args)
+ {
+ $obj = $this->adapter->getObjectFromArgs($args);
+
+ $geo = $this->driver->readGeoAnnotation($obj);
+ $geoQuery = $this->driver->readGeoQueryAnnotation($obj);
+
+ $this->updateObject($obj, $geo, $geoQuery);
+ }
+
+ /**
+ * Update coordinates on objects being updated before update
+ * if they require changing
+ *
+ * @param EventArgs $args The event arguments
+ */
+ public function preUpdate(EventArgs $args)
+ {
+ $obj = $this->adapter->getObjectFromArgs($args);
+
+ $geo = $this->driver->readGeoAnnotation($obj);
+ if ($this->shouldQueryOnUpdate($geo)) {
+ $geoQuery = $this->driver->readGeoQueryAnnotation($obj);
+
+ $this->updateObject($obj, $geo, $geoQuery, true, $args);
+ }
+ }
+
+ /**
+ * Queries the service for coordinates.
+ *
+ * @param object $obj The object.
+ * @param Geographical $geo The greographical annotation.
+ * @param GeographicalQuery $geoQuery The geogrphical query annotation.
+ * @param boolean $isUpdate True if the object is being updated, false otherwise.
+ * @param EventArgs $eventArgs The event args.
+ */
+ protected function updateObject($obj, $geo, $geoQuery, $isUpdate = false, EventArgs $eventArgs = null)
+ {
+ if (null === $geo) {
+ return;
+ }
+
+ if (null === $geoQuery) {
+ throw new \InvalidArgumentException('Unable to find GeograhicalQuery annotation.');
+ }
+
+ $queryMethod = $geoQuery->getMethod();
+ $query = $obj->$queryMethod();
+
+ $result = $this->queryService->queryCoordinates($query);
+
+ $latSetter = sprintf('set%s', $geo->getLat());
+ $lngSetter = sprintf('set%s', $geo->getLng());
+
+ $obj->$latSetter($result->getLatitude());
+ $obj->$lngSetter($result->getLongitude());
+
+ if ($isUpdate) {
+ $this->adapter->invokeChangesetRecompute($eventArgs);
+ }
+ }
+
+ /**
+ * Determines if the object coordinates should be updated when the object
+ * is udpated.
+ *
+ * @param Geographical $annot The Geographical annotation.
+ * @return boolean True if should query, false otherwise.
+ */
+ protected function shouldQueryOnUpdate(Geographical $annot)
+ {
+ return (null !== $annot) && ($annot->getOn() === Geographical::ON_UPDATE);
+ }
+}
View
73 Map/Coordinate/MapCoordinate.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Coordinate;
+
+/**
+ * MapCoordinate.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class MapCoordinate
+{
+ /**
+ * @var double $lat
+ */
+ private $lat;
+
+ /**
+ * @var double $lng
+ */
+ private $lng;
+
+ /**
+ * Gets the latitude.
+ *
+ * @return double The latitude
+ */
+ public function getLat()
+ {
+ return $this->lat;
+ }
+
+ /**
+ * Sets the latitude.
+ *
+ * @param double $value The latitude
+ */
+ public function setLat($value)
+ {
+ $this->lat = $value;
+ }
+
+ /**
+ * Gets the longitude.
+ *
+ * @return double The longitude
+ */
+ public function getLng()
+ {
+ return $this->lng;
+ }
+
+ /**
+ * Sets the longitude.
+ *
+ * @param double $value The longitude
+ */
+ public function setLng($value)
+ {
+ $this->lng = $value;
+ }
+
+ /**
+ * Constructs a new instance of MapCoordinate.
+ *
+ * @param double $lat The latitude
+ * @param double $lng The longitude
+ */
+ public function __construct($lat = 0, $lng = 0)
+ {
+ $this->lat = $lat;
+ $this->lng = $lng;
+ }
+}
View
6 Map/Map.php
@@ -2,8 +2,8 @@
namespace Vich\GeographicalBundle\Map;
-use Vich\GeographicalBundle\Map\MapCoordinate;
-use Vich\GeographicalBundle\Map\MapMarker;
+use Vich\GeographicalBundle\Map\Coordinate\MapCoordinate;
+use Vich\GeographicalBundle\Map\Marker\MapMarker;
use Vich\GeographicalBundle\Map\Renderer\MapRendererInterface;
use Vich\GeographicalBundle\Map\Renderer\MapRenderer;
@@ -30,7 +30,7 @@ class Map
);
/**
- * @var Vich\GeographicalBundle\Map\MapCoordinate $center
+ * @var MapCoordinate $center
*/
private $center = null;
View
21 Map/Marker/Icon/DefaultIconGenerator.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker\Icon;
+
+use Vich\GeographicalBundle\Map\Marker\Icon\IconGeneratorInterface;
+
+/**
+ * DefaultIconGenerator.
+ *
+ * @author Dustin Dobervich
+ */
+class DefaultIconGenerator implements IconGeneratorInterface
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function generateIcon($obj)
+ {
+ return null;
+ }
+}
View
19 Map/Marker/Icon/IconGeneratorInterface.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker\Icon;
+
+/**
+ * IconGeneratorInterface.
+ *
+ * @author Dustin Dobervich
+ */
+interface IconGeneratorInterface
+{
+ /**
+ * Generates the map marker icon url for the specified entity.
+ *
+ * @param object $obj The entity
+ * @return string The map marker icon url
+ */
+ function generateIcon($obj);
+}
View
150 Map/Marker/InfoWindow/InfoWindow.php
@@ -0,0 +1,150 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker\InfoWindow;
+
+/**
+ * InfoWindow.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class InfoWindow
+{
+ /**
+ * @var string $varName
+ */
+ protected $varName;
+
+ /**
+ * @var integer $width
+ */
+ protected $width;
+
+ /**
+ * @var integer $height
+ */
+ protected $height;
+
+ /**
+ * @var string $title
+ */
+ protected $title;
+
+ /**
+ * @var string $content
+ */
+ protected $content;
+
+ /**
+ * Gets the variable name of the info window
+ *
+ * @return string The variable name
+ */
+ public function getVarName()
+ {
+ return $this->varName;
+ }
+
+ /**
+ * Sets the variable name of the info window.
+ *
+ * @param string $value The variable name
+ */
+ public function setVarName($value)
+ {
+ $this->varName = $value;
+ }
+
+ /**
+ * Gets the info window width.
+ *
+ * @return integer The width
+ */
+ public function getWidth()
+ {
+ return $this->width;
+ }
+
+ /**
+ * Sets the info window width.
+ *
+ * @param integer $value The width
+ */
+ public function setWidth($value)
+ {
+ $this->width = $value;
+ }
+
+ /**
+ * Gets the info window height.
+ *
+ * @return integer The height
+ */
+ public function getHeight()
+ {
+ return $this->height;
+ }
+
+ /**
+ * Sets the info window height.
+ *
+ * @param integer $value The height
+ */
+ public function setHeight($value)
+ {
+ $this->width = $value;
+ }
+
+ /**
+ * Gets the info window title.
+ *
+ * @return string The title
+ */
+ public function getTitle()
+ {
+ return $this->title;
+ }
+
+ /**
+ * Sets the info window title.
+ *
+ * @param string $value The title
+ */
+ public function setTitle($value)
+ {
+ $this->title = $value;
+ }
+
+ /**
+ * Gets the content of the info window
+ *
+ * @return string The content
+ */
+ public function getContent()
+ {
+ return $this->content;
+ }
+
+ /**
+ * Sets the content of the info window
+ *
+ * @return string $value The content
+ */
+ public function setContent($value)
+ {
+ $this->content = $value;
+ }
+
+ /**
+ * Constucts a new instance of MapMarkerInfoWindow.
+ *
+ * @param string $content The content
+ */
+ public function __construct($content = '')
+ {
+ $this->content = $content;
+ $this->varName = sprintf('iw%s', uniqid());
+ $this->title = '';
+ $this->width = 250;
+ $this->height = 150;
+ }
+}
View
50 Map/Marker/InfoWindow/InfoWindowRenderer.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker\InfoWindow;
+
+use Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindowRendererInterface;
+use Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindow;
+
+/**
+ * InfoWindowBuilder.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class InfoWindowRenderer implements InfoWindowRendererInterface
+{
+ /**
+ * @var object $templating
+ */
+ private $templating;
+
+ /**
+ * @var string $templateName
+ */
+ private $templateName;
+
+ /**
+ * Constructs a new instance of InfoWindowRenderer.
+ *
+ * @param object $templating The templating engine
+ * @param string $templateName The template name
+ */
+ public function __construct($templating, $templateName)
+ {
+ $this->templating = $templating;
+ $this->templateName = $templateName;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function build($obj)
+ {
+ $content = $this->templating->render($this->templateName, array('obj' => $obj));
+
+ // escpape quotes and remove newlines and tabs
+ $content = str_replace('"', '\"', $content);
+ $content = preg_replace("/[\n\r\t]/", "", $content);
+
+ return new InfoWindow($content);
+ }
+}
View
19 Map/Marker/InfoWindow/InfoWindowRendererInterface.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker\InfoWindow;
+
+/**
+ * InfoWindowRendererInterface.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+interface InfoWindowRendererInterface
+{
+ /**
+ * Builds the info window for the object.
+ *
+ * @param object $obj
+ * @return InfoWindow The info window
+ */
+ function build($obj);
+}
View
133 Map/Marker/MapMarker.php
@@ -0,0 +1,133 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Marker;
+
+use Vich\GeographicalBundle\Map\Coordinate\MapCoordinate;
+use Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindow;
+
+/**
+ * MapMarker.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class MapMarker
+{
+ /**
+ * @var string $varName
+ */
+ private $varName;
+
+ /**
+ * @var MapCoordinate $coordinate
+ */
+ private $coordinate;
+
+ /**
+ * @var string $icon
+ */
+ private $icon;
+
+ /**
+ * @var InfoWindow $infoWindow
+ */
+ private $infoWindow;
+
+ /**
+ * Gets the javascript variable name.
+ *
+ * @return type The javascript variable name
+ */
+ public function getVarName()
+ {
+ return $this->varName;
+ }
+
+ /**
+ * Sets the javascript variable name.
+ *
+ * @param type $value The javascript variable name
+ */
+ public function setVarName($value)
+ {
+ $this->varName = $value;
+ }
+
+ /**
+ * Gets the coordinate.
+ *
+ * @return MapCoordinate The coordinate
+ */
+ public function getCoordinate()
+ {
+ return $this->coordinate;
+ }
+
+ /**
+ * Sets the coordinate.
+ *
+ * @param MapCoordinate $value The coordinate
+ */
+ public function setCoordinate(MapCoordinate $value)
+ {
+ $this->coordinate = $value;
+ }
+
+ /**
+ * Gets the icon url.
+ *
+ * @return string The icon url
+ */
+ public function getIcon()
+ {
+ return $this->icon;
+ }
+
+ /**
+ * Sets the icon url.
+ *
+ * @param string $value The icon url
+ */
+ public function setIcon($value)
+ {
+ $this->icon = $value;
+ }
+
+ /**
+ * Gets the map marker's info window.
+ *
+ * @return InfoWindow The info window
+ */
+ public function getInfoWindow()
+ {
+ return $this->infoWindow;
+ }
+
+ /**
+ * Sets the map marker's info window.
+ *
+ * @param InfoWindow $infoWindow The info window
+ */
+ public function setInfoWindow(InfoWindow $infoWindow)
+ {
+ $this->infoWindow = $infoWindow;
+ }
+
+ /**
+ * Constructs a new instance of MapMarker.
+ *
+ * @param type $lat The latitude
+ * @param type $lng The longitude
+ * @param type $icon The icon url
+ * @param type $varName The javascript variable name
+ */
+ public function __construct($lat, $lng, $icon = null, $varName = '')
+ {
+ $this->coordinate = new MapCoordinate($lat, $lng);
+ $this->icon = $icon;
+
+ $this->varName = $varName;
+ if ($this->varName === '') {
+ $this->varName = sprintf('mapMarker%s', uniqid());
+ }
+ }
+}
View
73 Map/Provider/MapProvider.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Vich\GeographicalBundle\Map\Provider;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * MapProvider.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class MapProvider
+{
+ /**
+ * @var ContainerInterface $container
+ */
+ private $container;
+
+ /**
+ * @var array $mapServiceIds
+ */
+ private $mapServiceIds = array();
+
+ /**
+ * @var array $map
+ */
+ private $maps = array();
+
+ /**
+ * Constructs a new instance of ContainerInterface.
+ *
+ * @param ContainerInterface $container The container
+ */
+ public function __construct(ContainerInterface $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * Adds a Map.
+ *
+ * @param Map $map The map
+ * @param string $alias The map alias
+ */
+ public function addMapServiceId($id, $alias)
+ {
+ $this->mapServiceIds[$alias] = $id;
+ }
+
+ /**
+ * Gets a Map.
+ *
+ * @param Map $alias A Map alias
+ */
+ public function getMap($alias)
+ {
+ if (!array_key_exists($alias, $this->maps)) {
+ $this->loadMap($alias);
+ }
+
+ return $this->maps[$alias];
+ }
+
+ /**
+ * Loads a Map from the container.
+ *
+ * @param string $alias The Map alias
+ */
+ private function loadMap($alias)
+ {
+ $this->maps[$alias] = $this->container->get($this->mapServiceIds[$alias]);
+ }
+}
View
2 QueryService/GoogleQueryService.php
@@ -17,7 +17,7 @@ class GoogleQueryService implements QueryServiceInterface
*
* @param QueryResult $query The query
*/
- public function queryForCoordinates($query)
+ public function queryCoordinates($query)
{
$result = new QueryResult();
View
4 QueryService/QueryResult.php
@@ -12,12 +12,12 @@ class QueryResult
/**
* @var double $latitude
*/
- private $latitude = 0;
+ protected $latitude = 0;
/**
* @var double $longitude
*/
- private $longitude = 0;
+ protected $longitude = 0;
/**
* Gets the latitude.
View
2 QueryService/QueryServiceInterface.php
@@ -14,5 +14,5 @@
*
* @return QueryResult The result.
*/
- function queryForCoordinates($query);
+ function queryCoordinates($query);
}
View
13 Resources/config/adapter.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <services>
+
+ <service id="vich_geographical.adapter" class="%vich_geographical.adapter.class%" public="false" />
+
+ </services>
+
+</container>
View
11 Resources/config/listener.xml
@@ -6,13 +6,10 @@
<services>
- <service id="vich_geographical.listener.geographical" class="%vich_geographical.listener.geographical.class%" public="false">
- <call method="setAnnotationDriver">
- <argument type="service" id="vich_geographical.annotation_driver" />
- </call>
- <call method="setQueryService">
- <argument type="service" id="vich_geographical.query_service" />
- </call>
+ <service id="vich_geographical.listener.geographical" class="Vich\GeographicalBundle\EventListener\GeographicalListener" public="false">
+ <argument type="service" id="vich_geographical.query_service" />
+ <argument type="service" id="vich_geographical.annotation_driver" />
+ <argument type="service" id="vich_geographical.adapter" />
</service>
</services>
View
20 Resources/config/map.xml
@@ -3,11 +3,19 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <parameters>
+ <parameter key="vich_geographical.annotation.geographical.class">Vich\GeographicalBundle\Annotation\Geographical</parameter>
+ <parameter key="vich_geographical.annotation.geographical_query.class">Vich\GeographicalBundle\Annotation\GeographicalQuery</parameter>
+ </parameters>
<services>
<service id="vich_geographical.annotation_driver" class="Vich\GeographicalBundle\Driver\AnnotationDriver" public="false">
<argument type="service" id="annotation_reader" />
+ <argument type="service" id="vich_geographical.adapter" />
+ <argument>%vich_geographical.annotation.geographical.class%</argument>
+ <argument>%vich_geographical.annotation.geographical_query.class%</argument>
</service>
<service id="vich_geographical.map_renderer.google" class="Vich\GeographicalBundle\Map\Renderer\GoogleMapRenderer" public="false">
@@ -26,19 +34,11 @@
<argument>%vich_geographical.map_renderer.options%</argument>
</service>
- <service id="vich_geographical.map_provider" class="Vich\GeographicalBundle\Map\MapProvider" public="false">
+ <service id="vich_geographical.map_provider" class="Vich\GeographicalBundle\Map\Provider\MapProvider" public="false">
<argument type="service" id="service_container" />
</service>
- <service id="vich_geographical.icon_generator.default" class="Vich\GeographicalBundle\Map\Generator\DefaultMapMarkerIconGenerator" public="false" />
-
- <service id="vich_geographical.map_helper" class="Vich\GeographicalBundle\Templating\Helper\MapHelper" public="false">
- <argument type="service" id="vich_geographical.annotation_driver" />
- <argument type="service" id="vich_geographical.map_provider" />
- <argument type="service" id="vich_geographical.map_renderer" />
- <argument type="service" id="vich_geographical.info_window_builder" />
- <argument type="service" id="vich_geographical.icon_generator" />
- </service>
+ <service id="vich_geographical.icon_generator.default" class="Vich\GeographicalBundle\Map\Marker\Icon\DefaultIconGenerator" public="false" />
</services>
View
20 Resources/config/templating.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <services>
+
+ <service id="vich_geographical.templating.helper.map_helper" class="Vich\GeographicalBundle\Templating\Helper\MapHelper" public="false">
+ <tag name="templating.helper" alias="vich_geographical" />
+ <argument type="service" id="vich_geographical.annotation_driver" />
+ <argument type="service" id="vich_geographical.map_provider" />
+ <argument type="service" id="vich_geographical.map_renderer" />
+ <argument type="service" id="vich_geographical.info_window_builder" />
+ <argument type="service" id="vich_geographical.icon_generator" />
+ </service>
+
+ </services>
+
+</container>
View
2 Resources/config/templating_php.xml
@@ -6,7 +6,7 @@
<services>
- <service id="vich_geographical.info_window_builder" class="Vich\GeographicalBundle\Map\Builder\MapMarkerInfoWindowBuilder" public="false">
+ <service id="vich_geographical.info_window_builder" class="Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindowRenderer" public="false">
<argument type="service" id="templating.engine.php" />
<argument>%vich_geographical.info_window.template_name%</argument>
</service>
View
2 Resources/config/templating_twig.xml
@@ -6,7 +6,7 @@
<services>
- <service id="vich_geographical.info_window_builder" class="Vich\GeographicalBundle\Map\Builder\MapMarkerInfoWindowBuilder" public="false">
+ <service id="vich_geographical.info_window_builder" class="Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindowRenderer" public="false">
<argument type="service" id="twig" />
<argument>%vich_geographical.info_window.template_name%</argument>
</service>
View
4 Resources/config/twig.xml
@@ -6,9 +6,9 @@
<services>
- <service id="vich_geographical.twig.extension" class="Vich\GeographicalBundle\Twig\GeographicalExtension" public="false">
+ <service id="vich_geographical.twig.extension" class="Vich\GeographicalBundle\Twig\Extension\GeographicalExtension" public="false">
<tag name="twig.extension" />
- <argument type="service" id="vich_geographical.map_helper" />
+ <argument type="service" id="vich_geographical.templating.helper.map_helper" />
</service>
</services>
View
39 Templating/Helper/MapHelper.php
@@ -4,11 +4,11 @@
use Vich\GeographicalBundle\Driver\AnnotationDriver;
use Vich\GeographicalBundle\Map\Map;
-use Vich\GeographicalBundle\Map\MapMarker;
-use Vich\GeographicalBundle\Map\MapProvider;
+use Vich\GeographicalBundle\Map\Marker\MapMarker;
+use Vich\GeographicalBundle\Map\Provider\MapProvider;
use Vich\GeographicalBundle\Map\Renderer\MapRendererInterface;
-use Vich\GeographicalBundle\Map\Builder\MapMarkerInfoWindowBuilderInterface;
-use Vich\GeographicalBundle\Map\Generator\MapMarkerIconGeneratorInterface;
+use Vich\GeographicalBundle\Map\Marker\InfoWindow\InfoWindowRendererInterface;
+use Vich\GeographicalBundle\Map\Marker\Icon\IconGeneratorInterface;
use Symfony\Component\Templating\Helper\Helper;
/**
@@ -19,41 +19,42 @@
class MapHelper extends Helper
{
/**
- * @var Vich\GeographicalBundle\Driver\AnnotationDriver $driver
+ * @var AnnotationDriver $driver
*/
private $driver;
/**
- * @var Vich\GeographicalBundle\Map\MapProvider $provider
+ * @var MapProvider $provider
*/
private $provider;
/**
- * @var Vich\GeographicalBundle\Map\MapRendererInterface $renderer
+ * @var MapRendererInterface $renderer
*/
private $renderer;
/**
- * @var Vich\GeographicalBundle\Map\Builder\MapMarkerInfoWindowBuilderInterface $infoWindowBuilder
+ * @var InfoWindowBuilderInterface $infoWindowBuilder
*/
private $infoWindowBuilder;
/**
- * @var Vich\GeographicalBundle\Map\Generator\MapMarkerIconGeneratorInterface $iconGenerator
+ * @var IconGeneratorInterface $iconGenerator
*/
private $iconGenerator;
/**
* Constructs a new instance of MapHelper.
*
- * @param Vich\GeographicalBundle\Driver\AnnotationDriver $driver The annotation driver
- * @param Vich\GeographicalBundle\Map\MapProvider $provider The provider
- * @param Vich\GeographicalBundle\Map\Renderer\MapRendererInterface Th renderer
- * @param Vich\GeographicalBundle\Map\Builder\MapMarkerInfoWindowBuilderInterface The info window builder
+ * @param AnnotationDriver $driver The annotation driver
+ * @param MapProvider $provider The provider
+ * @param MapRendererInterface Th renderer
+ * @param InfoWindowBuilderInterface The info window renderer
+ * @param IconGeneratorInterface $iconGenerator The marker icon url generator
*/
public function __construct(AnnotationDriver $driver, MapProvider $provider,
- MapRendererInterface $renderer, MapMarkerInfoWindowBuilderInterface $infoWindowBuilder,
- MapMarkerIconGeneratorInterface $iconGenerator)
+ MapRendererInterface $renderer, InfoWindowRendererInterface $infoWindowBuilder,
+ IconGeneratorInterface $iconGenerator)
{
$this->driver = $driver;
$this->provider = $provider;
@@ -69,7 +70,7 @@ public function __construct(AnnotationDriver $driver, MapProvider $provider,
*/
public function getName()
{
- return 'vichgeo';
+ return 'vich_geographical';
}
/**
@@ -156,11 +157,11 @@ public function render($alias)
* @param type $obj The object
* @return array An array
*/
- private function getLatLng($obj)
+ protected function getLatLng($obj)
{
- $annot = $this->driver->getGeographicalAnnotation($obj);
+ $annot = $this->driver->readGeoAnnotation($obj);
if (null === $annot) {
- throw new \InvalidArgumentException('Unable to find Geographical annotation');
+ throw new \InvalidArgumentException('Unable to find Geographical annotation.');
}
$latMethod = sprintf('get%s', $annot->getLat());
View
10 Tests/Driver/AnnotationDriverTest.php
@@ -50,7 +50,7 @@ protected function setUp()
}
/**
- * Test the getGeographicalAnnotation method of the AnnotationDriver.
+ * Test the readGeoAnnotation method of the AnnotationDriver.
*/
public function testGetGeographicalAnnotation()
{
@@ -70,17 +70,17 @@ public function testGetGeographicalAnnotation()
->with($this->isInstanceOf('\ReflectionClass'), $this->equalTo('Vich\GeographicalBundle\Annotation\Geographical'))
->will($this->returnValue($this->geographicalAnnotation));
- $annot = $this->driver->getGeographicalAnnotation($this->geographicalEntity);
+ $annot = $this->driver->readGeoAnnotation($this->geographicalEntity);
$this->assertNotNull($annot);
$this->assertEquals('latitude', $annot->getLat());
$this->assertEquals('longitude', $annot->getLng());
}
/**
- * Test the getGeographicalQueryAnnotation method of the AnnotationDriver.
+ * Test the readGeoQueryAnnotation method of the AnnotationDriver.
*/
- public function testGetGeographicalQueryAnnotation()
+ public function testReadGeoQueryAnnotation()
{
$this->geographicalQueryAnnotation
->expects($this->once())
@@ -93,7 +93,7 @@ public function testGetGeographicalQueryAnnotation()
->with($this->isInstanceOf('\ReflectionMethod'), $this->equalTo('Vich\GeographicalBundle\Annotation\GeographicalQuery'))
->will($this->returnValue($this->geographicalQueryAnnotation));
- $annot = $this->driver->getGeographicalQueryAnnotation($this->geographicalEntity);
+ $annot = $this->driver->readGeoQueryAnnotation($this->geographicalEntity);
$this->assertNotNull($annot);
$this->assertEquals('getAddress', $annot->getMethod());
View
219 Tests/Listener/GeographicalListenerTest.php
@@ -0,0 +1,219 @@
+<?php
+
+namespace Vich\GeographicalBundle\Tests\Listener\ORM;
+
+use Vich\GeographicalBundle\Driver\AnnotationDriver;
+use Vich\GeographicalBundle\QueryService\QueryServiceInterface;
+use Vich\GeographicalBundle\Listener\ORM\GeographicalListener;
+use Vich\GeographicalBundle\Tests\DummyGeoEntity;
+
+/**
+ * GeographicalListenerTest.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class GeographicalListenerTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var Vich\GeographicalBundle\Listener\GeographicalListener $listener
+ */
+ protected $listener;
+
+ /**
+ * @var Vich\GeographicalBundle\QueryService\QueryServiceInterface $queryService
+ */
+ protected $queryService;
+
+ /**
+ * @var Vich\GeographicalBundle\QueryService\QueryResult $queryResult
+ */
+ protected $queryResult;
+
+ /**
+ * @var Vich\GeographicalBundle\Driver\AnnotationDriver $driver
+ */
+ protected $driver;
+
+ /**
+ * @var Vich\GeographicalBundle\Tests\DummyGeoEntity $geographicalEntity
+ */
+ protected $geographicalEntity;
+
+ /**
+ * @var Vich\GeographicalBundle\Annotation\Geographical $geographicalAnnotation
+ */
+ protected $geographicalAnnotation;
+
+ /**
+ * @var Vich\GeographicalBundle\Annotation\GeographicalQuery $geographicalQueryAnnotation
+ */
+ protected $geographicalQueryAnnotation;
+
+ protected $lifecycleArgs;
+
+ /**
+ * Sets up the test.
+ */
+ protected function setUp()
+ {
+ $this->driver = $this->getMockDriver();
+ $this->queryService = $this->getMockQueryService();
+ $this->queryResult = $this->getMockQueryResult();
+ $this->listener = $this->getListener($this->driver, $this->queryService);
+ $this->geographicalEntity = $this->getGeographicalEntity();
+ $this->geographicalAnnotation = $this->getMockGeographicalAnnotation();
+ $this->geographicalQueryAnnotation = $this->getMockGeographicalQueryAnnotation();
+ $this->lifecycleArgs = $this->getMockLifecycleArgs();
+ }
+
+ /**
+ * Test the prePersist method of the GeographicalListener.
+ */
+ public function testPrePersist()
+ {
+ $this->lifecycleArgs
+ ->expects($this->once())
+ ->method('getEntity')
+ ->will($this->returnValue($this->geographicalEntity));
+
+ $this->geographicalAnnotation
+ ->expects($this->once())
+ ->method('getLat')
+ ->will($this->returnValue('latitude'));
+
+ $this->geographicalAnnotation
+ ->expects($this->once())
+ ->method('getLng')
+ ->will($this->returnValue('longitude'));
+
+ $this->geographicalQueryAnnotation
+ ->expects($this->once())
+ ->method('getMethod')
+ ->will($this->returnValue('getAddress'));
+
+ $this->queryService
+ ->expects($this->once())
+ ->method('queryForCoordinates')
+ ->with($this->anything())
+ ->will($this->returnValue($this->queryResult));
+
+ $this->queryResult
+ ->expects($this->once())
+ ->method('getLatitude')
+ ->will($this->returnValue(50));
+
+ $this->queryResult
+ ->expects($this->once())
+ ->method('getLongitude')
+ ->will($this->returnValue(-50));
+
+ $this->driver
+ ->expects($this->once())
+ ->method('getGeographicalAnnotation')
+ ->will($this->returnValue($this->geographicalAnnotation));
+
+ $this->driver
+ ->expects($this->once())
+ ->method('getGeographicalQueryAnnotation')
+ ->will($this->returnValue($this->geographicalQueryAnnotation));
+
+ $this->listener->prePersist($this->lifecycleArgs);
+
+ $this->assertEquals(50, $this->geographicalEntity->getLatitude());
+ $this->assertEquals(-50, $this->geographicalEntity->getLongitude());
+ }
+
+ /**
+ * Gets a mock query service.
+ *
+ * @return Vich\GeographicalBundle\QueryService\QueryServiceInterface The query service
+ */
+ public function getMockQueryService()
+ {
+ return $this->getMock('Vich\GeographicalBundle\QueryService\QueryServiceInterface');
+ }
+
+ /**
+ * Gets the mock driver object.
+ *
+ * @return Vich\GeographicalBundle\Driver\AnnotationDriver The driver
+ */
+ private function getMockDriver()
+ {
+ return $this->getMockBuilder('Vich\GeographicalBundle\Driver\AnnotationDriver')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * Gets the mock listener.
+ *
+ * @param Vich\GeographicalBundle\Driver\AnnotationDriver $driver The driver
+ * @param Vich\GeographicalBundle\QueryService\QueryServiceInterface $queryService The query service
+ * @return Vich\GeographicalBundle\Listener\GeographicalListener The listener
+ */
+ private function getListener(AnnotationDriver $driver, QueryServiceInterface $queryService)
+ {
+ $listener = new GeographicalListener();
+ $listener->setAnnotationDriver($driver);
+ $listener->setQueryService($queryService);
+
+ return $listener;
+ }
+
+ /**
+ * Gets a mock query result.
+ *
+ * @return Vich\GeographicalBundle\QueryService\QueryResult The query result
+ */
+ private function getMockQueryResult()
+ {
+ return $this->getMock('Vich\GeographicalBundle\QueryService\QueryResult');
+ }
+
+ /**
+ * Gets a mock query service.
+ *
+ * @return Vich\GeographicalBundle\Tests\DummyGeoEntity The entity
+ */
+ private function getGeographicalEntity()
+ {
+ return new DummyGeoEntity();
+ }
+
+ /**
+ * Gets a mock geographical annotation.
+ *
+ * @return Vich\GeographicalBundle\Annotation\Geographical The annotation
+ */
+ private function getMockGeographicalAnnotation()
+ {
+ return $this->getMockBuilder('Vich\GeographicalBundle\Annotation\Geographical')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * Gets a mock geographical query annotation.
+ *
+ * @return Vich\GeographicalBundle\Annotation\GeographicalQuery The annotation
+ */
+ private function getMockGeographicalQueryAnnotation()
+ {
+ return $this->getMockBuilder('Vich\GeographicalBundle\Annotation\GeographicalQuery')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * Gets the mock lifecycle event args.
+ *
+ * @return Doctrine\ORM\Event\LifecycleEventArgs The event args
+ */
+ private function getMockLifecycleArgs()
+ {
+ return $this->getMockBuilder('Doctrine\ORM\Event\LifecycleEventArgs')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+}
View
107 Twig/Extension/GeographicalExtension.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Vich\GeographicalBundle\Twig\Extension;
+
+use Vich\GeographicalBundle\Templating\Helper\MapHelper;
+
+/**
+ * GeographicalExtension.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class GeographicalExtension extends \Twig_Extension
+{
+ /**
+ * @var MapHelper $helper
+ */
+ private $helper;
+
+ /**
+ * Constructs a new instance of GeographicalExtension.
+ *
+ * @param MapHelper $helper
+ */
+ public function __construct(MapHelper $helper)
+ {
+ $this->helper = $helper;
+ }
+
+ /**
+ * Returns the canonical name of this helper.
+ *
+ * @return string The canonical name
+ */
+ public function getName()
+ {
+ return 'vich_geographical';
+ }
+
+ /**
+ * Returns a list of twig functions.
+ *
+ * @return array An array
+ */
+ public function getFunctions()
+ {
+ $names = array(
+ 'vichgeo_include_js' => 'includeJavascripts',
+ 'vichgeo_include_css' => 'includeStylesheets',
+ 'vichgeo_map' => 'renderMap',
+ 'vichgeo_map_for' => 'renderMapWithEntities'
+ );
+
+ $funcs = array();
+ foreach ($names as $twig => $local) {
+ $funcs[$twig] = new \Twig_Function_Method($this, $local, array('is_safe' => array('html')));
+ }
+
+ return $funcs;
+ }
+
+ /**
+ * Includes the necessary javascripts for the map renderer.
+ *
+ * @return string The html output
+ */
+ public function includeJavascripts()
+ {
+ return $this->helper->renderJavascripts();
+ }
+
+ /**
+ * Includes the necessary stylesheets for the map renderer.
+ *
+ * @return string The html output
+ */
+ public function includeStylesheets()
+ {
+ return $this->helper->renderStylesheets();
+ }
+
+ /**
+ * Renders a Map.
+ *
+ * @param type $alias The Map alias
+ * @return string The html output
+ */
+ public function renderMap($alias)
+ {
+ return $this->helper->render($alias);
+ }
+
+ /**
+ * Renders a Map with the specified entity or array of entities.
+ *
+ * @param string $alias The Map alias
+ * @param object $obj The object
+ * @return string The html output
+ */
+ public function renderMapWithEntities($alias, $obj)
+ {
+ if (empty($obj)) {
+ return $this->renderMap($alias);
+ }
+
+ return $this->helper->prepareAndRender($alias, $obj);
+ }
+}

0 comments on commit 74d5615

Please sign in to comment.
Something went wrong with that request. Please try again.