From aafb6e53fb8f99e37f4113027189cbb7abd4b949 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 22 Apr 2013 13:20:10 +0200 Subject: [PATCH] made the bundle compatible with Elastica 0.20.x --- Client.php | 6 +-- Command/SearchCommand.php | 12 ++--- DependencyInjection/Configuration.php | 3 +- DependencyInjection/FOSElasticaExtension.php | 3 ++ Finder/FinderInterface.php | 2 +- Finder/PaginatedFinderInterface.php | 4 +- Finder/TransformedFinder.php | 14 +++--- HybridResult.php | 4 +- IndexManager.php | 10 +++-- Paginator/RawPaginatorAdapter.php | 20 ++++----- Paginator/RawPartialResults.php | 10 ++--- Paginator/TransformedPaginatorAdapter.php | 12 ++--- Paginator/TransformedPartialResults.php | 6 +-- Persister/ObjectPersister.php | 8 ++-- Propel/ElasticaToModelTransformer.php | 5 ++- README.md | 44 ++++++++++--------- Resetter.php | 6 ++- Resources/config/config.xml | 6 +-- Serializer/Callback.php | 32 +++++++++++--- Tests/IndexManagerTest.php | 4 +- Tests/Persister/ObjectPersisterTest.php | 34 +++++++------- Tests/ResetterTest.php | 13 +++--- ...asticaToModelTransformerCollectionTest.php | 9 ++-- .../ModelToElasticaAutoTransformerTest.php | 6 +-- .../ElasticaToModelTransformerCollection.php | 4 +- .../ModelToElasticaAutoTransformer.php | 5 ++- .../ModelToElasticaTransformerInterface.php | 2 +- composer.json | 2 +- 28 files changed, 161 insertions(+), 125 deletions(-) diff --git a/Client.php b/Client.php index cd30cb2..31384e5 100644 --- a/Client.php +++ b/Client.php @@ -2,13 +2,13 @@ namespace FOS\ElasticaBundle; -use Elastica_Client; +use Elastica\Client as ElasticaClient; use FOS\ElasticaBundle\Logger\ElasticaLogger; /** * @author Gordon Franke */ -class Client extends Elastica_Client +class Client extends ElasticaClient { /** * @var ElasticaLogger @@ -20,7 +20,7 @@ class Client extends Elastica_Client $this->logger = $logger; } - public function request($path, $method, $data = array(), array $query = array()) + public function request($path, $method = Request::GET, $data = array(), array $query = array()) { $start = microtime(true); $response = parent::request($path, $method, $data, $query); diff --git a/Command/SearchCommand.php b/Command/SearchCommand.php index acf9449..62e3340 100644 --- a/Command/SearchCommand.php +++ b/Command/SearchCommand.php @@ -8,8 +8,8 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\Output; -use Elastica_Query; -use Elastica_Result; +use Elastica\Query; +use Elastica\Result; /** * Searches a type @@ -41,11 +41,11 @@ class SearchCommand extends ContainerAwareCommand protected function execute(InputInterface $input, OutputInterface $output) { $indexName = $input->getOption('index'); - /** @var $index \Elastica_Index */ + /** @var $index \Elastica\Index */ $index = $this->getContainer()->get('fos_elastica.index_manager')->getIndex($indexName ? $indexName : null); $type = $index->getType($input->getArgument('type')); - $query = Elastica_Query::create($input->getArgument('query')); - $query->setLimit($input->getOption('limit')); + $query = Query::create($input->getArgument('query')); + $query->setSize($input->getOption('limit')); if ($input->getOption('explain')) { $query->setExplain(true); } @@ -58,7 +58,7 @@ class SearchCommand extends ContainerAwareCommand } } - protected function formatResult(Elastica_Result $result, $showField, $showSource, $showId, $explain) + protected function formatResult(Result $result, $showField, $showSource, $showId, $explain) { $source = $result->getSource(); if ($showField) { diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c68aba5..ba8e9f5 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -37,7 +37,7 @@ class Configuration implements ConfigurationInterface ->arrayNode('serializer') ->treatNullLike(array()) ->children() - ->scalarNode('callback')->defaultValue('foq_elastica.serializer.callback.class')->end() + ->scalarNode('callback')->defaultValue('fos_elastica.serializer.callback.class')->end() ->scalarNode('serializer')->defaultValue('serializer')->end() ->end() ->end() @@ -219,6 +219,7 @@ class Configuration implements ConfigurationInterface ->treatNullLike(array()) ->prototype('scalar')->end() ->end() + ->scalarNode('version')->end() ->end() ->end() ->scalarNode('index_analyzer')->end() diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index c1d8aa2..8628199 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -189,6 +189,9 @@ class FOSElasticaExtension extends Extension if (isset($type['serializer']['groups'])) { $serializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } + if (isset($type['serializer']['version'])) { + $serializerDef->addMethodCall('version', array($type['serializer']['version'])); + } $container->setDefinition($serializerId, $serializerDef); diff --git a/Finder/FinderInterface.php b/Finder/FinderInterface.php index 2cc0838..4805d58 100644 --- a/Finder/FinderInterface.php +++ b/Finder/FinderInterface.php @@ -7,7 +7,7 @@ interface FinderInterface /** * Searches for query results within a given limit * - * @param mixed $query Can be a string, an array or an Elastica_Query object + * @param mixed $query Can be a string, an array or an \Elastica\Query object * @param int $limit How many results to get * @return array results */ diff --git a/Finder/PaginatedFinderInterface.php b/Finder/PaginatedFinderInterface.php index e26a048..3581cda 100644 --- a/Finder/PaginatedFinderInterface.php +++ b/Finder/PaginatedFinderInterface.php @@ -4,14 +4,14 @@ namespace FOS\ElasticaBundle\Finder; use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; use Pagerfanta\Pagerfanta; -use Elastica_Query; +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 mixed $query Can be a string, an array or an \Elastica\Query object * @return Pagerfanta paginated results */ function findPaginated($query); diff --git a/Finder/TransformedFinder.php b/Finder/TransformedFinder.php index e1eade8..e0b9cd0 100644 --- a/Finder/TransformedFinder.php +++ b/Finder/TransformedFinder.php @@ -7,8 +7,8 @@ use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter; use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; use Pagerfanta\Pagerfanta; -use Elastica_Searchable; -use Elastica_Query; +use Elastica\SearchableInterface; +use Elastica\Query; /** * Finds elastica documents and map them to persisted objects @@ -18,7 +18,7 @@ class TransformedFinder implements PaginatedFinderInterface protected $searchable; protected $transformer; - public function __construct(Elastica_Searchable $searchable, ElasticaToModelTransformerInterface $transformer) + public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) { $this->searchable = $searchable; $this->transformer = $transformer; @@ -52,9 +52,9 @@ class TransformedFinder implements PaginatedFinderInterface */ protected function search($query, $limit = null) { - $queryObject = Elastica_Query::create($query); + $queryObject = Query::create($query); if (null !== $limit) { - $queryObject->setLimit($limit); + $queryObject->setSize($limit); } $results = $this->searchable->search($queryObject)->getResults(); @@ -70,7 +70,7 @@ class TransformedFinder implements PaginatedFinderInterface */ public function findPaginated($query) { - $queryObject = Elastica_Query::create($query); + $queryObject = Query::create($query); $paginatorAdapter = $this->createPaginatorAdapter($queryObject); return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); @@ -81,7 +81,7 @@ class TransformedFinder implements PaginatedFinderInterface */ public function createPaginatorAdapter($query) { - $query = Elastica_Query::create($query); + $query = Query::create($query); return new TransformedPaginatorAdapter($this->searchable, $query, $this->transformer); } } diff --git a/HybridResult.php b/HybridResult.php index e2b8245..ebd0e99 100644 --- a/HybridResult.php +++ b/HybridResult.php @@ -2,14 +2,14 @@ namespace FOS\ElasticaBundle; -use Elastica_Result; +use Elastica\Result; class HybridResult { protected $result; protected $transformed; - public function __construct(Elastica_Result $result, $transformed = null) + public function __construct(Result $result, $transformed = null) { $this->result = $result; $this->transformed = $transformed; diff --git a/IndexManager.php b/IndexManager.php index ae844d0..e20a791 100644 --- a/IndexManager.php +++ b/IndexManager.php @@ -2,6 +2,8 @@ namespace FOS\ElasticaBundle; +use Elastica\Index; + class IndexManager { protected $indexesByName; @@ -11,9 +13,9 @@ class IndexManager * Constructor. * * @param array $indexesByName - * @param \Elastica_Index $defaultIndex + * @param Index $defaultIndex */ - public function __construct(array $indexesByName, \Elastica_Index $defaultIndex) + public function __construct(array $indexesByName, Index $defaultIndex) { $this->indexesByName = $indexesByName; $this->defaultIndexName = $defaultIndex->getName(); @@ -33,7 +35,7 @@ class IndexManager * Gets an index by its name * * @param string $name Index to return, or the default index if null - * @return \Elastica_Index + * @return Index * @throws \InvalidArgumentException if no index exists for the given name */ public function getIndex($name = null) @@ -52,7 +54,7 @@ class IndexManager /** * Gets the default index * - * @return \Elastica_Index + * @return Index */ public function getDefaultIndex() { diff --git a/Paginator/RawPaginatorAdapter.php b/Paginator/RawPaginatorAdapter.php index 9a80d39..3ed8638 100644 --- a/Paginator/RawPaginatorAdapter.php +++ b/Paginator/RawPaginatorAdapter.php @@ -2,35 +2,35 @@ namespace FOS\ElasticaBundle\Paginator; -use Elastica_Searchable; -use Elastica_Query; -use Elastica_ResultSet; +use Elastica\SearchableInterface; +use Elastica\Query; +use Elastica\ResultSet; use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; use FOS\ElasticaBundle\Paginator\RawPartialResults; use FOS\ElasticaBundle\Paginator\PartialResultsInterface; /** - * Allows pagination of Elastica_Query. Does not map results + * Allows pagination of Elastica\Query. Does not map results */ class RawPaginatorAdapter implements PaginatorAdapterInterface { /** - * @var Elastica_Searchable the object to search in + * @var SearchableInterface the object to search in */ private $searchable = null; /** - * @var Elastica_Query the query to search + * @var Query the query to search */ private $query = null; /** * @see PaginatorAdapterInterface::__construct * - * @param Elastica_Searchable $searchable the object to search in - * @param Elastica_Query $query the query to search + * @param SearchableInterface $searchable the object to search in + * @param Query $query the query to search */ - public function __construct(Elastica_Searchable $searchable, Elastica_Query $query) + public function __construct(SearchableInterface $searchable, Query $query) { $this->searchable = $searchable; $this->query = $query; @@ -41,7 +41,7 @@ class RawPaginatorAdapter implements PaginatorAdapterInterface * * @param $offset * @param $itemCountPerPage - * @return Elastica_ResultSet + * @return ResultSet */ protected function getElasticaResults($offset, $itemCountPerPage) { diff --git a/Paginator/RawPartialResults.php b/Paginator/RawPartialResults.php index ef1ed6b..270e4cd 100644 --- a/Paginator/RawPartialResults.php +++ b/Paginator/RawPartialResults.php @@ -3,8 +3,8 @@ namespace FOS\ElasticaBundle\Paginator; use FOS\ElasticaBundle\Paginator\PartialResultsInterface; -use Elastica_ResultSet; -use Elastica_Result; +use Elastica\ResultSet; +use Elastica\Result; /** * Raw partial results transforms to a simple array @@ -14,9 +14,9 @@ class RawPartialResults implements PartialResultsInterface protected $resultSet; /** - * @param \Elastica_ResultSet $resultSet + * @param ResultSet $resultSet */ - public function __construct(Elastica_ResultSet $resultSet) + public function __construct(ResultSet $resultSet) { $this->resultSet = $resultSet; } @@ -26,7 +26,7 @@ class RawPartialResults implements PartialResultsInterface */ public function toArray() { - return array_map(function(Elastica_Result $result) { + return array_map(function(Result $result) { return $result->getSource(); }, $this->resultSet->getResults()); } diff --git a/Paginator/TransformedPaginatorAdapter.php b/Paginator/TransformedPaginatorAdapter.php index 59a94d0..a668636 100644 --- a/Paginator/TransformedPaginatorAdapter.php +++ b/Paginator/TransformedPaginatorAdapter.php @@ -4,22 +4,22 @@ namespace FOS\ElasticaBundle\Paginator; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Paginator\TransformedPartialResults; -use Elastica_Searchable; -use Elastica_Query; +use Elastica\SearchableInterface; +use Elastica\Query; /** - * Allows pagination of Elastica_Query + * Allows pagination of \Elastica\Query */ class TransformedPaginatorAdapter extends RawPaginatorAdapter { private $transformer; /** - * @param Elastica_Searchable $searchable the object to search in - * @param Elastica_Query $query the query to search + * @param SearchableInterface $searchable the object to search in + * @param Query $query the query to search * @param ElasticaToModelTransformerInterface $transformer the transformer for fetching the results */ - public function __construct(Elastica_Searchable $searchable, Elastica_Query $query, ElasticaToModelTransformerInterface $transformer) + public function __construct(SearchableInterface $searchable, Query $query, ElasticaToModelTransformerInterface $transformer) { parent::__construct($searchable, $query); diff --git a/Paginator/TransformedPartialResults.php b/Paginator/TransformedPartialResults.php index 7afd6d4..f7f125a 100644 --- a/Paginator/TransformedPartialResults.php +++ b/Paginator/TransformedPartialResults.php @@ -4,7 +4,7 @@ namespace FOS\ElasticaBundle\Paginator; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Paginator\RawPartialResults; -use Elastica_ResultSet; +use Elastica\ResultSet; /** * Partial transformed result set @@ -14,10 +14,10 @@ class TransformedPartialResults extends RawPartialResults protected $transformer; /** - * @param \Elastica_ResultSet $resultSet + * @param ResultSet $resultSet * @param \FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface $transformer */ - public function __construct(Elastica_ResultSet $resultSet, ElasticaToModelTransformerInterface $transformer) + public function __construct(ResultSet $resultSet, ElasticaToModelTransformerInterface $transformer) { parent::__construct($resultSet); diff --git a/Persister/ObjectPersister.php b/Persister/ObjectPersister.php index db94880..643b817 100644 --- a/Persister/ObjectPersister.php +++ b/Persister/ObjectPersister.php @@ -3,8 +3,8 @@ namespace FOS\ElasticaBundle\Persister; use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; -use Elastica_Type; -use Elastica_Document; +use Elastica\Type; +use Elastica\Document; /** * Inserts, replaces and deletes single documents in an elastica type @@ -19,7 +19,7 @@ class ObjectPersister implements ObjectPersisterInterface protected $objectClass; protected $fields; - public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) + public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) { $this->type = $type; $this->transformer = $transformer; @@ -95,7 +95,7 @@ class ObjectPersister implements ObjectPersisterInterface * Transforms an object to an elastica document * * @param object $object - * @return Elastica_Document the elastica document + * @return Document the elastica document */ public function transformToElasticaDocument($object) { diff --git a/Propel/ElasticaToModelTransformer.php b/Propel/ElasticaToModelTransformer.php index 2678cd6..4f006a7 100644 --- a/Propel/ElasticaToModelTransformer.php +++ b/Propel/ElasticaToModelTransformer.php @@ -2,6 +2,7 @@ namespace FOS\ElasticaBundle\Propel; +use Elastica\Document; use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; @@ -65,12 +66,12 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface * Transforms an array of elastica objects into an array of * model objects fetched from the propel repository * - * @param \Elastica_Document[] $elasticaObjects array of elastica objects + * @param Document[] $elasticaObjects array of elastica objects * @return array */ public function transform(array $elasticaObjects) { - $ids = array_map(function(\Elastica_Document $elasticaObject) { + $ids = array_map(function(Document $elasticaObject) { return $elasticaObject->getId(); }, $elasticaObjects); diff --git a/README.md b/README.md index 8580481..e3f43ce 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Most of the time, you will need only one. Elastica can handle objects instead of data arrays if a serializer callable is configured #app/config/config.yml - foq_elastica: + fos_elastica: clients: default: { host: localhost, port: 9200 } serializer: @@ -73,12 +73,12 @@ Elastica can handle objects instead of data arrays if a serializer callable is c "callback" is the name of a parameter defining a class having a public method serialize($object). "serializer" is the service id for the actual serializer, e.g. 'serializer' if you're using the JMSSerializerBundle. If this is configured you can use -Elastica_Type::addObject instead of Elastica_Type::addDocument to add data to the index. +\Elastica\Type::addObject instead of \Elastica\Type::addDocument to add data to the index. The bundle provides a default implementation with a serializer service id 'serializer' that can be turned on by adding the following line to your config. #app/config/config.yml - foq_elastica: + fos_elastica: serializer: ~ #### Declare an index @@ -98,7 +98,7 @@ Most of the time, you will need only one. Here we created a "website" index, that uses our "default" client. -Our index is now available as a service: `fos_elastica.index.website`. It is an instance of `Elastica_Index`. +Our index is now available as a service: `fos_elastica.index.website`. It is an instance of `\Elastica\Index`. If you need to have different index name from the service name, for example, in order to have different indexes for different environments then you can @@ -136,14 +136,14 @@ Elasticsearch type is comparable to Doctrine entity repository. lastName: { boost: 3 } aboutMe: ~ -Our type is now available as a service: `fos_elastica.index.website.user`. It is an instance of `Elastica_Type`. +Our type is now available as a service: `fos_elastica.index.website.user`. It is an instance of `\Elastica\Type`. ### Declaring serializer groups If you are using the JMSSerializerBundle for serializing objects passed to elastica you can define serializer groups per type. - foq_elastica: + fos_elastica: clients: default: { host: localhost, port: 9200 } serializer: @@ -294,13 +294,14 @@ Its class must implement `FOS\ElasticaBundle\Provider\ProviderInterface`. namespace Acme\UserBundle\Provider; use FOS\ElasticaBundle\Provider\ProviderInterface; - use Elastica_Type; + use Elastica\Type; + use Elastica\Document; class UserProvider implements ProviderInterface { protected $userType; - public function __construct(Elastica_Type $userType) + public function __construct(Type $userType) { $this->userType = $userType; } @@ -316,7 +317,7 @@ Its class must implement `FOS\ElasticaBundle\Provider\ProviderInterface`. $loggerClosure('Indexing users'); } - $document = new \Elastica_Document(); + $document = new Document(); $document->setData(array('username' => 'Bob')); $this->userType->addDocuments(array($document)); } @@ -328,10 +329,10 @@ You will find a more complete implementation example in `src/FOS/ElasticaBundle/ You can just use the index and type Elastica objects, provided as services, to perform searches. - /** var Elastica_Type */ + /** var Elastica\Type */ $userType = $this->container->get('fos_elastica.index.website.user'); - /** var Elastica_ResultSet */ + /** var Elastica\ResultSet */ $resultSet = $userType->search('bob'); #### Doctrine/Propel finder @@ -383,7 +384,7 @@ Knp paginator: $userPaginator = $paginator->paginate($finder->createPaginatorAdapter('bob')); You can also get both the Elastica results and the entities together from the finder. -You can then access the score, highlights etc. from the Elastica_Result whilst +You can then access the score, highlights etc. from the Elastica\Result whilst still also getting the entity. /** var array of FOS\ElasticaBundle\HybridResult */ @@ -393,7 +394,7 @@ still also getting the entity. /** var Acme\UserBundle\Entity\User */ $user = $hybridResult->getTransformed(); - /** var Elastica_Result */ + /** var Elastica\Result */ $result = $hybridResult->getResult(); } @@ -628,7 +629,7 @@ Any setting can be specified when declaring a type. For example, to enable a cus By default, exceptions from the Elastica client library will propagate through the bundle's Client class. For instance, if the elasticsearch server is offline, -issuing a request will result in an `Elastica_Exception_Client` being thrown. +issuing a request will result in an `Elastica\Exception\Connection` being thrown. Depending on your needs, it may be desirable to suppress these exceptions and allow searches to fail silently. @@ -644,14 +645,17 @@ namespace Acme\ElasticaBundle; use FOS\ElasticaBundle\Client as BaseClient; +use Elastica\Exception\AbstractException; +use Elastica\Response; + class Client extends BaseClient { public function request($path, $method, $data = array()) { try { return parent::request($path, $method, $data); - } catch (\Elastica_Exception_Abstract $e) { - return new \Elastica_Response('{"took":0,"timed_out":false,"hits":{"total":0,"max_score":0,"hits":[]}}'); + } catch (AbstractException $e) { + return new Response('{"took":0,"timed_out":false,"hits":{"total":0,"max_score":0,"hits":[]}}'); } } } @@ -669,18 +673,18 @@ apply to queries against the `title` field. ```php $finder = $this->container->get('fos_elastica.finder.website.article'); -$boolQuery = new \Elastica_Query_Bool(); +$boolQuery = new \Elastica\Query\Bool(); -$fieldQuery = new \Elastica_Query_Text(); +$fieldQuery = new \Elastica\Query\Text(); $fieldQuery->setFieldQuery('title', 'I am a title string'); $fieldQuery->setFieldParam('title', 'analyzer', 'my_analyzer'); $boolQuery->addShould($fieldQuery); -$tagsQuery = new \Elastica_Query_Terms(); +$tagsQuery = new \Elastica\Query\Terms(); $tagsQuery->setTerms('tags', array('tag1', 'tag2')); $boolQuery->addShould($tagsQuery); -$categoryQuery = new \Elastica_Query_Terms(); +$categoryQuery = new \Elastica\Query\Terms(); $categoryQuery->setTerms('categoryIds', array('1', '2', '3')); $boolQuery->addMust($categoryQuery); diff --git a/Resetter.php b/Resetter.php index 7614675..7ef2895 100644 --- a/Resetter.php +++ b/Resetter.php @@ -2,6 +2,8 @@ namespace FOS\ElasticaBundle; +use Elastica\Type\Mapping; + /** * Deletes and recreates indexes */ @@ -66,11 +68,11 @@ class Resetter * create type mapping object * * @param array $indexConfig - * @return \Elastica_Type_Mapping + * @return Mapping */ protected function createMapping($indexConfig) { - $mapping = \Elastica_Type_Mapping::create($indexConfig['properties']); + $mapping = Mapping::create($indexConfig['properties']); foreach($indexConfig['properties'] as $type) { if (!empty($type['_parent']) && $type['_parent'] !== '~') { diff --git a/Resources/config/config.xml b/Resources/config/config.xml index f073aeb..7f6351d 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -6,15 +6,15 @@ FOS\ElasticaBundle\Client - Elastica_Index - Elastica_Type + Elastica\Index + Elastica\Type FOS\ElasticaBundle\Logger\ElasticaLogger FOS\ElasticaBundle\DataCollector\ElasticaDataCollector FOS\ElasticaBundle\Manager\RepositoryManager FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection FOS\ElasticaBundle\Provider\ProviderRegistry Symfony\Component\PropertyAccess\PropertyAccessor - FOQ\ElasticaBundle\Serializer\Callback + FOS\ElasticaBundle\Serializer\Callback diff --git a/Serializer/Callback.php b/Serializer/Callback.php index fff190d..a563da9 100644 --- a/Serializer/Callback.php +++ b/Serializer/Callback.php @@ -1,7 +1,8 @@ serializer = $serializer; @@ -20,14 +23,31 @@ class Callback $this->groups = $groups; } + public function setVersion($version) + { + $this->version = $version; + } + public function serialize($object) { - if ($this->serializer instanceof Serializer) { - $this->serializer->setGroups($this->groups); - } elseif ($this->groups) { - throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"'); + $context = $this->serializer instanceof Serializer ? new SerializationContext() : null; + + if ($this->groups) { + if (!$context) { + throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"'); + } + + $context->setGroups($this->groups); } - return $this->serializer->serialize($object, 'json'); + if ($this->version) { + if (!$context) { + throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer"'); + } + + $context->setVersion($this->version); + } + + return $this->serializer->serialize($object, 'json', $context); } } diff --git a/Tests/IndexManagerTest.php b/Tests/IndexManagerTest.php index 9a03986..0a8ea37 100644 --- a/Tests/IndexManagerTest.php +++ b/Tests/IndexManagerTest.php @@ -19,8 +19,8 @@ class IndexManagerTest extends \PHPUnit_Framework_TestCase 'index2' => 'test2', ); - /** @var $defaultIndex \PHPUnit_Framework_MockObject_MockObject|\Elastica_Index */ - $defaultIndex = $this->getMockBuilder('Elastica_Index') + /** @var $defaultIndex \PHPUnit_Framework_MockObject_MockObject|\Elastica\Index */ + $defaultIndex = $this->getMockBuilder('Elastica\Index') ->disableOriginalConstructor() ->getMock(); diff --git a/Tests/Persister/ObjectPersisterTest.php b/Tests/Persister/ObjectPersisterTest.php index e672e12..497c286 100644 --- a/Tests/Persister/ObjectPersisterTest.php +++ b/Tests/Persister/ObjectPersisterTest.php @@ -33,7 +33,7 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { public function setUp() { - if (!class_exists('Elastica_Type')) { + if (!class_exists('Elastica\Type')) { $this->markTestSkipped('The Elastica library classes are not available'); } } @@ -42,8 +42,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->once()) @@ -65,8 +65,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) @@ -84,8 +84,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) @@ -106,8 +106,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) @@ -125,8 +125,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->once()) @@ -147,8 +147,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) @@ -166,8 +166,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) @@ -190,8 +190,8 @@ class ObjectPersisterTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); - /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica_Type */ - $typeMock = $this->getMockBuilder('Elastica_Type') + /** @var $typeMock \PHPUnit_Framework_MockObject_MockObject|\Elastica\Type */ + $typeMock = $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); $typeMock->expects($this->never()) diff --git a/Tests/ResetterTest.php b/Tests/ResetterTest.php index 61a18a9..1a7190e 100644 --- a/Tests/ResetterTest.php +++ b/Tests/ResetterTest.php @@ -3,6 +3,7 @@ namespace FOS\ElasticaBundle\Tests\Resetter; use FOS\ElasticaBundle\Resetter; +use Elastica\Type\Mapping; class ResetterTest extends \PHPUnit_Framework_TestCase { @@ -91,7 +92,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase $type->expects($this->once()) ->method('delete'); - $mapping = \Elastica_Type_Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']); + $mapping = Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']); $type->expects($this->once()) ->method('setMapping') ->with($mapping); @@ -130,7 +131,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase $type->expects($this->once()) ->method('delete'); - $mapping = \Elastica_Type_Mapping::create($this->indexConfigsByName['parent']['config']['mappings']['a']['properties']); + $mapping = Mapping::create($this->indexConfigsByName['parent']['config']['mappings']['a']['properties']); $mapping->setParam('_parent', array('type' => 'b')); $type->expects($this->once()) ->method('setMapping') @@ -141,21 +142,21 @@ class ResetterTest extends \PHPUnit_Framework_TestCase } /** - * @return \Elastica_Index + * @return \Elastica\Index */ private function getMockElasticaIndex() { - return $this->getMockBuilder('Elastica_Index') + return $this->getMockBuilder('Elastica\Index') ->disableOriginalConstructor() ->getMock(); } /** - * @return \Elastica_Type + * @return \Elastica\Type */ private function getMockElasticaType() { - return $this->getMockBuilder('Elastica_Type') + return $this->getMockBuilder('Elastica\Type') ->disableOriginalConstructor() ->getMock(); } diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index 2660f07..9b1d782 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -2,6 +2,7 @@ namespace FOS\ElasticaBundle\Tests\Transformer; +use Elastica\Document; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection; class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCase @@ -53,8 +54,8 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa { $this->collectionSetup(); - $document1 = new \Elastica_Document(123, array('data' => 'lots of data'), 'type1'); - $document2 = new \Elastica_Document(124, array('data' => 'not so much data'), 'type2'); + $document1 = new Document(123, array('data' => 'lots of data'), 'type1'); + $document2 = new Document(124, array('data' => 'not so much data'), 'type2'); $result1 = new POPO(123, 'lots of data'); $result2 = new POPO2(124, 'not so much data'); @@ -80,8 +81,8 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa { $this->collectionSetup(); - $document1 = new \Elastica_Document(123, array('data' => 'lots of data'), 'type1'); - $document2 = new \Elastica_Document(124, array('data' => 'not so much data'), 'type1'); + $document1 = new Document(123, array('data' => 'lots of data'), 'type1'); + $document2 = new Document(124, array('data' => 'not so much data'), 'type1'); $result1 = new POPO(123, 'lots of data'); $result2 = new POPO2(124, 'not so much data'); diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index 5ac13e6..8bfbbae 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -112,7 +112,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase { public function setUp() { - if (!class_exists('Elastica_Document')) { + if (!class_exists('Elastica\Document')) { ; $this->markTestSkipped('The Elastica library classes are not available'); } @@ -124,7 +124,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase $document = $transformer->transform(new POPO(), array('name' => array())); $data = $document->getData(); - $this->assertInstanceOf('Elastica_Document', $document); + $this->assertInstanceOf('Elastica\Document', $document); $this->assertEquals(123, $document->getId()); $this->assertEquals('someName', $data['name']); } @@ -143,7 +143,7 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase ); $data = $document->getData(); - $this->assertInstanceOf('Elastica_Document', $document); + $this->assertInstanceOf('Elastica\Document', $document); $this->assertEquals(123, $document->getId()); $this->assertEquals('someName', $data['name']); $this->assertEquals(7.2, $data['float']); diff --git a/Transformer/ElasticaToModelTransformerCollection.php b/Transformer/ElasticaToModelTransformerCollection.php index 8f750c5..6264959 100644 --- a/Transformer/ElasticaToModelTransformerCollection.php +++ b/Transformer/ElasticaToModelTransformerCollection.php @@ -3,7 +3,7 @@ namespace FOS\ElasticaBundle\Transformer; use FOS\ElasticaBundle\HybridResult; -use Elastica_Document; +use Elastica\Document; /** * Holds a collection of transformers for an index wide transformation. @@ -40,7 +40,7 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer } /** - * @param Elastica_Document[] $elasticaObjects + * @param Document[] $elasticaObjects * @return array */ public function transform(array $elasticaObjects) diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index ad20529..38bd065 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -3,6 +3,7 @@ namespace FOS\ElasticaBundle\Transformer; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Elastica\Document; /** * Maps Elastica documents with Doctrine objects @@ -53,12 +54,12 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * - * @return \Elastica_Document + * @return Document **/ public function transform($object, array $fields) { $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']); - $document = new \Elastica_Document($identifier); + $document = new Document($identifier); foreach ($fields as $key => $mapping) { $value = $this->propertyAccessor->getValue($object, $key); diff --git a/Transformer/ModelToElasticaTransformerInterface.php b/Transformer/ModelToElasticaTransformerInterface.php index 924bc7c..ec9ada3 100644 --- a/Transformer/ModelToElasticaTransformerInterface.php +++ b/Transformer/ModelToElasticaTransformerInterface.php @@ -12,7 +12,7 @@ interface ModelToElasticaTransformerInterface * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array - * @return \Elastica_Document + * @return \Elastica\Document **/ function transform($object, array $fields); } diff --git a/composer.json b/composer.json index b1be5fa..bbd502f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "symfony/console": ">=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": "dev-master" }, "require-dev":{ "doctrine/orm": ">=2.2,<2.5-dev",