Fix array format for indexable_callback

This commit is contained in:
Tim Nagel 2014-06-26 17:46:29 +10:00
parent 4eacb5f4c8
commit 96dc613c71
7 changed files with 61 additions and 4 deletions

View file

@ -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()

View file

@ -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);
}

View file

@ -19,6 +19,7 @@
<service id="fos_elastica.indexable" class="%fos_elastica.indexable.class%">
<argument type="collection" /> <!-- array of indexable callbacks keyed by type name -->
<argument type="service" id="service_container" />
</service>
<service id="fos_elastica.index_prototype" class="%fos_elastica.index.class%" factory-service="fos_elastica.client" factory-method="getIndex" abstract="true">

View file

@ -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

View file

@ -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()

View file

@ -0,0 +1,25 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* 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;
}
}

View file

@ -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' ]