Skip to content
Browse files

initial MongoDB support

  • Loading branch information...
1 parent dcf2838 commit 09f1cb7f5850d4e670ad9e83739e76948754bb1d @dustin10 committed Jul 31, 2011
View
24 DependencyInjection/Compiler/ValidateExtensionConfigurationPass.php
@@ -1,24 +0,0 @@
-<?php
-
-namespace Vich\GeographicalBundle\DependencyInjection\Compiler;
-
-use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
-
-/**
- * ValidateExtensionConfigurationPass.
- *
- * @author Dustin Dobervic <ddobervich@gmail.com>
- */
-class ValidateExtensionConfigurationPass implements CompilerPassInterface
-{
- /**
- * Validate the extension configuration.
- *
- * @param ContainerBuilder $container
- */
- public function process(ContainerBuilder $container)
- {
- $container->getExtension('vich_geographical')->validateConfiguration($container);
- }
-}
View
29 DependencyInjection/Configuration.php
@@ -23,22 +23,7 @@ public function getConfigTree()
$root
->children()
- ->arrayNode('orm')
- ->useAttributeAsKey('id')
- ->prototype('array')
- ->performNoDeepMerging()
- ->children()
- ->scalarNode('enabled')->defaultTrue()->end()
- ->end()
- ->end()
- ->end()
- ->arrayNode('twig')
- ->performNoDeepMerging()
- ->addDefaultsIfNotSet()
- ->children()
- ->scalarNode('enabled')->defaultFalse()->end()
- ->end()
- ->end()
+ ->scalarNode('db_driver')->cannotBeOverwritten()->isRequired()->end()
->arrayNode('class')
->addDefaultsIfNotSet()
->children()
@@ -52,23 +37,23 @@ public function getConfigTree()
->end()
->end()
->end()
- ->arrayNode('leaflet')
+ ->arrayNode('templating')
->addDefaultsIfNotSet()
->children()
- ->scalarNode('api_key')->defaultNull()->end()
+ ->scalarNode('engine')->defaultValue('twig')->end()
+ ->scalarNode('info_window')->defaultValue('VichGeographicalBundle:InfoWindow:default.html.twig')->end()
->end()
->end()
- ->arrayNode('bing')
+ ->arrayNode('leaflet')
->addDefaultsIfNotSet()
->children()
->scalarNode('api_key')->defaultNull()->end()
->end()
->end()
- ->arrayNode('templating')
+ ->arrayNode('bing')
->addDefaultsIfNotSet()
->children()
- ->scalarNode('engine')->defaultValue('twig')->end()
- ->scalarNode('info_window')->defaultValue('VichGeographicalBundle:InfoWindow:default.html.twig')->end()
+ ->scalarNode('api_key')->defaultNull()->end()
->end()
->end()
->end()
View
61 DependencyInjection/VichGeographicalExtension.php
@@ -17,9 +17,12 @@
class VichGeographicalExtension extends Extension
{
/**
- * @var array $entityManagers
+ * @var array $driverMap
*/
- private $entityManagers = array();
+ private $driverMap = array(
+ 'orm' => 'Vich\GeographicalBundle\Listener\ORM\GeographicalListener',
+ 'mongodb' => 'Vich\GeographicalBundle\Listener\ODM\MongoDB\GeographicalListener'
+ );
/**
* Loads the extension.
@@ -36,34 +39,38 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
- $toLoad = array('query.xml', 'services.xml', 'listener.xml');
- foreach ($toLoad as $file) {
- $loader->load($file);
+ $dbDriver = strtolower($config['db_driver']);
+ if (!in_array($dbDriver, array_keys($this->driverMap))) {
+ throw new \InvalidArgumentException(
+ sprintf(
+ 'Invalid "db_driver" configuration option specified: "%s"',
+ $config['db_driver']
+ )
+ );
}
- $container->setParameter('vich_geographical.query_service.class', $config['class']['query_service']);
+ $container->setParameter('vich_geographical.listener.geographical.class', $this->driverMap[$dbDriver]);
- $listenerName = 'vich_geographical.listener.geographical';
- foreach ($config['orm'] as $name => $params) {
- if ($params['enabled']) {
- $definition = $container->getDefinition($listenerName);
- $definition->addTag('doctrine.event_subscriber', array('connection' => $name));
- }
-
- $this->entityManagers[] = $name;
+ $toLoad = array('query.xml', 'map.xml', 'listener.xml', 'twig.xml');
+ foreach ($toLoad as $file) {
+ $loader->load($file);
}
- if ($config['twig']['enabled']) {
- $loader->load('twig.xml');
+ $templateEngine = strtolower($config['templating']['engine']);
+ if (!in_array($templateEngine, array('twig', 'php'))) {
+ throw new \InvalidArgumentException(
+ sprintf(
+ 'The "templating.engine" configuration option specified: "%s"',
+ $templateEngine
+ )
+ );
}
- $templating = sprintf('templating_%s.xml', $config['templating']['engine']);
- $loader->load($templating);
+ $loader->load(sprintf('templating_%s.xml', $templateEngine));
$container->setParameter('vich_geographical.info_window.template_name', $config['templating']['info_window']);
$rendererOptions = array();
-
if (null !== $config['leaflet']['api_key']) {
$rendererOptions['leaflet_api_key'] = $config['leaflet']['api_key'];
}
@@ -72,22 +79,8 @@ public function load(array $configs, ContainerBuilder $container)
$rendererOptions['bing_api_key'] = $config['bing']['api_key'];
}
+ $container->setParameter('vich_geographical.query_service.class', $config['class']['query_service']);
$container->setParameter('vich_geographical.map_renderer.options', $rendererOptions);
-
$container->setParameter('vich_geographical.map_renderer.class', $config['class']['map_renderer']);
}
-
- /**
- * Validates the DBAL configuration.
- *
- * @param ContainerBuilder $container The container builder
- */
- public function validateConfiguration(ContainerBuilder $container)
- {
- foreach ($this->entityManagers as $name) {
- if (!$container->hasDefinition(sprintf('doctrine.dbal.%s_connection', $name))) {
- throw new \InvalidArgumentException(sprintf('Invalid %s config: DBAL connection "%s" not found', $this->getAlias(), $name));
- }
- }
- }
}
View
56 Listener/GeographicalListener.php → Listener/AbstractGeographicalListener.php
@@ -3,47 +3,41 @@
namespace Vich\GeographicalBundle\Listener;
use Doctrine\Common\EventSubscriber;
-use Doctrine\ORM\Event\LifecycleEventArgs;
-use Doctrine\ORM\Event\PreUpdateEventArgs;
use Vich\GeographicalBundle\Driver\AnnotationDriver;
use Vich\GeographicalBundle\QueryService\QueryServiceInterface;
use Vich\GeographicalBundle\Annotation\Geographical;
/**
- * GeographicalListener.
+ * AbstractGeographicalListener.
*
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
-class GeographicalListener implements EventSubscriber
+abstract class AbstractGeographicalListener implements GeographicalListenerInterface
{
/**
* @var QueryServiceInterface $queryService
*/
- private $queryService;
+ protected $queryService;
/**
* @var AnnotationDriver $reader
*/
- private $driver;
+ protected $driver;
/**
- * Sets the query service the listener should use to query for coordinates.
- *
- * @param QueryServiceInterface $queryService The query service
+ * {@inheritDoc}
*/
- public function setQueryService(QueryServiceInterface $queryService)
+ public function setAnnotationDriver(AnnotationDriver $driver)
{
- $this->queryService = $queryService;
+ $this->driver = $driver;
}
/**
- * Constructs a new instance of GeographicalListener.
- *
- * @param Reader $reader The annotation reader
+ * {@inheritDoc}
*/
- public function __construct(AnnotationDriver $driver)
+ public function setQueryService(QueryServiceInterface $queryService)
{
- $this->driver = $driver;
+ $this->queryService = $queryService;
}
/**
@@ -58,38 +52,30 @@ public function getSubscribedEvents()
'preUpdate'
);
}
-
+
/**
- * Checks for persisted object to update coordinates
- *
- * @param LifecycleEventArgs $args The event arguments
+ * Updates the object on pre persist.
+ *
+ * @param object $obj The object
*/
- public function prePersist(LifecycleEventArgs $args)
+ protected function doPrePersist($obj)
{
- $obj = $args->getEntity();
- $reflClass = new \ReflectionClass($obj);
-
$geographical = $this->driver->getGeographicalAnnotation($obj);
-
if ($geographical) {
$geographicalQuery = $this->driver->getGeographicalQueryAnnotation($obj);
if ($geographicalQuery) {
$this->queryCoordinates($obj, $geographical, $geographicalQuery);
}
}
-
}
-
+
/**
- * Update coordinates on objects being updated before update
- * if they require changing
- *
- * @param PreUpdateEventArgs $args The event arguments
+ * Updates the object on pre update.
+ *
+ * @param object $obj The object
*/
- public function preUpdate(PreUpdateEventArgs $args)
+ protected function doPreUpdate($obj)
{
- $obj = $args->getEntity();
-
$geographical = $this->driver->getGeographicalAnnotation($obj);
if ($geographical && $geographical->getOn() === Geographical::ON_UPDATE) {
$geographicalQuery = $this->driver->getGeographicalQueryAnnotation($obj);
@@ -106,7 +92,7 @@ public function preUpdate(PreUpdateEventArgs $args)
* @param Geographical $geographical The greographical annotation
* @param GeographicalQuery $geographicalQuery The geogrphical query annotation
*/
- private function queryCoordinates($entity, $geographical, $geographicalQuery)
+ protected function queryCoordinates($entity, $geographical, $geographicalQuery)
{
$queryMethod = $geographicalQuery->getMethod();
$query = $entity->$queryMethod();
View
29 Listener/GeographicalListenerInterface.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Vich\GeographicalBundle\Listener;
+
+use Doctrine\Common\EventSubscriber;
+use Vich\GeographicalBundle\Driver\AnnotationDriver;
+use Vich\GeographicalBundle\QueryService\QueryServiceInterface;
+
+/**
+ * GeographicalListenerInterface.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+interface GeographicalListenerInterface extends EventSubscriber
+{
+ /**
+ * Sets the annotation driver.
+ *
+ * @param AnnotationDriver $driver The annotation driver
+ */
+ function setAnnotationDriver(AnnotationDriver $driver);
+
+ /**
+ * Sets the query service.
+ *
+ * @param QueryServiceInterface $queryService The query service
+ */
+ function setQueryService(QueryServiceInterface $queryService);
+}
View
40 Listener/ODM/MongoDB/GeographicalListener.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Vich\GeographicalBundle\Listener\ODM\MongoDB;
+
+use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
+use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
+use Vich\GeographicalBundle\Listener\AbstractGeographicalListener;
+
+/**
+ * GeographicalListener.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class GeographicalListener extends AbstractGeographicalListener
+{
+ /**
+ * Checks for persisted object to update coordinates
+ *
+ * @param LifecycleEventArgs $args The event arguments
+ */
+ public function prePersist(LifecycleEventArgs $args)
+ {
+ $obj = $args->getDocument();
+
+ $this->doPrePersist($obj);
+ }
+
+ /**
+ * Update coordinates on objects being updated before update
+ * if they require changing
+ *
+ * @param PreUpdateEventArgs $args The event arguments
+ */
+ public function preUpdate(PreUpdateEventArgs $args)
+ {
+ $obj = $args->getDocument();
+
+ $this->doPreUpdate($obj);
+ }
+}
View
41 Listener/ORM/GeographicalListener.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Vich\GeographicalBundle\Listener\ORM;
+
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+use Vich\GeographicalBundle\Listener\AbstractGeographicalListener;
+
+/**
+ * GeographicalListener.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class GeographicalListener extends AbstractGeographicalListener
+{
+ /**
+ * Checks for persisted object to update coordinates
+ *
+ * @param LifecycleEventArgs $args The event arguments
+ */
+ public function prePersist(LifecycleEventArgs $args)
+ {
+ $obj = $args->getEntity();
+
+ $this->doPrePersist($obj);
+
+ }
+
+ /**
+ * Update coordinates on objects being updated before update
+ * if they require changing
+ *
+ * @param PreUpdateEventArgs $args The event arguments
+ */
+ public function preUpdate(PreUpdateEventArgs $args)
+ {
+ $obj = $args->getEntity();
+
+ $this->doPreUpdate($obj);
+ }
+}
View
9 Resources/config/listener.xml
@@ -3,15 +3,14 @@
<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.listener.geographical.class">Vich\GeographicalBundle\Listener\GeographicalListener</parameter>
- </parameters>
<services>
<service id="vich_geographical.listener.geographical" class="%vich_geographical.listener.geographical.class%" public="false">
- <argument type="service" id="vich_geographical.annotation_driver" />
+ <tag name="doctrine.event_subscriber" />
+ <call method="setAnnotationDriver">
+ <argument type="service" id="vich_geographical.annotation_driver" />
+ </call>
<call method="setQueryService">
<argument type="service" id="vich_geographical.query_service" />
</call>
View
0 Resources/config/services.xml → Resources/config/map.xml
File renamed without changes.
View
7 Tests/Listener/GeographicalListenerTest.php → ...Listener/ORM/GeographicalListenerTest.php
@@ -1,10 +1,10 @@
<?php
-namespace Vich\GeographicalBundle\Tests\Listener;
+namespace Vich\GeographicalBundle\Tests\Listener\ORM;
use Vich\GeographicalBundle\Driver\AnnotationDriver;
use Vich\GeographicalBundle\QueryService\QueryServiceInterface;
-use Vich\GeographicalBundle\Listener\GeographicalListener;
+use Vich\GeographicalBundle\Listener\ORM\GeographicalListener;
use Vich\GeographicalBundle\Tests\DummyGeoEntity;
/**
@@ -154,7 +154,8 @@ private function getMockDriver()
*/
private function getListener(AnnotationDriver $driver, QueryServiceInterface $queryService)
{
- $listener = new GeographicalListener($driver);
+ $listener = new GeographicalListener();
+ $listener->setAnnotationDriver($driver);
$listener->setQueryService($queryService);
return $listener;
View
2 VichGeographicalBundle.php
@@ -4,7 +4,6 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Vich\GeographicalBundle\DependencyInjection\Compiler\ValidateExtensionConfigurationPass;
use Vich\GeographicalBundle\DependencyInjection\Compiler\RegisterMapsPass;
/**
@@ -23,7 +22,6 @@ public function build(ContainerBuilder $container)
{
parent::build($container);
- $container->addCompilerPass(new ValidateExtensionConfigurationPass());
$container->addCompilerPass(new RegisterMapsPass());
}
}

0 comments on commit 09f1cb7

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