Skip to content

Commit

Permalink
PHPCR support
Browse files Browse the repository at this point in the history
  • Loading branch information
webda2l committed Mar 3, 2015
1 parent f83aa6f commit 8f8b331
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 23 deletions.
Expand Up @@ -9,7 +9,7 @@
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class ElasticaToDocumentTransformer extends AbstractElasticaToModelTransformer
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
{
/**
* Fetch objects for theses identifier values
Expand All @@ -20,22 +20,20 @@ class ElasticaToDocumentTransformer extends AbstractElasticaToModelTransformer
*/
protected function findByIdentifiers(array $identifierValues, $hydrate)
{
// Special case where model is interface. But maybe remove because negative performance impact
// Special case where model is interface
$reflectionClass = new \ReflectionClass($this->objectClass);
if ($reflectionClass->isInterface()) {
return iterator_to_array(
$this->registry
->getManager()
->findMany(null, $identifierValues)
);
return $this->registry
->getManager()
->findMany(null, $identifierValues)
->toArray();
}

// General case
return iterator_to_array(
$this->registry
->getManager()
->getRepository($this->objectClass)
->findMany($identifierValues)
);
return $this->registry
->getManager()
->getRepository($this->objectClass)
->findMany($identifierValues)
->toArray();
}
}
13 changes: 6 additions & 7 deletions Doctrine/PHPCR/Provider.php
Expand Up @@ -57,13 +57,12 @@ protected function fetchSlice($queryBuilder, $limit, $offset)
throw new InvalidArgumentTypeException($queryBuilder, 'Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder');
}

return iterator_to_array(
$queryBuilder
->getQuery()
->setFirstResult($offset)
->setMaxResults($limit)
->getResult()
);
return $queryBuilder
->getQuery()
->setFirstResult($offset)
->setMaxResults($limit)
->getResult()
->toArray();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/phpcr.xml
Expand Up @@ -7,7 +7,7 @@
<parameters>
<parameter key="fos_elastica.provider.prototype.phpcr.class">FOS\ElasticaBundle\Doctrine\PHPCR\Provider</parameter>
<parameter key="fos_elastica.listener.prototype.phpcr.class">FOS\ElasticaBundle\Doctrine\Listener</parameter>
<parameter key="fos_elastica.elastica_to_model_transformer.prototype.phpcr.class">FOS\ElasticaBundle\Doctrine\PHPCR\ElasticaToDocumentTransformer</parameter>
<parameter key="fos_elastica.elastica_to_model_transformer.prototype.phpcr.class">FOS\ElasticaBundle\Doctrine\PHPCR\ElasticaToModelTransformer</parameter>
<parameter key="fos_elastica.manager.phpcr.class">FOS\ElasticaBundle\Doctrine\RepositoryManager</parameter>
</parameters>

Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/setup.md
Expand Up @@ -38,7 +38,7 @@ class AppKernel extends Kernel
// ...
new FOS\ElasticaBundle\FOSElasticaBundle(),
);

// ...
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ Below is an example for the Doctrine ORM.
lastName: ~
email: ~
persistence:
# the driver can be orm, mongodb or propel
# the driver can be orm, mongodb, phpcr or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
Expand Down
65 changes: 65 additions & 0 deletions Tests/Doctrine/PHPCR/ElasticaToModelTransformerTest.php
@@ -0,0 +1,65 @@
<?php

namespace FOS\ElasticaBundle\Tests\Doctrine\PHPCR;

use FOS\ElasticaBundle\Doctrine\PHPCR\ElasticaToModelTransformer;

class ElasticaToModelTransformerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Doctrine\Common\Persistence\ManagerRegistry|\PHPUnit_Framework_MockObject_MockObject
*/
protected $registry;

/**
* @var \Doctrine\ODM\PHPCR\DocumentManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $manager;

/**
* @var \Doctrine\Common\Persistence\ObjectRepository|\PHPUnit_Framework_MockObject_MockObject
*/
protected $repository;

/**
* @var string
*/
protected $objectClass = 'stdClass';

protected function setUp()
{
if (!interface_exists('Doctrine\Common\Persistence\ManagerRegistry')) {
$this->markTestSkipped('Doctrine Common is not present');
}
if (!class_exists('Doctrine\ODM\PHPCR\DocumentManager')) {
$this->markTestSkipped('Doctrine Common is not present');
}

$this->registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();

$this->manager = $this->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
->disableOriginalConstructor()
->getMock();

$this->registry->expects($this->any())
->method('getManager')
->will($this->returnValue($this->manager));

$this->repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository', array(
'customQueryBuilderCreator',
'createQueryBuilder',
'find',
'findAll',
'findBy',
'findOneBy',
'getClassName'
));

$this->manager->expects($this->any())
->method('getRepository')
->with($this->objectClass)
->will($this->returnValue($this->repository));
}
}
35 changes: 35 additions & 0 deletions Tests/Doctrine/PHPCR/ListenerTest.php
@@ -0,0 +1,35 @@
<?php

namespace FOS\ElasticaBundle\Tests\Doctrine\PHPCR;

use FOS\ElasticaBundle\Tests\Doctrine\ListenerTest as BaseListenerTest;

class ListenerTest extends BaseListenerTest
{
public function setUp()
{
if (!class_exists('Doctrine\ODM\PHPCR\DocumentManager')) {
$this->markTestSkipped('Doctrine PHPCR is not available.');
}
}

protected function getClassMetadataClass()
{
return 'Doctrine\ODM\PHPCR\Mapping\ClassMetadata';
}

protected function getLifecycleEventArgsClass()
{
return 'Doctrine\Common\Persistence\Event\LifecycleEventArgs';
}

protected function getListenerClass()
{
return 'FOS\ElasticaBundle\Doctrine\Listener';
}

protected function getObjectManagerClass()
{
return 'Doctrine\ODM\PHPCR\DocumentManager';
}
}
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -23,6 +23,7 @@
"require-dev":{
"doctrine/orm": "~2.4",
"doctrine/doctrine-bundle": "~1.2@beta",
"doctrine/phpcr-bundle": "~1.2",
"jms/serializer-bundle": "@stable",
"phpunit/phpunit": "~4.1",
"propel/propel1": "1.6.*",
Expand All @@ -36,6 +37,7 @@
"suggest": {
"doctrine/orm": "~2.4",
"doctrine/mongodb-odm": "1.0.*@dev",
"doctrine/phpcr-bundle": "~1.2",
"propel/propel1": "1.6.*",
"pagerfanta/pagerfanta": "1.0.*@dev",
"knplabs/knp-components": "~1.2",
Expand Down

0 comments on commit 8f8b331

Please sign in to comment.