Require PropertyAccess component

This commit is contained in:
Karel Souffriau 2013-04-04 15:44:42 +02:00
parent 55920a3397
commit 83e27ede93
10 changed files with 71 additions and 56 deletions

View file

@ -5,7 +5,7 @@ namespace FOS\ElasticaBundle\Doctrine;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
use Symfony\Component\Form\Util\PropertyPath; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/** /**
* Maps Elastica documents with Doctrine objects * Maps Elastica documents with Doctrine objects
@ -33,9 +33,16 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
*/ */
protected $options = array( protected $options = array(
'hydrate' => true, 'hydrate' => true,
'identifier' => 'id' 'identifier' => 'id'
); );
/**
* PropertyAccessor instance
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Instantiates a new Mapper * Instantiates a new Mapper
* *
@ -60,6 +67,16 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
return $this->objectClass; return $this->objectClass;
} }
/**
* Set the PropertyAccessor
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/** /**
* Transforms an array of elastica objects into an array of * Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository * model objects fetched from the doctrine repository
@ -87,13 +104,13 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
} }
} }
$identifierProperty = new PropertyPath($this->options['identifier']);
// sort objects in the order of ids // sort objects in the order of ids
$idPos = array_flip($ids); $idPos = array_flip($ids);
usort($objects, function($a, $b) use ($idPos, $identifierProperty) $identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor;
usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor)
{ {
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
}); });
return $objects; return $objects;

View file

@ -4,7 +4,7 @@ namespace FOS\ElasticaBundle\Propel;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Symfony\Component\Form\Util\PropertyPath; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/** /**
* Maps Elastica documents with Propel objects * Maps Elastica documents with Propel objects
@ -32,6 +32,13 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
'identifier' => 'id' 'identifier' => 'id'
); );
/**
* PropertyAccessor instance
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Instantiates a new Mapper * Instantiates a new Mapper
* *
@ -44,6 +51,16 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
/**
* Set the PropertyAccessor
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/** /**
* Transforms an array of elastica objects into an array of * Transforms an array of elastica objects into an array of
* model objects fetched from the propel repository * model objects fetched from the propel repository
@ -59,17 +76,17 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']); $objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
$identifierProperty = new PropertyPath($this->options['identifier']);
// sort objects in the order of ids // sort objects in the order of ids
$idPos = array_flip($ids); $idPos = array_flip($ids);
$identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor;
if (is_object($objects)) { if (is_object($objects)) {
$objects->uasort(function($a, $b) use ($idPos, $identifierProperty) { $objects->uasort(function($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
}); });
} else { } else {
usort($objects, function($a, $b) use ($idPos, $identifierProperty) { usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$identifierProperty->getValue($a)] > $idPos[$identifierProperty->getValue($b)]; return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
}); });
} }

View file

@ -13,6 +13,7 @@
<parameter key="fos_elastica.manager.class">FOS\ElasticaBundle\Manager\RepositoryManager</parameter> <parameter key="fos_elastica.manager.class">FOS\ElasticaBundle\Manager\RepositoryManager</parameter>
<parameter key="fos_elastica.elastica_to_model_transformer.collection.class">FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection</parameter> <parameter key="fos_elastica.elastica_to_model_transformer.collection.class">FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection</parameter>
<parameter key="fos_elastica.provider_registry.class">FOS\ElasticaBundle\Provider\ProviderRegistry</parameter> <parameter key="fos_elastica.provider_registry.class">FOS\ElasticaBundle\Provider\ProviderRegistry</parameter>
<parameter key="fos_elastica.property_accessor.class">Symfony\Component\PropertyAccess\PropertyAccessor</parameter>
</parameters> </parameters>
<services> <services>
@ -58,7 +59,7 @@
<service id="fos_elastica.model_to_elastica_transformer.prototype.auto" class="FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer" public="false" abstract="true"> <service id="fos_elastica.model_to_elastica_transformer.prototype.auto" class="FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer" public="false" abstract="true">
<argument /> <!-- options --> <argument /> <!-- options -->
<call method="setPropertyAccessor"> <call method="setPropertyAccessor">
<argument type="service" id="property_accessor" on-invalid="null" /> <argument type="service" id="fos_elastica.property_accessor" />
</call> </call>
</service> </service>
@ -75,6 +76,9 @@
<service id="fos_elastica.paginator.subscriber" class="FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber"> <service id="fos_elastica.paginator.subscriber" class="FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber">
<tag name="knp_paginator.subscriber" /> <tag name="knp_paginator.subscriber" />
</service> </service>
<service id="fos_elastica.property_accessor" class="%fos_elastica.property_accessor.class%">
</service>
</services> </services>
</container> </container>

View file

@ -24,6 +24,9 @@
<argument type="service" id="doctrine_mongodb" /> <argument type="service" id="doctrine_mongodb" />
<argument /> <!-- model --> <argument /> <!-- model -->
<argument type="collection" /> <!-- options --> <argument type="collection" /> <!-- options -->
<call method="setPropertyAccessor">
<argument type="service" id="fos_elastica.property_accessor" />
</call>
</service> </service>
<service id="fos_elastica.manager.mongodb" class="FOS\ElasticaBundle\Doctrine\RepositoryManager"> <service id="fos_elastica.manager.mongodb" class="FOS\ElasticaBundle\Doctrine\RepositoryManager">

View file

@ -25,6 +25,9 @@
<argument type="service" id="doctrine" /> <argument type="service" id="doctrine" />
<argument /> <!-- model --> <argument /> <!-- model -->
<argument type="collection" /> <!-- options --> <argument type="collection" /> <!-- options -->
<call method="setPropertyAccessor">
<argument type="service" id="fos_elastica.property_accessor" />
</call>
</service> </service>
<service id="fos_elastica.manager.orm" class="FOS\ElasticaBundle\Doctrine\RepositoryManager"> <service id="fos_elastica.manager.orm" class="FOS\ElasticaBundle\Doctrine\RepositoryManager">

View file

@ -14,6 +14,9 @@
<service id="fos_elastica.elastica_to_model_transformer.prototype.propel" class="FOS\ElasticaBundle\Propel\ElasticaToModelTransformer" public="false"> <service id="fos_elastica.elastica_to_model_transformer.prototype.propel" class="FOS\ElasticaBundle\Propel\ElasticaToModelTransformer" public="false">
<argument /> <!-- model --> <argument /> <!-- model -->
<argument type="collection" /> <!-- options --> <argument type="collection" /> <!-- options -->
<call method="setPropertyAccessor">
<argument type="service" id="fos_elastica.property_accessor" />
</call>
</service> </service>
<service id="fos_elastica.manager.propel" class="%fos_elastica.manager.class%"> <service id="fos_elastica.manager.propel" class="%fos_elastica.manager.class%">

View file

@ -213,10 +213,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase
private function getTransformer() private function getTransformer()
{ {
$transformer = new ModelToElasticaAutoTransformer(); $transformer = new ModelToElasticaAutoTransformer();
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
}
return $transformer; return $transformer;
} }

View file

@ -203,12 +203,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
public function testThatCannotTransformObjectWhenGetterDoesNotExistForPrivateMethod() public function testThatCannotTransformObjectWhenGetterDoesNotExistForPrivateMethod()
{ {
// Support both Symfony 2.1 (Form component) and 2.2 (PropertyAccess component) $this->setExpectedException('Symfony\Component\PropertyAccess\Exception\PropertyAccessDeniedException');
$expectedException = class_exists('Symfony\Component\PropertyAccess\PropertyAccess')
? 'Symfony\Component\PropertyAccess\Exception\PropertyAccessDeniedException'
: 'Symfony\Component\Form\Exception\PropertyAccessDeniedException';
$this->setExpectedException($expectedException);
$transformer = $this->getTransformer(); $transformer = $this->getTransformer();
$transformer->transform(new POPO(), array('desc' => array())); $transformer->transform(new POPO(), array('desc' => array()));
@ -302,10 +297,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
private function getTransformer() private function getTransformer()
{ {
$transformer = new ModelToElasticaAutoTransformer(); $transformer = new ModelToElasticaAutoTransformer();
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
}
return $transformer; return $transformer;
} }

View file

@ -2,7 +2,6 @@
namespace FOS\ElasticaBundle\Transformer; namespace FOS\ElasticaBundle\Transformer;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/** /**
@ -22,11 +21,11 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
); );
/** /**
* PropertyAccessor instance (will be used if available) * PropertyAccessor instance
* *
* @var PropertyAccessorInterface * @var PropertyAccessorInterface
*/ */
private $propertyAccessor; protected $propertyAccessor;
/** /**
* Instanciates a new Mapper * Instanciates a new Mapper
@ -43,7 +42,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
* *
* @param PropertyAccessorInterface $propertyAccessor * @param PropertyAccessorInterface $propertyAccessor
*/ */
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor = null) public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{ {
$this->propertyAccessor = $propertyAccessor; $this->propertyAccessor = $propertyAccessor;
} }
@ -58,17 +57,17 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
**/ **/
public function transform($object, array $fields) public function transform($object, array $fields)
{ {
$identifier = $this->getPropertyValue($object, $this->options['identifier']); $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']);
$document = new \Elastica_Document($identifier); $document = new \Elastica_Document($identifier);
foreach ($fields as $key => $mapping) { foreach ($fields as $key => $mapping) {
$value = $this->getPropertyValue($object, $key); $value = $this->propertyAccessor->getValue($object, $key);
if (isset($mapping['_parent']['identifier'])) { if (isset($mapping['_parent']['identifier'])) {
/* $value is the parent. Read its identifier and set that as the /* $value is the parent. Read its identifier and set that as the
* document's parent. * document's parent.
*/ */
$document->setParent($this->getPropertyValue($value, $mapping['_parent']['identifier'])); $document->setParent($this->propertyAccessor->getValue($value, $mapping['_parent']['identifier']));
continue; continue;
} }
@ -96,26 +95,6 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
return $document; return $document;
} }
/**
* Get the value of an object property.
*
* This method will use Symfony 2.2's PropertyAccessor if it is available.
*
* @param object $object
* @param string $property
* @return mixed
*/
protected function getPropertyValue($object, $property)
{
if (isset($this->propertyAccessor)) {
return $this->propertyAccessor->getValue($object, $property);
}
$propertyPath = new PropertyPath($property);
return $propertyPath->getValue($object);
}
/** /**
* transform a nested document or an object property into an array of ElasticaDocument * transform a nested document or an object property into an array of ElasticaDocument
* *

View file

@ -15,6 +15,7 @@
"symfony/framework-bundle": ">=2.1.0,<2.3.0-dev", "symfony/framework-bundle": ">=2.1.0,<2.3.0-dev",
"symfony/console": ">=2.1.0,<2.3.0-dev", "symfony/console": ">=2.1.0,<2.3.0-dev",
"symfony/form": ">=2.1.0,<2.3.0-dev", "symfony/form": ">=2.1.0,<2.3.0-dev",
"symfony/property-access": "2.2.*",
"ruflin/elastica": "0.19.8" "ruflin/elastica": "0.19.8"
}, },
"require-dev":{ "require-dev":{
@ -25,7 +26,6 @@
"knplabs/knp-components": "1.2.*" "knplabs/knp-components": "1.2.*"
}, },
"suggest": { "suggest": {
"symfony/property-access": "2.2.*",
"doctrine/orm": ">=2.2,<2.5-dev", "doctrine/orm": ">=2.2,<2.5-dev",
"doctrine/mongodb-odm": "1.0.*@dev", "doctrine/mongodb-odm": "1.0.*@dev",
"propel/propel1": "1.6.*", "propel/propel1": "1.6.*",