Merge branch 'code-qa' into 3.1.x

This commit is contained in:
Tim Nagel 2015-03-13 15:17:52 +11:00
commit dd3269d1ef
16 changed files with 108 additions and 85 deletions

View file

@ -502,7 +502,7 @@ class FOSElasticaExtension extends Extension
break; break;
} }
if ($tagName) { if (null !== $tagName) {
foreach ($this->getDoctrineEvents($typeConfig) as $event) { foreach ($this->getDoctrineEvents($typeConfig) as $event) {
$listenerDef->addTag($tagName, array('event' => $event)); $listenerDef->addTag($tagName, array('event' => $event));
} }
@ -527,7 +527,6 @@ class FOSElasticaExtension extends Extension
break; break;
default: default:
throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver'])); throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver']));
break;
} }
$events = array(); $events = array();

View file

@ -2,20 +2,22 @@
namespace FOS\ElasticaBundle\Doctrine; namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Common\Persistence\ManagerRegistry;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/** /**
* Maps Elastica documents with Doctrine objects * Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between * This mapper assumes an exact match between
* elastica documents ids and doctrine object ids. * elastica documents ids and doctrine object ids.
*/ */
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface abstract class AbstractElasticaToModelTransformer extends BaseTransformer
{ {
/** /**
* Manager registry. * Manager registry.
*
* @var ManagerRegistry
*/ */
protected $registry = null; protected $registry = null;
@ -38,21 +40,14 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
'query_builder_method' => 'createQueryBuilder', 'query_builder_method' => 'createQueryBuilder',
); );
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Instantiates a new Mapper. * Instantiates a new Mapper.
* *
* @param object $registry * @param ManagerRegistry $registry
* @param string $objectClass * @param string $objectClass
* @param array $options * @param array $options
*/ */
public function __construct($registry, $objectClass, array $options = array()) public function __construct(ManagerRegistry $registry, $objectClass, array $options = array())
{ {
$this->registry = $registry; $this->registry = $registry;
$this->objectClass = $objectClass; $this->objectClass = $objectClass;
@ -69,16 +64,6 @@ 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.
@ -111,10 +96,7 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
// 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']; $identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor; usort($objects, $this->getSortingClosure($idPos, $identifier));
usort($objects, function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
});
return $objects; return $objects;
} }
@ -138,7 +120,7 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
} }
/** /**
* {@inheritdoc} * {@inheritDoc}
*/ */
public function getIdentifierField() public function getIdentifierField()
{ {

View file

@ -71,7 +71,7 @@ abstract class AbstractProvider extends BaseAbstractProvider
$objects = $this->getSlice($queryBuilder, $batchSize, $offset, $objects); $objects = $this->getSlice($queryBuilder, $batchSize, $offset, $objects);
$objects = array_filter($objects, array($this, 'isObjectIndexable')); $objects = array_filter($objects, array($this, 'isObjectIndexable'));
if ($objects) { if (!empty($objects)) {
if (!$ignoreErrors) { if (!$ignoreErrors) {
$this->objectPersister->insertMany($objects); $this->objectPersister->insertMany($objects);
} else { } else {

View file

@ -3,8 +3,8 @@
namespace FOS\ElasticaBundle\Doctrine; namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use FOS\ElasticaBundle\Persister\ObjectPersister; use FOS\ElasticaBundle\Persister\ObjectPersister;
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use FOS\ElasticaBundle\Provider\IndexableInterface; use FOS\ElasticaBundle\Provider\IndexableInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccess;
@ -19,14 +19,14 @@ class Listener
/** /**
* Object persister. * Object persister.
* *
* @var ObjectPersister * @var ObjectPersisterInterface
*/ */
protected $objectPersister; protected $objectPersister;
/** /**
* Configuration for the listener. * Configuration for the listener.
* *
* @var string * @var array
*/ */
private $config; private $config;
@ -84,7 +84,7 @@ class Listener
$this->objectPersister = $objectPersister; $this->objectPersister = $objectPersister;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
if ($logger) { if ($logger && $this->objectPersister instanceof ObjectPersister) {
$this->objectPersister->setLogger($logger); $this->objectPersister->setLogger($logger);
} }
} }

View file

@ -6,6 +6,11 @@ use FOS\ElasticaBundle\Elastica\Index;
class IndexManager class IndexManager
{ {
/**
* @var Index
*/
private $defaultIndex;
/** /**
* @var array * @var array
*/ */

View file

@ -38,13 +38,13 @@ class MappingBuilder
} }
$mapping = array(); $mapping = array();
if ($typeMappings) { if (!empty($typeMappings)) {
$mapping['mappings'] = $typeMappings; $mapping['mappings'] = $typeMappings;
} }
// 'warmers' => $indexConfig->getWarmers(), // 'warmers' => $indexConfig->getWarmers(),
$settings = $indexConfig->getSettings(); $settings = $indexConfig->getSettings();
if ($settings) { if (!empty($settings)) {
$mapping['settings'] = $settings; $mapping['settings'] = $settings;
} }
@ -95,7 +95,7 @@ class MappingBuilder
$mapping['_meta']['model'] = $typeConfig->getModel(); $mapping['_meta']['model'] = $typeConfig->getModel();
} }
if (!$mapping) { if (empty($mapping)) {
// Empty mapping, we want it encoded as a {} instead of a [] // Empty mapping, we want it encoded as a {} instead of a []
$mapping = new \stdClass(); $mapping = new \stdClass();
} }

View file

@ -10,6 +10,15 @@ namespace FOS\ElasticaBundle\Persister;
*/ */
interface ObjectPersisterInterface interface ObjectPersisterInterface
{ {
/**
* Checks if this persister can handle the given object or not.
*
* @param mixed $object
*
* @return boolean
*/
public function handlesObject($object);
/** /**
* Insert one object into the type * Insert one object into the type
* The object will be transformed to an elastica document. * The object will be transformed to an elastica document.
@ -66,13 +75,4 @@ interface ObjectPersisterInterface
* @param array $identifiers array of domain model object identifiers * @param array $identifiers array of domain model object identifiers
*/ */
public function deleteManyByIdentifiers(array $identifiers); public function deleteManyByIdentifiers(array $identifiers);
/**
* If the object persister handles the given object.
*
* @param object $object
*
* @return bool
*/
public function handlesObject($object);
} }

View file

@ -18,11 +18,15 @@ class ObjectSerializerPersister extends ObjectPersister
protected $serializer; protected $serializer;
/** /**
* @param Type $type
* @param ModelToElasticaTransformerInterface $transformer
* @param string $objectClass * @param string $objectClass
* @param callable $serializer
*/ */
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer) public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer)
{ {
parent::__construct($type, $transformer, $objectClass, array()); parent::__construct($type, $transformer, $objectClass, array());
$this->serializer = $serializer; $this->serializer = $serializer;
} }

