dev
This commit is contained in:
parent
15d3f1e4f8
commit
7d06cc429b
33
DependencyInjection/Compiler/LookupPass.php
Normal file
33
DependencyInjection/Compiler/LookupPass.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* Registers any lookup services into the LookupManager
|
||||
*
|
||||
* @author Tim Nagel <tim@nagel.com.au>
|
||||
*/
|
||||
class LookupPass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('fos_elastica.lookup_manager')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$lookups = array();
|
||||
foreach ($container->findTaggedServiceIds('fos_elastica.lookup') as $id => $tags) {
|
||||
$lookups[] = new Reference($id);
|
||||
}
|
||||
|
||||
$managerDefinition = $container->getDefinition('fos_elastica.lookup_manager');
|
||||
$managerDefinition->setArguments(0, $lookups);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ namespace FOS\ElasticaBundle\DependencyInjection;
|
|||
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
|
@ -14,6 +13,13 @@ use InvalidArgumentException;
|
|||
|
||||
class FOSElasticaExtension extends Extension
|
||||
{
|
||||
/**
|
||||
* Stores references to all defined clients loaded by the extension.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $clients = array();
|
||||
|
||||
protected $indexConfigs = array();
|
||||
protected $typeFields = array();
|
||||
protected $loadedDrivers = array();
|
||||
|
@ -21,9 +27,7 @@ class FOSElasticaExtension extends Extension
|
|||
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$configuration = $this->getConfiguration($configs, $container);
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
$config = $this->processConfiguration(new Configuration($configs, $container), $configs);
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
|
||||
if (empty($config['clients']) || empty($config['indexes'])) {
|
||||
|
@ -43,25 +47,19 @@ class FOSElasticaExtension extends Extension
|
|||
$config['default_index'] = reset($keys);
|
||||
}
|
||||
|
||||
$clientIdsByName = $this->loadClients($config['clients'], $container);
|
||||
$this->serializerConfig = isset($config['serializer']) ? $config['serializer'] : null;
|
||||
$indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']);
|
||||
$indexRefsByName = array_map(function($id) {
|
||||
return new Reference($id);
|
||||
}, $indexIdsByName);
|
||||
|
||||
$this->loadIndexManager($indexRefsByName, $container);
|
||||
$this->loadResetter($this->indexConfigs, $container);
|
||||
|
||||
$this->loadClients($config['clients'], $container);
|
||||
$container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client']));
|
||||
|
||||
// XX serializer can be done better....
|
||||
// $this->serializerConfig = isset($config['serializer']) ? $config['serializer'] : null;
|
||||
|
||||
$this->loadIndexes($config['indexes'], $container);
|
||||
$container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index']));
|
||||
|
||||
$this->createDefaultManagerAlias($config['default_manager'], $container);
|
||||
}
|
||||
$this->loadIndexManager($container);
|
||||
$this->loadResetter($container);
|
||||
|
||||
public function getConfiguration(array $config, ContainerBuilder $container)
|
||||
{
|
||||
return new Configuration($config);
|
||||
$this->createDefaultManagerAlias($config['default_manager'], $container);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,22 +71,24 @@ class FOSElasticaExtension extends Extension
|
|||
*/
|
||||
protected function loadClients(array $clients, ContainerBuilder $container)
|
||||
{
|
||||
$clientIds = array();
|
||||
foreach ($clients as $name => $clientConfig) {
|
||||
$clientId = sprintf('fos_elastica.client.%s', $name);
|
||||
$clientDef = new Definition('%fos_elastica.client.class%', array($clientConfig));
|
||||
|
||||
$clientDef = new DefinitionDecorator('fos_elastica.client_prototype');
|
||||
$clientDef->replaceArgument(0, $clientConfig);
|
||||
|
||||
$logger = $clientConfig['servers'][0]['logger'];
|
||||
if (false !== $logger) {
|
||||
$clientDef->addMethodCall('setLogger', array(new Reference($logger)));
|
||||
}
|
||||
$clientDef->addTag('fos_elastica.client');
|
||||
|
||||
$container->setDefinition($clientId, $clientDef);
|
||||
|
||||
$clientIds[$name] = $clientId;
|
||||
$this->clients[$name] = array(
|
||||
'id' => $clientId,
|
||||
'reference' => new Reference($clientId)
|
||||
);
|
||||
}
|
||||
|
||||
return $clientIds;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,35 +96,29 @@ class FOSElasticaExtension extends Extension
|
|||
*
|
||||
* @param array $indexes An array of indexes configurations
|
||||
* @param ContainerBuilder $container A ContainerBuilder instance
|
||||
* @param array $clientIdsByName
|
||||
* @param $defaultClientName
|
||||
* @param $serializerConfig
|
||||
* @throws \InvalidArgumentException
|
||||
* @return array
|
||||
*/
|
||||
protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName)
|
||||
protected function loadIndexes(array $indexes, ContainerBuilder $container)
|
||||
{
|
||||
$indexIds = array();
|
||||
|
||||
foreach ($indexes as $name => $index) {
|
||||
if (isset($index['client'])) {
|
||||
$clientName = $index['client'];
|
||||
if (!isset($clientIdsByName[$clientName])) {
|
||||
throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName));
|
||||
}
|
||||
} else {
|
||||
$clientName = $defaultClientName;
|
||||
$indexId = sprintf('fos_elastica.index.%s', $name);
|
||||
$indexName = $index['index_name'] ?: $name;
|
||||
|
||||
$indexDef = new DefinitionDecorator('fos_elastica.index_prototype');
|
||||
$indexDef->replaceArgument(0, $indexName);
|
||||
|
||||
if ($index['client']) {
|
||||
$client = $this->getClient($index['client']);
|
||||
$indexDef->setFactoryService($client);
|
||||
}
|
||||
|
||||
$clientId = $clientIdsByName[$clientName];
|
||||
$indexId = sprintf('fos_elastica.index.%s', $name);
|
||||
$indexName = isset($index['index_name']) ? $index['index_name'] : $name;
|
||||
$indexDefArgs = array($indexName);
|
||||
$indexDef = new Definition('%fos_elastica.index.class%', $indexDefArgs);
|
||||
$indexDef->setFactoryService($clientId);
|
||||
$indexDef->setFactoryMethod('getIndex');
|
||||
$container->setDefinition($indexId, $indexDef);
|
||||
|
||||
$typePrototypeConfig = isset($index['type_prototype']) ? $index['type_prototype'] : array();
|
||||
$indexIds[$name] = $indexId;
|
||||
|
||||
$this->indexConfigs[$name] = array(
|
||||
'index' => new Reference($indexId),
|
||||
'name_or_alias' => $indexName,
|
||||
|
@ -132,10 +126,13 @@ class FOSElasticaExtension extends Extension
|
|||
'mappings' => array()
|
||||
)
|
||||
);
|
||||
|
||||
if ($index['finder']) {
|
||||
// XX Deprecated
|
||||
$this->loadIndexFinder($container, $name, $indexId);
|
||||
}
|
||||
if (!empty($index['settings'])) {
|
||||
// XX What is this for?
|
||||
$this->indexConfigs[$name]['config']['settings'] = $index['settings'];
|
||||
}
|
||||
if ($index['use_alias']) {
|
||||
|
@ -522,26 +519,26 @@ class FOSElasticaExtension extends Extension
|
|||
/**
|
||||
* Loads the index manager
|
||||
*
|
||||
* @param array $indexRefsByName
|
||||
* @param ContainerBuilder $container
|
||||
**/
|
||||
protected function loadIndexManager(array $indexRefsByName, ContainerBuilder $container)
|
||||
*/
|
||||
protected function loadIndexManager(ContainerBuilder $container)
|
||||
{
|
||||
$indexRefs = array_map(function ($index) { return $index['index']; }, $this->indexConfigs);
|
||||
|
||||
$managerDef = $container->getDefinition('fos_elastica.index_manager');
|
||||
$managerDef->replaceArgument(0, $indexRefsByName);
|
||||
$managerDef->replaceArgument(0, $indexRefs);
|
||||
$managerDef->replaceArgument(1, new Reference('fos_elastica.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the resetter
|
||||
*
|
||||
* @param array $indexConfigs
|
||||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
|
||||
* @param ContainerBuilder $container
|
||||
*/
|
||||
protected function loadResetter(array $indexConfigs, ContainerBuilder $container)
|
||||
protected function loadResetter(ContainerBuilder $container)
|
||||
{
|
||||
$resetterDef = $container->getDefinition('fos_elastica.resetter');
|
||||
$resetterDef->replaceArgument(0, $indexConfigs);
|
||||
$resetterDef->replaceArgument(0, $this->indexConfigs);
|
||||
}
|
||||
|
||||
protected function loadDriver(ContainerBuilder $container, $driver)
|
||||
|
@ -549,6 +546,7 @@ class FOSElasticaExtension extends Extension
|
|||
if (in_array($driver, $this->loadedDrivers)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load($driver.'.xml');
|
||||
$this->loadedDrivers[] = $driver;
|
||||
|
@ -570,4 +568,20 @@ class FOSElasticaExtension extends Extension
|
|||
|
||||
$container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reference to a client.
|
||||
*
|
||||
* @param string $clientName
|
||||
* @return Reference
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function getClient($clientName)
|
||||
{
|
||||
if (!array_key_exists($clientName, $this->clients)) {
|
||||
throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName));
|
||||
}
|
||||
|
||||
return $this->clients[$clientName]['reference'];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace FOS\ElasticaBundle\Doctrine\MongoDB;
|
||||
|
||||
use FOS\ElasticaBundle\Doctrine\AbstractLookup;
|
||||
use FOS\ElasticaBundle\Type\TypeConfigurationInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfiguration;
|
||||
|
||||
class Lookup extends AbstractLookup
|
||||
{
|
||||
|
@ -20,11 +20,11 @@ class Lookup extends AbstractLookup
|
|||
/**
|
||||
* Look up objects of a specific type with ids as supplied.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
public function lookup(TypeConfigurationInterface $configuration, array $ids)
|
||||
public function lookup(TypeConfiguration $configuration, array $ids)
|
||||
{
|
||||
$qb = $this->createQueryBuilder($configuration);
|
||||
$qb->hydrate($configuration->isHydrate());
|
||||
|
@ -36,10 +36,10 @@ class Lookup extends AbstractLookup
|
|||
}
|
||||
|
||||
/**
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @return \Doctrine\ODM\MongoDB\Query\Builder
|
||||
*/
|
||||
private function createQueryBuilder(TypeConfigurationInterface $configuration)
|
||||
private function createQueryBuilder(TypeConfiguration $configuration)
|
||||
{
|
||||
$method = $configuration->getRepositoryMethod();
|
||||
$manager = $this->registry->getManagerForClass($configuration->getModelClass());
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace FOS\ElasticaBundle\Doctrine\ORM;
|
|||
|
||||
use Doctrine\ORM\Query;
|
||||
use FOS\ElasticaBundle\Doctrine\AbstractLookup;
|
||||
use FOS\ElasticaBundle\Type\TypeConfigurationInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfiguration;
|
||||
|
||||
class Lookup extends AbstractLookup
|
||||
{
|
||||
|
@ -23,11 +23,11 @@ class Lookup extends AbstractLookup
|
|||
/**
|
||||
* Look up objects of a specific type with ids as supplied.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
public function lookup(TypeConfigurationInterface $configuration, array $ids)
|
||||
public function lookup(TypeConfiguration $configuration, array $ids)
|
||||
{
|
||||
$hydrationMode = $configuration->isHydrate() ?
|
||||
Query::HYDRATE_OBJECT :
|
||||
|
@ -45,10 +45,10 @@ class Lookup extends AbstractLookup
|
|||
}
|
||||
|
||||
/**
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @return \Doctrine\ORM\QueryBuilder
|
||||
*/
|
||||
private function createQueryBuilder(TypeConfigurationInterface $configuration)
|
||||
private function createQueryBuilder(TypeConfiguration $configuration)
|
||||
{
|
||||
$repository = $this->registry->getRepository($configuration->getModelClass());
|
||||
$method = $configuration->getRepositoryMethod();
|
||||
|
|
|
@ -8,6 +8,6 @@ class UnexpectedObjectException extends \Exception
|
|||
{
|
||||
public function __construct($id)
|
||||
{
|
||||
parent::__construct(sprintf('Lookup returned an unknown object with id %d', $id));
|
||||
parent::__construct(sprintf('Lookup returned an unexpected object with id %d', $id));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Finder;
|
||||
|
||||
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use Elastica\Query;
|
||||
|
||||
interface PaginatedFinderInterface extends FinderInterface
|
||||
{
|
||||
/**
|
||||
* Searches for query results and returns them wrapped in a paginator
|
||||
*
|
||||
* @param mixed $query Can be a string, an array or an \Elastica\Query object
|
||||
* @param array $options
|
||||
* @return Pagerfanta paginated results
|
||||
*/
|
||||
function findPaginated($query, $options = array());
|
||||
|
||||
/**
|
||||
* Creates a paginator adapter for this query
|
||||
*
|
||||
* @param mixed $query
|
||||
* @param array $options
|
||||
* @return PaginatorAdapterInterface
|
||||
*/
|
||||
function createPaginatorAdapter($query, $options = array());
|
||||
}
|
|
@ -13,7 +13,7 @@ use Elastica\Query;
|
|||
/**
|
||||
* Finds elastica documents and map them to persisted objects
|
||||
*/
|
||||
class TransformedFinder implements PaginatedFinderInterface
|
||||
class TransformedFinder implements FinderInterface
|
||||
{
|
||||
protected $searchable;
|
||||
protected $transformer;
|
||||
|
@ -31,7 +31,7 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
* @param integer $limit
|
||||
* @param array $options
|
||||
* @return array of model objects
|
||||
**/
|
||||
*/
|
||||
public function find($query, $limit = null, $options = array())
|
||||
{
|
||||
$results = $this->search($query, $limit, $options);
|
||||
|
@ -78,29 +78,4 @@ class TransformedFinder implements PaginatedFinderInterface
|
|||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a paginator wrapping the result of a search
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $options
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findPaginated($query, $options = array())
|
||||
{
|
||||
$queryObject = Query::create($query);
|
||||
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
|
||||
|
||||
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createPaginatorAdapter($query, $options = array())
|
||||
{
|
||||
$query = Query::create($query);
|
||||
|
||||
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,20 +2,25 @@
|
|||
|
||||
namespace FOS\ElasticaBundle\Index;
|
||||
|
||||
use Elastica\Index;
|
||||
use FOS\ElasticaBundle\Elastica\TransformingIndex;
|
||||
|
||||
class IndexManager
|
||||
{
|
||||
/**
|
||||
* @var TransformingIndex[]
|
||||
*/
|
||||
protected $indexesByName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultIndexName;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $indexesByName
|
||||
* @param Index $defaultIndex
|
||||
* @param TransformingIndex[] $indexesByName
|
||||
* @param TransformingIndex $defaultIndex
|
||||
*/
|
||||
public function __construct(array $indexesByName, Index $defaultIndex)
|
||||
public function __construct(array $indexesByName, TransformingIndex $defaultIndex)
|
||||
{
|
||||
$this->indexesByName = $indexesByName;
|
||||
$this->defaultIndexName = $defaultIndex->getName();
|
||||
|
@ -24,7 +29,7 @@ class IndexManager
|
|||
/**
|
||||
* Gets all registered indexes
|
||||
*
|
||||
* @return array
|
||||
* @return TransformingIndex[]
|
||||
*/
|
||||
public function getAllIndexes()
|
||||
{
|
||||
|
@ -35,7 +40,7 @@ class IndexManager
|
|||
* Gets an index by its name
|
||||
*
|
||||
* @param string $name Index to return, or the default index if null
|
||||
* @return Index
|
||||
* @return TransformingIndex
|
||||
* @throws \InvalidArgumentException if no index exists for the given name
|
||||
*/
|
||||
public function getIndex($name = null)
|
||||
|
@ -54,7 +59,7 @@ class IndexManager
|
|||
/**
|
||||
* Gets the default index
|
||||
*
|
||||
* @return Index
|
||||
* @return TransformingIndex
|
||||
*/
|
||||
public function getDefaultIndex()
|
||||
{
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
use Pagerfanta\Adapter\AdapterInterface;
|
||||
|
||||
class FantaPaginatorAdapter implements AdapterInterface
|
||||
{
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @param \FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface $adapter
|
||||
*/
|
||||
public function __construct(PaginatorAdapterInterface $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of results.
|
||||
*
|
||||
* @return integer The number of results.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getNbResults()
|
||||
{
|
||||
return $this->adapter->getTotalHits();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Facets
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getFacets()
|
||||
{
|
||||
return $this->adapter->getFacets();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a slice of the results.
|
||||
*
|
||||
* @param integer $offset The offset.
|
||||
* @param integer $length The length.
|
||||
*
|
||||
* @return array|\Traversable The slice.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getSlice($offset, $length)
|
||||
{
|
||||
return $this->adapter->getResults($offset, $length)->toArray();
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
interface PaginatorAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Returns the number of results.
|
||||
*
|
||||
* @return integer The number of results.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getTotalHits();
|
||||
|
||||
/**
|
||||
* Returns an slice of the results.
|
||||
*
|
||||
* @param integer $offset The offset.
|
||||
* @param integer $length The length.
|
||||
*
|
||||
* @return PartialResultsInterface
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getResults($offset, $length);
|
||||
|
||||
/**
|
||||
* Returns Facets
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function getFacets();
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
interface PartialResultsInterface
|
||||
{
|
||||
/**
|
||||
* Returns the paginated results.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function toArray();
|
||||
|
||||
/**
|
||||
* Returns the number of results.
|
||||
*
|
||||
* @return integer The number of results.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getTotalHits();
|
||||
|
||||
/**
|
||||
* Returns the facets
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getFacets();
|
||||
}
|
|
@ -1,139 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
use Elastica\SearchableInterface;
|
||||
use Elastica\Query;
|
||||
use Elastica\ResultSet;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Allows pagination of Elastica\Query. Does not map results
|
||||
*/
|
||||
class RawPaginatorAdapter implements PaginatorAdapterInterface
|
||||
{
|
||||
/**
|
||||
* @var SearchableInterface the object to search in
|
||||
*/
|
||||
private $searchable;
|
||||
|
||||
/**
|
||||
* @var Query the query to search
|
||||
*/
|
||||
private $query;
|
||||
|
||||
/**
|
||||
* @var array search options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var integer the number of hits
|
||||
*/
|
||||
private $totalHits;
|
||||
|
||||
/**
|
||||
* @var array for the facets
|
||||
*/
|
||||
private $facets;
|
||||
|
||||
/**
|
||||
* @see PaginatorAdapterInterface::__construct
|
||||
*
|
||||
* @param SearchableInterface $searchable the object to search in
|
||||
* @param Query $query the query to search
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(SearchableInterface $searchable, Query $query, array $options = array())
|
||||
{
|
||||
$this->searchable = $searchable;
|
||||
$this->query = $query;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the paginated results.
|
||||
*
|
||||
* @param $offset
|
||||
* @param $itemCountPerPage
|
||||
* @throws \InvalidArgumentException
|
||||
* @return ResultSet
|
||||
*/
|
||||
protected function getElasticaResults($offset, $itemCountPerPage)
|
||||
{
|
||||
$offset = (integer) $offset;
|
||||
$itemCountPerPage = (integer) $itemCountPerPage;
|
||||
$size = $this->query->hasParam('size')
|
||||
? (integer) $this->query->getParam('size')
|
||||
: null;
|
||||
|
||||
if ($size && $size < $offset + $itemCountPerPage) {
|
||||
$itemCountPerPage = $size - $offset;
|
||||
}
|
||||
|
||||
if ($itemCountPerPage < 1) {
|
||||
throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
|
||||
}
|
||||
|
||||
$query = clone $this->query;
|
||||
$query->setFrom($offset);
|
||||
$query->setSize($itemCountPerPage);
|
||||
|
||||
$resultSet = $this->searchable->search($query, $this->options);
|
||||
$this->totalHits = $resultSet->getTotalHits();
|
||||
$this->facets = $resultSet->getFacets();
|
||||
return $resultSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the paginated results.
|
||||
*
|
||||
* @param int $offset
|
||||
* @param int $itemCountPerPage
|
||||
* @return PartialResultsInterface
|
||||
*/
|
||||
public function getResults($offset, $itemCountPerPage)
|
||||
{
|
||||
return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of results.
|
||||
*
|
||||
* @return integer The number of results.
|
||||
*/
|
||||
public function getTotalHits()
|
||||
{
|
||||
if ( ! isset($this->totalHits)) {
|
||||
$this->totalHits = $this->searchable->search($this->query)->getTotalHits();
|
||||
}
|
||||
|
||||
return $this->query->hasParam('size')
|
||||
? min($this->totalHits, (integer) $this->query->getParam('size'))
|
||||
: $this->totalHits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Facets
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFacets()
|
||||
{
|
||||
if ( ! isset($this->facets)) {
|
||||
$this->facets = $this->searchable->search($this->query)->getFacets();
|
||||
}
|
||||
|
||||
return $this->facets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Query
|
||||
*
|
||||
* @return Query the search query
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
use Elastica\ResultSet;
|
||||
use Elastica\Result;
|
||||
|
||||
/**
|
||||
* Raw partial results transforms to a simple array
|
||||
*/
|
||||
class RawPartialResults implements PartialResultsInterface
|
||||
{
|
||||
protected $resultSet;
|
||||
|
||||
/**
|
||||
* @param ResultSet $resultSet
|
||||
*/
|
||||
public function __construct(ResultSet $resultSet)
|
||||
{
|
||||
$this->resultSet = $resultSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return array_map(function(Result $result) {
|
||||
return $result->getSource();
|
||||
}, $this->resultSet->getResults());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getTotalHits()
|
||||
{
|
||||
return $this->resultSet->getTotalHits();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getFacets()
|
||||
{
|
||||
if ($this->resultSet->hasFacets()) {
|
||||
return $this->resultSet->getFacets();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
||||
use Elastica\SearchableInterface;
|
||||
use Elastica\Query;
|
||||
|
||||
/**
|
||||
* Allows pagination of \Elastica\Query
|
||||
*/
|
||||
class TransformedPaginatorAdapter extends RawPaginatorAdapter
|
||||
{
|
||||
private $transformer;
|
||||
|
||||
/**
|
||||
* @param SearchableInterface $searchable the object to search in
|
||||
* @param Query $query the query to search
|
||||
* @param array $options
|
||||
* @param ElasticaToModelTransformerInterface $transformer the transformer for fetching the results
|
||||
*/
|
||||
public function __construct(SearchableInterface $searchable, Query $query, array $options = array(), ElasticaToModelTransformerInterface $transformer)
|
||||
{
|
||||
parent::__construct($searchable, $query, $options);
|
||||
|
||||
$this->transformer = $transformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getResults($offset, $length)
|
||||
{
|
||||
return new TransformedPartialResults($this->getElasticaResults($offset, $length), $this->transformer);
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Paginator;
|
||||
|
||||
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
||||
use Elastica\ResultSet;
|
||||
|
||||
/**
|
||||
* Partial transformed result set
|
||||
*/
|
||||
class TransformedPartialResults extends RawPartialResults
|
||||
{
|
||||
protected $transformer;
|
||||
|
||||
/**
|
||||
* @param ResultSet $resultSet
|
||||
* @param \FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface $transformer
|
||||
*/
|
||||
public function __construct(ResultSet $resultSet, ElasticaToModelTransformerInterface $transformer)
|
||||
{
|
||||
parent::__construct($resultSet);
|
||||
|
||||
$this->transformer = $transformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->transformer->transform($this->resultSet->getResults());
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ namespace FOS\ElasticaBundle\Propel;
|
|||
|
||||
use Doctrine\Common\Util\Inflector;
|
||||
use FOS\ElasticaBundle\Type\LookupInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfigurationInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfiguration;
|
||||
|
||||
class Lookup implements LookupInterface
|
||||
{
|
||||
|
@ -21,11 +21,11 @@ class Lookup implements LookupInterface
|
|||
/**
|
||||
* Look up objects of a specific type with ids as supplied.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param int[] $ids
|
||||
* @return object[]
|
||||
*/
|
||||
public function lookup(TypeConfigurationInterface $configuration, array $ids)
|
||||
public function lookup(TypeConfiguration $configuration, array $ids)
|
||||
{
|
||||
$query = $this->createQuery($configuration, $ids);
|
||||
|
||||
|
@ -39,11 +39,11 @@ class Lookup implements LookupInterface
|
|||
/**
|
||||
* Create a query to use in the findByIdentifiers() method.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param array $ids
|
||||
* @return \ModelCriteria
|
||||
*/
|
||||
protected function createQuery(TypeConfigurationInterface $configuration, array $ids)
|
||||
protected function createQuery(TypeConfiguration $configuration, array $ids)
|
||||
{
|
||||
$queryClass = $configuration->getModelClass() . 'Query';
|
||||
$query = $queryClass::create();
|
||||
|
|
|
@ -6,96 +6,29 @@
|
|||
|
||||
<parameters>
|
||||
<parameter key="fos_elastica.client.class">FOS\ElasticaBundle\Elastica\LoggingClient</parameter>
|
||||
<parameter key="fos_elastica.index.class">FOS\ElasticaBundle\Elastica\TransformingIndex</parameter>
|
||||
<parameter key="fos_elastica.type.class">Elastica\Type</parameter>
|
||||
<parameter key="fos_elastica.index_manager.class">FOS\ElasticaBundle\IndexManager</parameter>
|
||||
<parameter key="fos_elastica.resetter.class">FOS\ElasticaBundle\Resetter</parameter>
|
||||
<parameter key="fos_elastica.finder.class">FOS\ElasticaBundle\Finder\TransformedFinder</parameter>
|
||||
<parameter key="fos_elastica.paginator_subscriber.class">FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber</parameter>
|
||||
<parameter key="fos_elastica.logger.class">FOS\ElasticaBundle\Logger\ElasticaLogger</parameter>
|
||||
<parameter key="fos_elastica.data_collector.class">FOS\ElasticaBundle\DataCollector\ElasticaDataCollector</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.provider_registry.class">FOS\ElasticaBundle\Provider\ProviderRegistry</parameter>
|
||||
<parameter key="fos_elastica.property_accessor.class">Symfony\Component\PropertyAccess\PropertyAccessor</parameter>
|
||||
<parameter key="fos_elastica.object_persister.class">FOS\ElasticaBundle\Persister\ObjectPersister</parameter>
|
||||
<parameter key="fos_elastica.object_serializer_persister.class">FOS\ElasticaBundle\Persister\ObjectSerializerPersister</parameter>
|
||||
<parameter key="fos_elastica.model_to_elastica_transformer.class">FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer</parameter>
|
||||
<parameter key="fos_elastica.model_to_elastica_identifier_transformer.class">FOS\ElasticaBundle\Transformer\ModelToElasticaIdentifierTransformer</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.client_prototype" class="%fos_elastica.client.class%" abstract="true">
|
||||
<argument type="collection" /> <!-- configuration -->
|
||||
<argument /> <!-- callback -->
|
||||
<argument type="service" id="fos_elastica.transformer.combined" />
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.logger" class="%fos_elastica.logger.class%">
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
<argument>%kernel.debug%</argument>
|
||||
<tag name="monolog.logger" channel="elastica" />
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.data_collector" class="%fos_elastica.data_collector.class%" public="true">
|
||||
<tag name="data_collector" template="FOSElasticaBundle:Collector:elastica" id="elastica" />
|
||||
<argument type="service" id="fos_elastica.logger" />
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.index_manager" class="%fos_elastica.index_manager.class%">
|
||||
<argument /> <!-- indexes -->
|
||||
<argument /> <!-- default index -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.resetter" class="%fos_elastica.resetter.class%">
|
||||
<argument /> <!-- index configs -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.object_persister" class="%fos_elastica.object_persister.class%" abstract="true">
|
||||
<argument /> <!-- type -->
|
||||
<argument /> <!-- model to elastica transformer -->
|
||||
<argument /> <!-- model -->
|
||||
<argument /> <!-- properties mapping -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.finder" class="%fos_elastica.finder.class%" public="true" abstract="true">
|
||||
<argument /> <!-- searchable -->
|
||||
<argument /> <!-- transformer -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.object_serializer_persister" class="%fos_elastica.object_serializer_persister.class%" abstract="true">
|
||||
<argument /> <!-- type -->
|
||||
<argument /> <!-- model to elastica transformer -->
|
||||
<argument /> <!-- model -->
|
||||
<argument /> <!-- serializer -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.model_to_elastica_transformer" class="%fos_elastica.model_to_elastica_transformer.class%" public="false" abstract="true">
|
||||
<argument /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.model_to_elastica_identifier_transformer" class="%fos_elastica.model_to_elastica_identifier_transformer.class%" public="false" abstract="true">
|
||||
<argument /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.elastica_to_model_transformer.collection" class="%fos_elastica.elastica_to_model_transformer.collection.class%" public="true" abstract="true">
|
||||
<argument type="collection" /> <!-- transformers -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.provider_registry" class="%fos_elastica.provider_registry.class%">
|
||||
<call method="setContainer">
|
||||
<argument type="service" id="service_container" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.paginator.subscriber" class="%fos_elastica.paginator_subscriber.class%">
|
||||
<call method="setRequest">
|
||||
<argument type="service" id="request" on-invalid="null" strict="false" />
|
||||
</call>
|
||||
<tag name="knp_paginator.subscriber" />
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.property_accessor" class="%fos_elastica.property_accessor.class%" />
|
||||
</services>
|
||||
|
||||
</container>
|
||||
|
|
35
Resources/config/index.xml
Normal file
35
Resources/config/index.xml
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<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="fos_elastica.index.class">FOS\ElasticaBundle\Elastica\TransformingIndex</parameter>
|
||||
<parameter key="fos_elastica.type.class">Elastica\Type</parameter>
|
||||
<parameter key="fos_elastica.index_manager.class">FOS\ElasticaBundle\IndexManager</parameter>
|
||||
<parameter key="fos_elastica.resetter.class">FOS\ElasticaBundle\Resetter</parameter>
|
||||
<parameter key="fos_elastica.finder.class">FOS\ElasticaBundle\Finder\TransformedFinder</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.index_prototype" factory-service="fos_elastica.client" factory-method="getIndex" abstract="true">
|
||||
<argument /> <!-- index name -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.index_manager" class="%fos_elastica.index_manager.class%">
|
||||
<argument /> <!-- indexes -->
|
||||
<argument /> <!-- default index -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.resetter" class="%fos_elastica.resetter.class%">
|
||||
<argument /> <!-- index configs -->
|
||||
</service>
|
||||
|
||||
<!-- Abstract definition for all finders. -->
|
||||
<service id="fos_elastica.finder" class="%fos_elastica.finder.class%" public="true" abstract="true">
|
||||
<argument /> <!-- searchable -->
|
||||
<argument /> <!-- transformer -->
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
|
@ -31,8 +31,8 @@
|
|||
</service>
|
||||
|
||||
<service id="fos_elastica.manager.orm" class="FOS\ElasticaBundle\Doctrine\RepositoryManager">
|
||||
<argument type="service" id="doctrine"/>
|
||||
<argument type="service" id="annotation_reader"/>
|
||||
<argument type="service" id="doctrine" />
|
||||
<argument type="service" id="annotation_reader" />
|
||||
</service>
|
||||
|
||||
</services>
|
||||
|
|
27
Resources/config/persister.xml
Normal file
27
Resources/config/persister.xml
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<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="fos_elastica.object_persister.class">FOS\ElasticaBundle\Persister\ObjectPersister</parameter>
|
||||
<parameter key="fos_elastica.object_serializer_persister.class">FOS\ElasticaBundle\Persister\ObjectSerializerPersister</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.object_persister" class="%fos_elastica.object_persister.class%" abstract="true">
|
||||
<argument /> <!-- type -->
|
||||
<argument /> <!-- model to elastica transformer -->
|
||||
<argument /> <!-- model -->
|
||||
<argument /> <!-- properties mapping -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.object_serializer_persister" class="%fos_elastica.object_serializer_persister.class%" abstract="true">
|
||||
<argument /> <!-- type -->
|
||||
<argument /> <!-- model to elastica transformer -->
|
||||
<argument /> <!-- model -->
|
||||
<argument /> <!-- serializer -->
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
18
Resources/config/provider.xml
Normal file
18
Resources/config/provider.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<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="fos_elastica.provider_registry.class">FOS\ElasticaBundle\Provider\ProviderRegistry</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.provider_registry" class="%fos_elastica.provider_registry.class%">
|
||||
<call method="setContainer">
|
||||
<argument type="service" id="service_container" />
|
||||
</call>
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
32
Resources/config/transformer.xml
Normal file
32
Resources/config/transformer.xml
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<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="fos_elastica.elastica_to_model_transformer.collection.class">FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection</parameter>
|
||||
<parameter key="fos_elastica.model_to_elastica_transformer.class">FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer</parameter>
|
||||
<parameter key="fos_elastica.model_to_elastica_identifier_transformer.class">FOS\ElasticaBundle\Transformer\ModelToElasticaIdentifierTransformer</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.model_to_elastica_transformer" class="%fos_elastica.model_to_elastica_transformer.class%" public="false" abstract="true">
|
||||
<argument /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.model_to_elastica_identifier_transformer" class="%fos_elastica.model_to_elastica_identifier_transformer.class%" public="false" abstract="true">
|
||||
<argument /> <!-- options -->
|
||||
<call method="setPropertyAccessor">
|
||||
<argument type="service" id="fos_elastica.property_accessor" />
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.elastica_to_model_transformer.collection" class="%fos_elastica.elastica_to_model_transformer.collection.class%" public="true" abstract="true">
|
||||
<argument type="collection" /> <!-- transformers -->
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
|
@ -12,7 +12,7 @@ use FOS\ElasticaBundle\Elastica\TransformingResult;
|
|||
class CombinedResultTransformer
|
||||
{
|
||||
/**
|
||||
* @var \FOS\ElasticaBundle\Type\TypeConfigurationInterface
|
||||
* @var \FOS\ElasticaBundle\Type\TypeConfiguration
|
||||
*/
|
||||
private $configurations;
|
||||
|
||||
|
@ -22,7 +22,7 @@ class CombinedResultTransformer
|
|||
private $transformer;
|
||||
|
||||
/**
|
||||
* @param \FOS\ElasticaBundle\Type\TypeConfigurationInterface[] $configurations
|
||||
* @param \FOS\ElasticaBundle\Type\TypeConfiguration[] $configurations
|
||||
* @param ResultTransformerInterface $transformer
|
||||
*/
|
||||
public function __construct(array $configurations, ResultTransformerInterface $transformer)
|
||||
|
@ -54,7 +54,7 @@ class CombinedResultTransformer
|
|||
* Retrieves the transformer for a given type.
|
||||
*
|
||||
* @param string $type
|
||||
* @return \FOS\ElasticaBundle\Type\TypeConfigurationInterface
|
||||
* @return \FOS\ElasticaBundle\Type\TypeConfiguration
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function getConfiguration($type)
|
||||
|
|
|
@ -5,8 +5,7 @@ namespace FOS\ElasticaBundle\Transformer;
|
|||
use FOS\ElasticaBundle\Exception\MissingModelException;
|
||||
use FOS\ElasticaBundle\Exception\UnexpectedObjectException;
|
||||
use FOS\ElasticaBundle\Type\LookupManager;
|
||||
use FOS\ElasticaBundle\Type\TypeConfigurationInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfiguration;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
|
||||
/**
|
||||
|
@ -35,12 +34,12 @@ class ResultTransformer implements ResultTransformerInterface
|
|||
/**
|
||||
* Transforms Elastica results into Models.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param \FOS\ElasticaBundle\Elastica\TransformingResult[] $results
|
||||
* @throws \FOS\ElasticaBundle\Exception\MissingModelException
|
||||
* @throws \FOS\ElasticaBundle\Exception\UnexpectedObjectException
|
||||
*/
|
||||
public function transform(TypeConfigurationInterface $configuration, $results)
|
||||
public function transform(TypeConfiguration $configuration, $results)
|
||||
{
|
||||
$results = $this->processResults($results);
|
||||
$lookup = $this->lookupManager->getLookup($configuration->getType());
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace FOS\ElasticaBundle\Transformer;
|
||||
|
||||
use FOS\ElasticaBundle\Type\TypeConfigurationInterface;
|
||||
use FOS\ElasticaBundle\Type\TypeConfiguration;
|
||||
|
||||
interface ResultTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms Elastica results into Models.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param array $results
|
||||
*/
|
||||
public function transform(TypeConfigurationInterface $configuration, $results);
|
||||
public function transform(TypeConfiguration $configuration, $results);
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ interface LookupInterface
|
|||
/**
|
||||
* Look up objects of a specific type with ids as supplied.
|
||||
*
|
||||
* @param TypeConfigurationInterface $configuration
|
||||
* @param TypeConfiguration $configuration
|
||||
* @param int[] $ids
|
||||
* @return object[]
|
||||
*/
|
||||
public function lookup(TypeConfigurationInterface $configuration, array $ids);
|
||||
public function lookup(TypeConfiguration $configuration, array $ids);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ class LookupManager
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return LookupInterface
|
||||
|
|
102
Type/TypeConfiguration.php
Normal file
102
Type/TypeConfiguration.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Type;
|
||||
|
||||
/**
|
||||
* A data object that contains configuration information about a specific type.
|
||||
*
|
||||
* @author Tim Nagel <tim@nagel.com.au>
|
||||
*/
|
||||
final class TypeConfiguration
|
||||
{
|
||||
/**
|
||||
* The identifier property that is used to retrieve an identifier from the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $identifierProperty;
|
||||
|
||||
/**
|
||||
* Returns the fully qualified class for the model that this type represents.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $modelClass;
|
||||
|
||||
/**
|
||||
* Returns the repository method that will create a query builder or associated
|
||||
* query object for lookup purposes.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $repositoryMethod;
|
||||
|
||||
/**
|
||||
* Returns the name of the type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* If the lookup should hydrate models to objects or leave data as an array.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $hydrate = true;
|
||||
|
||||
/**
|
||||
* If the type should ignore missing results from a lookup.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $ignoreMissing = false;
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isHydrate()
|
||||
{
|
||||
return $this->hydrate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIdentifierProperty()
|
||||
{
|
||||
return $this->identifierProperty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isIgnoreMissing()
|
||||
{
|
||||
return $this->ignoreMissing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModelClass()
|
||||
{
|
||||
return $this->modelClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRepositoryMethod()
|
||||
{
|
||||
return $this->repositoryMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOS\ElasticaBundle\Type;
|
||||
|
||||
/**
|
||||
* A data object that contains configuration information about a specific type.
|
||||
*
|
||||
* @author Tim Nagel <tim@nagel.com.au>
|
||||
*/
|
||||
interface TypeConfigurationInterface
|
||||
{
|
||||
/**
|
||||
* The identifier property that is used to retrieve an identifier from the model.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIdentifierProperty();
|
||||
|
||||
/**
|
||||
* Returns the fully qualified class for the model that this type represents.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModelClass();
|
||||
|
||||
/**
|
||||
* Returns the repository method that will create a query builder or associated
|
||||
* query object for lookup purposes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRepositoryMethod();
|
||||
|
||||
/**
|
||||
* Returns the name of the type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType();
|
||||
|
||||
/**
|
||||
* If the lookup should hydrate models to objects or leave data as an array.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isHydrate();
|
||||
|
||||
/**
|
||||
* If the type should ignore missing results from a lookup.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isIgnoreMissing();
|
||||
}
|
Loading…
Reference in a new issue