From 96dc613c7125670d237da725f8f28117a8bae573 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Thu, 26 Jun 2014 17:46:29 +1000 Subject: [PATCH] Fix array format for indexable_callback --- DependencyInjection/Configuration.php | 2 +- Provider/Indexable.php | 15 ++++++++++- Resources/config/index.xml | 1 + Resources/doc/types.md | 3 +++ Tests/Functional/IndexableCallbackTest.php | 3 ++- Tests/Functional/app/ORM/IndexableService.php | 25 +++++++++++++++++++ Tests/Functional/app/ORM/config.yml | 16 +++++++++++- 7 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/app/ORM/IndexableService.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 3564fd4..52088b6 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -205,7 +205,7 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('index_analyzer')->end() ->scalarNode('search_analyzer')->end() - ->scalarNode('indexable_callback')->end() + ->variableNode('indexable_callback')->end() ->append($this->getPersistenceNode()) ->append($this->getSerializerNode()) ->end() diff --git a/Provider/Indexable.php b/Provider/Indexable.php index 09168a1..827b3a4 100644 --- a/Provider/Indexable.php +++ b/Provider/Indexable.php @@ -11,6 +11,7 @@ namespace FOS\ElasticaBundle\Provider; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\ExpressionLanguage\Expression; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\ExpressionLanguage\SyntaxError; @@ -26,6 +27,11 @@ class Indexable implements IndexableInterface */ private $callbacks = array(); + /** + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + /** * An instance of ExpressionLanguage * @@ -50,9 +56,10 @@ class Indexable implements IndexableInterface /** * @param array $callbacks */ - public function __construct(array $callbacks) + public function __construct(array $callbacks, ContainerInterface $container) { $this->callbacks = $callbacks; + $this->container = $container; $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); } @@ -105,6 +112,12 @@ class Indexable implements IndexableInterface if (is_array($callback)) { list($class, $method) = $callback + array(null, null); + if (strpos($class, '@') === 0) { + $service = $this->container->get(substr($class, 1)); + + return array($service, $method); + } + if (is_object($class)) { $class = get_class($class); } diff --git a/Resources/config/index.xml b/Resources/config/index.xml index 17a0ec9..11586ff 100644 --- a/Resources/config/index.xml +++ b/Resources/config/index.xml @@ -19,6 +19,7 @@ + diff --git a/Resources/doc/types.md b/Resources/doc/types.md index be687d7..80d295b 100644 --- a/Resources/doc/types.md +++ b/Resources/doc/types.md @@ -177,6 +177,9 @@ The callback option supports multiple approaches: * An array of a service id and a method which will be called with the object as the first and only argument. `[ @my_custom_service, 'userIndexable' ]` will call the userIndexable method on a service defined as my_custom_service. +* An array of a class and a static method to call on that class which will be called with + the object as the only argument. `[ 'Acme\DemoBundle\IndexableChecker', 'isIndexable' ]` + will call Acme\DemoBundle\IndexableChecker::isIndexable($object) * If you have the ExpressionLanguage component installed, A valid ExpressionLanguage expression provided as a string. The object being indexed will be supplied as `object` in the expression. `object.isEnabled() or object.shouldBeIndexedAnyway()`. For more diff --git a/Tests/Functional/IndexableCallbackTest.php b/Tests/Functional/IndexableCallbackTest.php index 89fca1d..41ed402 100644 --- a/Tests/Functional/IndexableCallbackTest.php +++ b/Tests/Functional/IndexableCallbackTest.php @@ -31,8 +31,9 @@ class IndexableCallbackTest extends WebTestCase $in = $client->getContainer()->get('fos_elastica.indexable'); $this->assertTrue($in->isObjectIndexable('index', 'type', new TypeObj())); - $this->assertFalse($in->isObjectIndexable('index', 'type2', new TypeObj())); + $this->assertTrue($in->isObjectIndexable('index', 'type2', new TypeObj())); $this->assertFalse($in->isObjectIndexable('index', 'type3', new TypeObj())); + $this->assertFalse($in->isObjectIndexable('index', 'type4', new TypeObj())); } protected function setUp() diff --git a/Tests/Functional/app/ORM/IndexableService.php b/Tests/Functional/app/ORM/IndexableService.php new file mode 100644 index 0000000..018451e --- /dev/null +++ b/Tests/Functional/app/ORM/IndexableService.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Functional\app\ORM; + +class IndexableService +{ + public function isIndexable($object) + { + return true; + } + + public static function isntIndexable($object) + { + return false; + } +} diff --git a/Tests/Functional/app/ORM/config.yml b/Tests/Functional/app/ORM/config.yml index 448d62b..9ba6830 100644 --- a/Tests/Functional/app/ORM/config.yml +++ b/Tests/Functional/app/ORM/config.yml @@ -9,6 +9,10 @@ doctrine: auto_generate_proxy_classes: false auto_mapping: false +services: + indexableService: + class: FOS\ElasticaBundle\Tests\Functional\app\ORM\IndexableService + fos_elastica: clients: default: @@ -41,7 +45,7 @@ fos_elastica: driver: orm model: FOS\ElasticaBundle\Tests\Functional\TypeObj listener: - is_indexable_callback: 'object.isntIndexable()' + is_indexable_callback: [ @indexableService, 'isIndexable' ] type3: mappings: field1: ~ @@ -52,3 +56,13 @@ fos_elastica: provider: ~ listener: is_indexable_callback: 'isntIndexable' + type4: + mappings: + field1: ~ + persistence: + driver: orm + model: FOS\ElasticaBundle\Tests\Functional\TypeObj + finder: ~ + provider: ~ + listener: + is_indexable_callback: [ 'FOS\ElasticaBundle\Tests\Functional\app\ORM\IndexableService', 'isntIndexable' ]