View file

@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle\Propel; namespace FOS\ElasticaBundle\Propel;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@ -14,7 +15,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
* *
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
*/ */
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
{ {
/** /**
* Propel model class to map to Elastica documents. * Propel model class to map to Elastica documents.
@ -33,13 +34,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
'identifier' => 'id', 'identifier' => 'id',
); );
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Constructor. * Constructor.
* *
@ -52,16 +46,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/** /**
* Transforms an array of Elastica document into an array of Propel entities * Transforms an array of Elastica document into an array of Propel entities
* fetched from the database. * fetched from the database.
@ -82,11 +66,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
// Sort objects in the order of their IDs // Sort objects in the order of their IDs
$idPos = array_flip($ids); $idPos = array_flip($ids);
$identifier = $this->options['identifier']; $identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor; $sortCallback = $this->getSortingClosure($idPos, $identifier);
$sortCallback = function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
};
if (is_object($objects)) { if (is_object($objects)) {
$objects->uasort($sortCallback); $objects->uasort($sortCallback);
@ -105,7 +85,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$objects = $this->transform($elasticaObjects); $objects = $this->transform($elasticaObjects);
$result = array(); $result = array();
for ($i = 0; $i < count($elasticaObjects); $i++) { for ($i = 0, $j = count($elasticaObjects); $i < $j; $i++) {
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]); $result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
} }

View file

@ -23,23 +23,19 @@ class Callback
{ {
$this->groups = $groups; $this->groups = $groups;
if ($this->groups) { if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) {
if (!$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".'); throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
} }
} }
}
public function setVersion($version) public function setVersion($version)
{ {
$this->version = $version; $this->version = $version;
if ($this->version) { if ($this->version && !$this->serializer instanceof SerializerInterface) {
if (!$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".'); throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
} }
} }
}
public function serialize($object) public function serialize($object)
{ {

View file

@ -58,7 +58,6 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase
->with('index', 'type', $this->anything()) ->with('index', 'type', $this->anything())
->will($this->returnValue(true)); ->will($this->returnValue(true));
$providerInvocationOffset = 2;
$previousSlice = array(); $previousSlice = array();
foreach ($objectsByIteration as $i => $objects) { foreach ($objectsByIteration as $i => $objects) {

View file

@ -13,12 +13,12 @@ namespace FOS\ElasticaBundle\Tests\Functional\app\ORM;
class IndexableService class IndexableService
{ {
public function isIndexable($object) public function isIndexable()
{ {
return true; return true;
} }
public static function isntIndexable($object) public static function isntIndexable()
{ {
return false; return false;
} }

View file

@ -37,7 +37,7 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa
$this->collection = new ElasticaToModelTransformerCollection($this->transformers = array( $this->collection = new ElasticaToModelTransformerCollection($this->transformers = array(
'type1' => $transformer1, 'type1' => $transformer1,
'type2' => $transformer2, 'type2' => $transformer2,
), array()); ));
} }
public function testGetObjectClass() public function testGetObjectClass()

View file

@ -0,0 +1,51 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Transformer;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
{
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* Returns a sorting closure to be used with usort() to put retrieved objects
* back in the order that they were returned by ElasticSearch.
*
* @param array $idPos
* @param string $identifierPath
* @return callable
*/
protected function getSortingClosure(array $idPos, $identifierPath)
{
$propertyAccessor = $this->propertyAccessor;
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifierPath)] > $idPos[$propertyAccessor->getValue($b, $identifierPath)];
};
}
}

View file

@ -81,7 +81,7 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
$objects = $this->transform($elasticaObjects); $objects = $this->transform($elasticaObjects);
$result = array(); $result = array();
for ($i = 0; $i < count($elasticaObjects); $i++) { for ($i = 0, $j = count($elasticaObjects); $i < $j; $i++) {
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]); $result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
} }

View file

@ -3,10 +3,17 @@
namespace FOS\ElasticaBundle\Transformer; namespace FOS\ElasticaBundle\Transformer;
/** /**
* Maps Elastica documents with model objects. * Indicates that the model should have elastica highlights injected.
*/ */
interface HighlightableModelInterface interface HighlightableModelInterface
{ {
/**
* Returns a unique identifier for the model.
*
* @return mixed
*/
public function getId();
/** /**
* Set ElasticSearch highlight data. * Set ElasticSearch highlight data.
* *