Skip to content

Commit

Permalink
initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin10 committed Oct 3, 2011
1 parent 9353d6d commit 74d5615
Show file tree
Hide file tree
Showing 29 changed files with 1,253 additions and 73 deletions.
36 changes: 36 additions & 0 deletions Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -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);
}
44 changes: 44 additions & 0 deletions Adapter/ODM/MongoDB/MongoDBAdapter.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions Adapter/ORM/DoctrineORMAdapter.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 10 additions & 7 deletions DependencyInjection/VichGeographicalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"',
Expand All @@ -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']);
Expand Down
47 changes: 33 additions & 14 deletions Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vich\GeographicalBundle\Driver;

use Doctrine\Common\Annotations\Reader;
use Vich\GeographicalBundle\Adapter\AdapterInterface;

/**
* AnnotationDriver.
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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);
}
}
Loading

0 comments on commit 74d5615

Please sign in to comment.