From c5ee26099bbfe846a558f0357704ddbd8b62750c Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 10 Jan 2013 14:05:50 +0100 Subject: [PATCH 01/14] Adding support to pass a serializer to elastica --- DependencyInjection/Configuration.php | 10 ++++++++++ DependencyInjection/FOSElasticaExtension.php | 12 ++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4482a4c..6f5c35c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,6 +34,7 @@ class Configuration implements ConfigurationInterface ->scalarNode('default_client')->end() ->scalarNode('default_index')->end() ->scalarNode('default_manager')->defaultValue('orm')->end() + ->scalarNode('serializer_id')->end() ->end() ; @@ -205,6 +206,15 @@ class Configuration implements ConfigurationInterface ->prototype('array') ->treatNullLike(array()) ->children() + ->arrayNode('serializer') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('groups') + ->treatNullLike(array()) + ->prototype('scalar')->end() + ->end() + ->end() + ->end() ->scalarNode('index_analyzer')->end() ->scalarNode('search_analyzer')->end() ->arrayNode('persistence') diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 0cb5c27..4cad42c 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -40,7 +40,7 @@ class FOSElasticaExtension extends Extension } $clientIdsByName = $this->loadClients($config['clients'], $container); - $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']); + $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $config['serializer_id']); $indexRefsByName = array_map(function($id) { return new Reference($id); }, $indexIdsByName); @@ -93,7 +93,7 @@ class FOSElasticaExtension extends Extension * @throws \InvalidArgumentException * @return array */ - protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName) + protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName, $serializerId) { $indexIds = array(); foreach ($indexes as $name => $index) { @@ -128,7 +128,7 @@ class FOSElasticaExtension extends Extension if (!empty($index['settings'])) { $this->indexConfigs[$name]['config']['settings'] = $index['settings']; } - $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig); + $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig, $serializerId); } return $indexIds; @@ -169,8 +169,9 @@ class FOSElasticaExtension extends Extension * @param $indexId * @param array $typePrototypeConfig */ - protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig) + protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig, $serializerId) { + $serializer = $serializerId ? new Reference($serializerId) : null; foreach ($types as $name => $type) { $type = self::deepArrayUnion($typePrototypeConfig, $type); $typeId = sprintf('%s.%s', $indexId, $name); @@ -178,6 +179,9 @@ class FOSElasticaExtension extends Extension $typeDef = new Definition('%fos_elastica.type.class%', $typeDefArgs); $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); + if ($serializer) { + $typeDef->addMethodCall('setSerializer', array($serializer, $type['serializer']['groups'])); + } $container->setDefinition($typeId, $typeDef); if (isset($type['_source'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['_source'] = $type['_source']; From 73fd4fe6b00e22dfa582fd8e13261d341edb3ddb Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 24 Jan 2013 15:07:38 +0100 Subject: [PATCH 02/14] Creating a serializer callable for every type in an index and passing it to elastica --- DependencyInjection/Configuration.php | 10 ++++++++- DependencyInjection/FOSElasticaExtension.php | 23 ++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6f5c35c..7e0af16 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,7 +34,15 @@ class Configuration implements ConfigurationInterface ->scalarNode('default_client')->end() ->scalarNode('default_index')->end() ->scalarNode('default_manager')->defaultValue('orm')->end() - ->scalarNode('serializer_id')->end() + ->arrayNode('serializer') + ->prototype('array') + ->treatNullLike(array()) + ->children() + ->scalarNode('callable_class')->end() + ->scalarNode('id')->end() + ->end() + ->end() + ->end() ->end() ; diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 4cad42c..1469510 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -40,7 +40,7 @@ class FOSElasticaExtension extends Extension } $clientIdsByName = $this->loadClients($config['clients'], $container); - $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $config['serializer_id']); + $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $config['serializer']); $indexRefsByName = array_map(function($id) { return new Reference($id); }, $indexIdsByName); @@ -93,7 +93,7 @@ class FOSElasticaExtension extends Extension * @throws \InvalidArgumentException * @return array */ - protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName, $serializerId) + protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName, $serializerConfig) { $indexIds = array(); foreach ($indexes as $name => $index) { @@ -128,7 +128,7 @@ class FOSElasticaExtension extends Extension if (!empty($index['settings'])) { $this->indexConfigs[$name]['config']['settings'] = $index['settings']; } - $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig, $serializerId); + $this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId, $typePrototypeConfig, $serializerConfig); } return $indexIds; @@ -169,9 +169,8 @@ class FOSElasticaExtension extends Extension * @param $indexId * @param array $typePrototypeConfig */ - protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig, $serializerId) + protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId, array $typePrototypeConfig, $serializerConfig) { - $serializer = $serializerId ? new Reference($serializerId) : null; foreach ($types as $name => $type) { $type = self::deepArrayUnion($typePrototypeConfig, $type); $typeId = sprintf('%s.%s', $indexId, $name); @@ -179,8 +178,18 @@ class FOSElasticaExtension extends Extension $typeDef = new Definition('%fos_elastica.type.class%', $typeDefArgs); $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); - if ($serializer) { - $typeDef->addMethodCall('setSerializer', array($serializer, $type['serializer']['groups'])); + if ($serializerConfig) { + + $serializerDefArgs = array(new Reference($serializerConfig['id'])); + if (isset($type['serializer']['groups'])) { + $serializerDefArgs[] = $type['serializer']['groups']; + } + + $serializerDef = new Definition("%{$serializerConfig['callable_class']}%", $serializerDefArgs); + $serializerId = sprintf('%s.%s.serializer', $indexId, $name); + $container->setDefinition($serializerId, $serializerDef); + + $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); } $container->setDefinition($typeId, $typeDef); if (isset($type['_source'])) { From c99eee9c4b0b8f1239e1d6256bd7f53a35cfd631 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 24 Jan 2013 15:57:02 +0100 Subject: [PATCH 03/14] Adding some documentation --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/README.md b/README.md index 0bcae02..cc39175 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,23 @@ Most of the time, you will need only one. clients: default: { host: localhost, port: 9200 } + +#### Declare a serializer + +Elastica can handle objects instead of data arrays if a serializer callable is configured + + #app/config/config.yml + foq_elastica: + clients: + default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer + +"callable_class" is the class name of a class having a public method serialize($object). "id" 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. + #### Declare an index Elasticsearch index is comparable to Doctrine entity manager. @@ -66,6 +83,9 @@ Most of the time, you will need only one. fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default @@ -96,6 +116,9 @@ Elasticsearch type is comparable to Doctrine entity repository. fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default @@ -109,11 +132,38 @@ Elasticsearch type is comparable to Doctrine entity repository. 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: + clients: + default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer + indexes: + website: + client: default + types: + user: + mappings: + username: { boost: 5 } + firstName: { boost: 3 } + lastName: { boost: 3 } + aboutMe: + serializer: + groups: [elastica, Default] + ### Declaring parent field fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default @@ -129,6 +179,9 @@ Our type is now available as a service: `fos_elastica.index.website.user`. It is fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default @@ -164,6 +217,9 @@ some configuration will let ElasticaBundle do it for us. fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default @@ -281,6 +337,9 @@ Declare that you want a Doctrine/Propel finder in your configuration: fos_elastica: clients: default: { host: localhost, port: 9200 } + serializer: + callable_class: %classname% + id: serializer indexes: website: client: default From a139d18b220b60334192669e6eb87c2dd143cdab Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Fri, 25 Jan 2013 16:48:29 +0100 Subject: [PATCH 04/14] Adding default callback for serialization if serialization is turned on --- DependencyInjection/Configuration.php | 10 ++++---- DependencyInjection/FOSElasticaExtension.php | 13 +++++----- Resources/config/config.xml | 6 +++-- Serializer/Callback.php | 25 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 Serializer/Callback.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7e0af16..f23f3a8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -35,12 +35,10 @@ class Configuration implements ConfigurationInterface ->scalarNode('default_index')->end() ->scalarNode('default_manager')->defaultValue('orm')->end() ->arrayNode('serializer') - ->prototype('array') - ->treatNullLike(array()) - ->children() - ->scalarNode('callable_class')->end() - ->scalarNode('id')->end() - ->end() + ->treatNullLike(array()) + ->children() + ->scalarNode('callback')->defaultValue('foq_elastica.serializer.callback')->end() + ->scalarNode('serializer')->defaultValue('serializer')->end() ->end() ->end() ->end() diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 1469510..1c2cbe0 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -40,7 +40,8 @@ class FOSElasticaExtension extends Extension } $clientIdsByName = $this->loadClients($config['clients'], $container); - $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $config['serializer']); + $serializerConfig = isset($config['serializer']) ? $config['serializer'] : null; + $indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client'], $serializerConfig); $indexRefsByName = array_map(function($id) { return new Reference($id); }, $indexIdsByName); @@ -179,14 +180,12 @@ class FOSElasticaExtension extends Extension $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); if ($serializerConfig) { - - $serializerDefArgs = array(new Reference($serializerConfig['id'])); + $serializerDef = clone $container->getDefinition($serializerConfig['callback']); + $serializerDef->addMethodCall('setSerializer', array(new Reference($serializerConfig['serializer']))); if (isset($type['serializer']['groups'])) { - $serializerDefArgs[] = $type['serializer']['groups']; + $serializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } - - $serializerDef = new Definition("%{$serializerConfig['callable_class']}%", $serializerDefArgs); - $serializerId = sprintf('%s.%s.serializer', $indexId, $name); + $serializerId = sprintf('%s.%s.serializer.callback', $indexId, $name); $container->setDefinition($serializerId, $serializerDef); $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); diff --git a/Resources/config/config.xml b/Resources/config/config.xml index bf9d40d..f073aeb 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -14,6 +14,7 @@ FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection FOS\ElasticaBundle\Provider\ProviderRegistry Symfony\Component\PropertyAccess\PropertyAccessor + FOQ\ElasticaBundle\Serializer\Callback @@ -77,8 +78,9 @@ - - + + + diff --git a/Serializer/Callback.php b/Serializer/Callback.php new file mode 100644 index 0000000..576004a --- /dev/null +++ b/Serializer/Callback.php @@ -0,0 +1,25 @@ +serializer = $serializer; + } + + public function setGroups($groups){ + $this->groups = $groups; + } + + public function serialize($object) + { + $this->serializer->setGroups(null); + $this->serializer->setGroups($this->groups); + return $this->serializer->serialize($object, 'json'); + } +} From bcf564d09c2116b2f9ed1df28503e54b9bc5f40d Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Mon, 28 Jan 2013 08:22:01 +0100 Subject: [PATCH 05/14] Adapting readme --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cc39175..e8ba475 100644 --- a/README.md +++ b/README.md @@ -68,12 +68,18 @@ Elastica can handle objects instead of data arrays if a serializer callable is c clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% - id: serializer + callback: callback + serializer: serializer -"callable_class" is the class name of a class having a public method serialize($object). "id" is the service id for the +"callback" is the service 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. +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: + serializer: ~ #### Declare an index From 1abe1f48ddd92253f106c72f9bb77dd3601c5e41 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Mon, 28 Jan 2013 08:54:53 +0100 Subject: [PATCH 06/14] Having a parameter name defining a class for the serializer callback, because directly putting service id or parameter doesn't work because these values are not available at bundle configuration time --- DependencyInjection/Configuration.php | 2 +- DependencyInjection/FOSElasticaExtension.php | 8 ++++++-- README.md | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f23f3a8..c68aba5 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')->end() + ->scalarNode('callback')->defaultValue('foq_elastica.serializer.callback.class')->end() ->scalarNode('serializer')->defaultValue('serializer')->end() ->end() ->end() diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 1c2cbe0..c1d8aa2 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -180,12 +180,16 @@ class FOSElasticaExtension extends Extension $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); if ($serializerConfig) { - $serializerDef = clone $container->getDefinition($serializerConfig['callback']); + + $serializerDef = new Definition("%{$serializerConfig['callback']}%"); + $serializerId = sprintf('%s.%s.serializer.callback', $indexId, $name); + + $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); $serializerDef->addMethodCall('setSerializer', array(new Reference($serializerConfig['serializer']))); if (isset($type['serializer']['groups'])) { $serializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } - $serializerId = sprintf('%s.%s.serializer.callback', $indexId, $name); + $container->setDefinition($serializerId, $serializerDef); $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); diff --git a/README.md b/README.md index e8ba475..8580481 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Elastica can handle objects instead of data arrays if a serializer callable is c callback: callback serializer: serializer -"callback" is the service having a public method serialize($object). "serializer" is the service id for the +"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. The bundle provides a default implementation with a serializer service id 'serializer' that can be turned on by adding From d8f836d17929b0e15342a6bbe6dc849ba4ca70a1 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Tue, 29 Jan 2013 23:17:40 +0100 Subject: [PATCH 07/14] CS fixes and minor tweaks in the Callback class --- Serializer/Callback.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Serializer/Callback.php b/Serializer/Callback.php index 576004a..fff190d 100644 --- a/Serializer/Callback.php +++ b/Serializer/Callback.php @@ -2,24 +2,32 @@ namespace FOQ\ElasticaBundle\Serializer; +use JMS\Serializer\Serializer; + class Callback { protected $serializer; protected $groups; - public function setSerializer($serializer){ + public function setSerializer($serializer) + { $this->serializer = $serializer; } - public function setGroups($groups){ + public function setGroups(array $groups) + { $this->groups = $groups; } public function serialize($object) { - $this->serializer->setGroups(null); - $this->serializer->setGroups($this->groups); + if ($this->serializer instanceof Serializer) { + $this->serializer->setGroups($this->groups); + } elseif ($this->groups) { + throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"'); + } + return $this->serializer->serialize($object, 'json'); } } From aafb6e53fb8f99e37f4113027189cbb7abd4b949 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 22 Apr 2013 13:20:10 +0200 Subject: [PATCH 08/14] 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", From 965ee39c5a67497bea59513eda5fc89dff4f3166 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 22 Apr 2013 16:08:21 +0200 Subject: [PATCH 09/14] typo fix --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e3f43ce..02c600f 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Most of the time, you will need only one. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -123,7 +123,7 @@ Elasticsearch type is comparable to Doctrine entity repository. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -147,7 +147,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -168,7 +168,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -186,7 +186,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -224,7 +224,7 @@ some configuration will let ElasticaBundle do it for us. clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: @@ -345,7 +345,7 @@ Declare that you want a Doctrine/Propel finder in your configuration: clients: default: { host: localhost, port: 9200 } serializer: - callable_class: %classname% + callback: %classname% id: serializer indexes: website: From 8a9a9686babbff82320fd8e249d5d6f2719e6de5 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 22 Apr 2013 17:00:26 +0200 Subject: [PATCH 10/14] various tweaks --- DependencyInjection/FOSElasticaExtension.php | 2 +- README.md | 10 +++++----- Serializer/Callback.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 8628199..e67ba53 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -190,7 +190,7 @@ class FOSElasticaExtension extends Extension $serializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } if (isset($type['serializer']['version'])) { - $serializerDef->addMethodCall('version', array($type['serializer']['version'])); + $serializerDef->addMethodCall('setVersion', array($type['serializer']['version'])); } $container->setDefinition($serializerId, $serializerDef); diff --git a/README.md b/README.md index 02c600f..87b01aa 100644 --- a/README.md +++ b/README.md @@ -71,11 +71,11 @@ Elastica can handle objects instead of data arrays if a serializer callable is c callback: callback serializer: serializer -"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. -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. +``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 +JMSSerializerBundle. If this is configured you can use ``\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 fos_elastica: diff --git a/Serializer/Callback.php b/Serializer/Callback.php index a563da9..fbf391e 100644 --- a/Serializer/Callback.php +++ b/Serializer/Callback.php @@ -3,7 +3,7 @@ namespace FOS\ElasticaBundle\Serializer; use JMS\Serializer\SerializationContext; -use JMS\Serializer\Serializer; +use JMS\Serializer\SerializerInterface; class Callback { @@ -30,7 +30,7 @@ class Callback public function serialize($object) { - $context = $this->serializer instanceof Serializer ? new SerializationContext() : null; + $context = $this->serializer instanceof SerializerInterface ? new SerializationContext() : array(); if ($this->groups) { if (!$context) { From 800e38f8aa9666860440f9eab1d622d04b0c3725 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 22 Apr 2013 20:02:12 +0200 Subject: [PATCH 11/14] tweaked the callback handling --- DependencyInjection/Configuration.php | 2 +- DependencyInjection/FOSElasticaExtension.php | 17 +++++---- README.md | 36 +++++++++++--------- Serializer/Callback.php | 23 ++++++++----- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ba8e9f5..966f04f 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('fos_elastica.serializer.callback.class')->end() + ->scalarNode('callback_class')->defaultValue('FOS\ElasticaBundle\Serializer\Callback')->end() ->scalarNode('serializer')->defaultValue('serializer')->end() ->end() ->end() diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index e67ba53..3f757df 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -180,22 +180,21 @@ class FOSElasticaExtension extends Extension $typeDef->setFactoryService($indexId); $typeDef->setFactoryMethod('getType'); if ($serializerConfig) { + $callbackDef = new Definition($serializerConfig['callback_class']); + $callbackId = sprintf('%s.%s.serializer.callback', $indexId, $name); - $serializerDef = new Definition("%{$serializerConfig['callback']}%"); - $serializerId = sprintf('%s.%s.serializer.callback', $indexId, $name); - - $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); - $serializerDef->addMethodCall('setSerializer', array(new Reference($serializerConfig['serializer']))); + $typeDef->addMethodCall('setSerializer', array(array(new Reference($callbackId), 'serialize'))); + $callbackDef->addMethodCall('setSerializer', array(new Reference($serializerConfig['serializer']))); if (isset($type['serializer']['groups'])) { - $serializerDef->addMethodCall('setGroups', array($type['serializer']['groups'])); + $callbackDef->addMethodCall('setGroups', array($type['serializer']['groups'])); } if (isset($type['serializer']['version'])) { - $serializerDef->addMethodCall('setVersion', array($type['serializer']['version'])); + $callbackDef->addMethodCall('setVersion', array($type['serializer']['version'])); } - $container->setDefinition($serializerId, $serializerDef); + $container->setDefinition($callbackId, $callbackDef); - $typeDef->addMethodCall('setSerializer', array(array(new Reference($serializerId), 'serialize'))); + $typeDef->addMethodCall('setSerializer', array(array(new Reference($callbackId), 'serialize'))); } $container->setDefinition($typeId, $typeDef); if (isset($type['_source'])) { diff --git a/README.md b/README.md index 87b01aa..a0acec2 100644 --- a/README.md +++ b/README.md @@ -68,10 +68,12 @@ Elastica can handle objects instead of data arrays if a serializer callable is c clients: default: { host: localhost, port: 9200 } serializer: - callback: callback + callback_class: callback_class serializer: serializer -``callback`` is the name of a parameter defining a class having a public method serialize($object). +``callback_class`` is the name of a class having a public method serialize($object) and should +extends from ``FOS\ElasticaBundle\Serializer\Callback``. + ``serializer`` is the service id for the actual serializer, e.g. ``serializer`` if you're using JMSSerializerBundle. If this is configured you can use ``\Elastica\Type::addObject`` instead of ``\Elastica\Type::addDocument`` to add data to the index. The bundle provides a default implementation @@ -90,7 +92,7 @@ Most of the time, you will need only one. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -123,7 +125,7 @@ Elasticsearch type is comparable to Doctrine entity repository. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -147,7 +149,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: %classname% id: serializer indexes: website: @@ -168,7 +170,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -186,7 +188,7 @@ per type. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -224,7 +226,7 @@ some configuration will let ElasticaBundle do it for us. clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -345,7 +347,7 @@ Declare that you want a Doctrine/Propel finder in your configuration: clients: default: { host: localhost, port: 9200 } serializer: - callback: %classname% + callback_class: FOS\ElasticaBundle\Serializer\Callback id: serializer indexes: website: @@ -575,28 +577,28 @@ You can also choose to only listen for some of the events: If you use listeners to update your index, you may need to validate your entities before you index them (e.g. only index "public" entities). Typically, you'll want the listener to be consistent with the provider's query criteria. -This may be achieved by using the `is_indexable_callback` config parameter: +This may be achieved by using the `is_indexable_callback_class` config parameter: persistence: listener: - is_indexable_callback: "isPublic" + is_indexable_callback_class: "isPublic" -If `is_indexable_callback` is a string and the entity has a method with the +If `is_indexable_callback_class` is a string and the entity has a method with the specified name, the listener will only index entities for which the method returns `true`. Additionally, you may provide a service and method name pair: persistence: listener: - is_indexable_callback: [ "%custom_service_id%", "isIndexable" ] + is_indexable_callback_class: [ "%custom_service_id%", "isIndexable" ] -In this case, the callback will be the `isIndexable()` method on the specified +In this case, the callback_class will be the `isIndexable()` method on the specified service and the object being considered for indexing will be passed as the only argument. This allows you to do more complex validation (e.g. ACL checks). -As you might expect, new entities will only be indexed if the callback returns +As you might expect, new entities will only be indexed if the callback_class returns `true`. Additionally, modified entities will be updated or removed from the -index depending on whether the callback returns `true` or `false`, respectively. -The delete listener disregards the callback. +index depending on whether the callback_class returns `true` or `false`, respectively. +The delete listener disregards the callback_class. > **Propel** doesn't support this feature yet. diff --git a/Serializer/Callback.php b/Serializer/Callback.php index fbf391e..50307e0 100644 --- a/Serializer/Callback.php +++ b/Serializer/Callback.php @@ -16,16 +16,31 @@ class Callback public function setSerializer($serializer) { $this->serializer = $serializer; + if (!method_exists($this->serializer, 'serialize')) { + throw new \RuntimeException('The serializer must have a "serialize" method.'); + } } public function setGroups(array $groups) { $this->groups = $groups; + + if ($this->groups) { + if (!$this->serializer instanceof SerializerInterface) { + throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".'); + } + } } public function setVersion($version) { $this->version = $version; + + if ($this->version) { + if (!$this->serializer instanceof SerializerInterface) { + throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".'); + } + } } public function serialize($object) @@ -33,18 +48,10 @@ class Callback $context = $this->serializer instanceof SerializerInterface ? new SerializationContext() : array(); if ($this->groups) { - if (!$context) { - throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"'); - } - $context->setGroups($this->groups); } if ($this->version) { - if (!$context) { - throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer"'); - } - $context->setVersion($this->version); } From 0af8f6ce199c89e9e4de852c0d2208f9a1eaa12d Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Wed, 24 Apr 2013 09:02:33 +0200 Subject: [PATCH 12/14] updated for Elastica master changes --- README.md | 4 ++-- composer.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0acec2..822cf3f 100644 --- a/README.md +++ b/README.md @@ -647,7 +647,7 @@ namespace Acme\ElasticaBundle; use FOS\ElasticaBundle\Client as BaseClient; -use Elastica\Exception\AbstractException; +use Elastica\Exception\ExceptionInterface; use Elastica\Response; class Client extends BaseClient @@ -656,7 +656,7 @@ class Client extends BaseClient { try { return parent::request($path, $method, $data); - } catch (AbstractException $e) { + } catch (ExceptionInterface $e) { return new Response('{"took":0,"timed_out":false,"hits":{"total":0,"max_score":0,"hits":[]}}'); } } diff --git a/composer.json b/composer.json index bbd502f..701cfde 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": "dev-master" + "ruflin/elastica": "0.20.5.0.*@dev" }, "require-dev":{ "doctrine/orm": ">=2.2,<2.5-dev", @@ -38,7 +38,7 @@ "target-dir": "FOS/ElasticaBundle", "extra": { "branch-alias": { - "dev-master": "2.1.x-dev" + "dev-master": "3.0.x-dev" } } } From c35cb1b25f333de990ec0e3d2d454eea55b762f9 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Wed, 24 Apr 2013 10:02:41 +0200 Subject: [PATCH 13/14] fixed Elastica dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 701cfde..69edc36 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.20.5.0.*@dev" + "ruflin/elastica": "0.20.5.*@dev" }, "require-dev":{ "doctrine/orm": ">=2.2,<2.5-dev", From e898deb6df0accc4a9f994f480fb90ea5fa99780 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Fri, 26 Apr 2013 14:12:28 +0200 Subject: [PATCH 14/14] cleanups --- Resources/config/config.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 7f6351d..cd91961 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -14,7 +14,6 @@ FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection FOS\ElasticaBundle\Provider\ProviderRegistry Symfony\Component\PropertyAccess\PropertyAccessor - FOS\ElasticaBundle\Serializer\Callback @@ -79,8 +78,6 @@ - -