-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto register doctrine services #303
Open
pierredup
wants to merge
4
commits into
geocoder-php:master
Choose a base branch
from
pierredup:doctrine-service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
131c7ca
Auto register doctrine services, and support multiple metadata drivers
pierredup 7dade60
Fix CS
pierredup ef2e36d
Add missing property and constructor for Geocodeable
pierredup af0bdec
Fix service config to register new listener class
pierredup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the BazingaGeocoderBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\GeocoderBundle\Doctrine\ORM; | ||
|
||
use Bazinga\GeocoderBundle\Mapping\ClassMetadata; | ||
use Bazinga\GeocoderBundle\Mapping\Driver\DriverInterface; | ||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\ORM\UnitOfWork; | ||
use Geocoder\Query\GeocodeQuery; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
/** | ||
* @author Markus Bachmann <[email protected]> | ||
* @author Pierre du Plessis <[email protected]> | ||
*/ | ||
class GeocodeEntityListener implements EventSubscriber | ||
{ | ||
/** | ||
* @var DriverInterface | ||
*/ | ||
private $driver; | ||
|
||
/** | ||
* @var ServiceLocator | ||
*/ | ||
private $providerLocator; | ||
|
||
public function __construct(ServiceLocator $providerLocator, DriverInterface $driver) | ||
{ | ||
$this->driver = $driver; | ||
$this->providerLocator = $providerLocator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::onFlush, | ||
]; | ||
} | ||
|
||
public function onFlush(OnFlushEventArgs $args) | ||
{ | ||
$em = $args->getEntityManager(); | ||
$uow = $em->getUnitOfWork(); | ||
|
||
foreach ($uow->getScheduledEntityInsertions() as $entity) { | ||
if (!$this->driver->isGeocodeable($entity)) { | ||
continue; | ||
} | ||
|
||
/** @var ClassMetadata $metadata */ | ||
$metadata = $this->driver->loadMetadataFromObject($entity); | ||
|
||
$this->geocodeEntity($metadata, $entity); | ||
|
||
$uow->recomputeSingleEntityChangeSet( | ||
$em->getClassMetadata(get_class($entity)), | ||
$entity | ||
); | ||
} | ||
|
||
foreach ($uow->getScheduledEntityUpdates() as $entity) { | ||
if (!$this->driver->isGeocodeable($entity)) { | ||
continue; | ||
} | ||
|
||
/** @var ClassMetadata $metadata */ | ||
$metadata = $this->driver->loadMetadataFromObject($entity); | ||
|
||
if (!$this->shouldGeocode($metadata, $uow, $entity)) { | ||
continue; | ||
} | ||
|
||
$this->geocodeEntity($metadata, $entity); | ||
|
||
$uow->recomputeSingleEntityChangeSet( | ||
$em->getClassMetadata(get_class($entity)), | ||
$entity | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param object $entity | ||
*/ | ||
private function geocodeEntity(ClassMetadata $metadata, $entity) | ||
{ | ||
if (null !== $metadata->addressGetter) { | ||
$address = $metadata->addressGetter->invoke($entity); | ||
} else { | ||
$address = $metadata->addressProperty->getValue($entity); | ||
} | ||
|
||
if (empty($address)) { | ||
return; | ||
} | ||
|
||
$serviceId = sprintf('bazinga_geocoder.provider.%s', $metadata->provider); | ||
|
||
if (!$this->providerLocator->has($serviceId)) { | ||
throw new \RuntimeException(sprintf('The provider "%s" is invalid for object "%s".', $metadata->provider, get_class($entity))); | ||
} | ||
|
||
$results = $this->providerLocator->get($serviceId)->geocodeQuery(GeocodeQuery::create($address)); | ||
|
||
if (!$results->isEmpty()) { | ||
$result = $results->first(); | ||
$metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude()); | ||
$metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude()); | ||
} | ||
} | ||
|
||
/** | ||
* @param object $entity | ||
*/ | ||
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, $entity): bool | ||
{ | ||
if (null !== $metadata->addressGetter) { | ||
return true; | ||
} | ||
|
||
$changeSet = $unitOfWork->getEntityChangeSet($entity); | ||
|
||
return isset($changeSet[$metadata->addressProperty->getName()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,123 +12,25 @@ | |
|
||
namespace Bazinga\GeocoderBundle\Doctrine\ORM; | ||
|
||
use Bazinga\GeocoderBundle\Mapping\ClassMetadata; | ||
use Bazinga\GeocoderBundle\Mapping\Driver\DriverInterface; | ||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\ORM\UnitOfWork; | ||
use Geocoder\Provider\Provider; | ||
use Geocoder\Query\GeocodeQuery; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
/** | ||
* @author Markus Bachmann <[email protected]> | ||
*/ | ||
class GeocoderListener implements EventSubscriber | ||
class GeocoderListener extends GeocodeEntityListener | ||
{ | ||
/** | ||
* @var DriverInterface | ||
*/ | ||
private $driver; | ||
|
||
/** | ||
* @var Provider | ||
*/ | ||
private $geocoder; | ||
|
||
public function __construct(Provider $geocoder, DriverInterface $driver) | ||
{ | ||
$this->driver = $driver; | ||
$this->geocoder = $geocoder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::onFlush, | ||
]; | ||
} | ||
|
||
public function onFlush(OnFlushEventArgs $args) | ||
{ | ||
$em = $args->getEntityManager(); | ||
$uow = $em->getUnitOfWork(); | ||
|
||
foreach ($uow->getScheduledEntityInsertions() as $entity) { | ||
if (!$this->driver->isGeocodeable($entity)) { | ||
continue; | ||
} | ||
|
||
/** @var ClassMetadata $metadata */ | ||
$metadata = $this->driver->loadMetadataFromObject($entity); | ||
|
||
$this->geocodeEntity($metadata, $entity); | ||
|
||
$uow->recomputeSingleEntityChangeSet( | ||
$em->getClassMetadata(get_class($entity)), | ||
$entity | ||
); | ||
} | ||
|
||
foreach ($uow->getScheduledEntityUpdates() as $entity) { | ||
if (!$this->driver->isGeocodeable($entity)) { | ||
continue; | ||
} | ||
|
||
/** @var ClassMetadata $metadata */ | ||
$metadata = $this->driver->loadMetadataFromObject($entity); | ||
|
||
if (!$this->shouldGeocode($metadata, $uow, $entity)) { | ||
continue; | ||
} | ||
|
||
$this->geocodeEntity($metadata, $entity); | ||
|
||
$uow->recomputeSingleEntityChangeSet( | ||
$em->getClassMetadata(get_class($entity)), | ||
$entity | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param object $entity | ||
*/ | ||
private function geocodeEntity(ClassMetadata $metadata, $entity) | ||
{ | ||
if (null !== $metadata->addressGetter) { | ||
$address = $metadata->addressGetter->invoke($entity); | ||
} else { | ||
$address = $metadata->addressProperty->getValue($entity); | ||
} | ||
|
||
if (empty($address)) { | ||
return; | ||
} | ||
|
||
$results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address)); | ||
|
||
if (!$results->isEmpty()) { | ||
$result = $results->first(); | ||
$metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude()); | ||
$metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude()); | ||
} | ||
} | ||
|
||
/** | ||
* @param object $entity | ||
*/ | ||
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, $entity): bool | ||
{ | ||
if (null !== $metadata->addressGetter) { | ||
return true; | ||
} | ||
@trigger_error(sprintf('The class "%s" is deprecated and will be removed from a future version. Please remove it from your service definition.', self::class)); | ||
|
||
$changeSet = $unitOfWork->getEntityChangeSet($entity); | ||
$locator = new ServiceLocator([ | ||
'bazinga_geocoder.provider.' => function () use ($geocoder) { | ||
return $geocoder; | ||
}, | ||
]); | ||
|
||
return isset($changeSet[$metadata->addressProperty->getName()]); | ||
parent::__construct($locator, $driver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the BazingaGeocoderBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\GeocoderBundle\Mapping\Driver; | ||
|
||
use Bazinga\GeocoderBundle\Mapping\Exception\MappingException; | ||
|
||
/** | ||
* @author Pierre du Plessis <[email protected]> | ||
*/ | ||
class ChainDriver implements DriverInterface | ||
{ | ||
private $drivers; | ||
|
||
public function __construct(iterable $drivers) | ||
{ | ||
$this->drivers = $drivers; | ||
} | ||
|
||
public function isGeocodeable($object): bool | ||
{ | ||
foreach ($this->drivers as $driver) { | ||
if ($driver->isGeocodeable($object)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function loadMetadataFromObject($object) | ||
{ | ||
foreach ($this->drivers as $driver) { | ||
try { | ||
return $driver->loadMetadataFromObject($object); | ||
} catch (MappingException $exception) { | ||
continue; | ||
} | ||
} | ||
|
||
throw new MappingException(sprintf('The class %s is not geocodeable', get_class($object))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.