Skip to content
This repository has been archived by the owner on Apr 22, 2022. It is now read-only.

[WIP] Implemented DependentMappingDriver #105

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/Doctrine/Search/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,38 @@ public function getEntitySerializer()
}

/**
* @param ObjectManager $entityManager
* @param ObjectManager $objectManager
*/
public function setEntityManager(ObjectManager $entityManager)
public function setObjectManager(ObjectManager $objectManager)
{
$this->attributes['entityManager'] = $entityManager;
$this->attributes['objectManager'] = $objectManager;
}

/**
* @return ObjectManager
*/
public function getObjectManager()
{
if (isset($this->attributes['objectManager'])) {
return $this->attributes['objectManager'];
}
}

/**
* @deprecated
*/
public function setEntityManager(ObjectManager $objectManager)
{
$this->setObjectManager($objectManager);
}

/**
* @deprecated
*/
public function getEntityManager()
{
if (isset($this->attributes['entityManager'])) {
return $this->attributes['entityManager'];
if (isset($this->attributes['objectManager'])) {
return $this->attributes['objectManager'];
}
}
}
10 changes: 10 additions & 0 deletions lib/Doctrine/Search/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Doctrine\Search\Mapping;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Search\SearchManager;
use Doctrine\Search\Configuration;
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
Expand Down Expand Up @@ -66,6 +67,15 @@ protected function initialize()
$this->driver = $this->config->getMetadataDriverImpl();
$this->evm = $this->sm->getEventManager();
$this->initialized = true;

$om = $this->sm->getObjectManager();
if ($this->driver instanceof DependentMappingDriver && $om instanceof ObjectManager) {
$parentMetadataFactory = $om->getMetadataFactory();
if ($parentMetadataFactory instanceof AbstractClassMetadataFactory) {
$parentMetadataFactory->initialize();
$this->driver->setParentDriver($parentMetadataFactory->getDriver());
}
}
}

/**
Expand Down
41 changes: 41 additions & 0 deletions lib/Doctrine/Search/Mapping/DependentMappingDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Search\Mapping;

use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;



/**
* If a driver implements this interface,
* it should delegate the getAllClassNames method to the parent driver.
*
* @author Filip Procházka <[email protected]>
*/
interface DependentMappingDriver
{

/**
* @param MappingDriver $driver
* @return void
*/
public function setParentDriver(MappingDriver $driver);

}
38 changes: 37 additions & 1 deletion lib/Doctrine/Search/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
namespace Doctrine\Search\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Search\Mapping\Annotations as Search;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Search\Exception\Driver as DriverException;
use Doctrine\Search\Mapping\DependentMappingDriver;



/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
Expand All @@ -31,7 +35,7 @@
* @since 1.0
* @author Mike Lohmann <[email protected]>
*/
class AnnotationDriver extends AbstractAnnotationDriver
class AnnotationDriver extends AbstractAnnotationDriver implements DependentMappingDriver
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -59,7 +63,18 @@ class AnnotationDriver extends AbstractAnnotationDriver
'Doctrine\\Search\\Mapping\\Annotations\\SolrField',
);

/**
* @var MappingDriver
*/
private $parentDriver;

/**
* @param MappingDriver $driver
*/
public function setParentDriver(MappingDriver $driver)
{
$this->parentDriver = $driver;
}

/**
* @param string $className
Expand Down Expand Up @@ -207,4 +222,25 @@ private function addValuesToMetadata(array $reflectedClassProperties, ClassMetad

return $metadata;
}

public function getAllClassNames()
{
if ($this->classNames !== NULL) {
return $this->classNames;
}

if ($this->parentDriver === NULL) {
return parent::getAllClassNames();
}

$classes = array();
foreach ($this->parentDriver->getAllClassNames() as $className) {
if (!$this->isTransient($className)) {
$classes[] = $className;
}
}

return $this->classNames = $classes;
}

}
39 changes: 29 additions & 10 deletions lib/Doctrine/Search/SearchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SearchManager implements ObjectManager
/**
* @var ObjectManager
*/
private $entityManager;
private $objectManager;

/**
* The event manager that is the central point of the event system.
Expand Down Expand Up @@ -95,27 +95,25 @@ public function __construct(Configuration $config, SearchClientInterface $client
$this->metadataFactory->setCacheDriver($this->configuration->getMetadataCacheImpl());

$this->serializer = $this->configuration->getEntitySerializer();
$this->entityManager = $this->configuration->getEntityManager();
$this->objectManager = $this->configuration->getObjectManager();

$this->unitOfWork = new UnitOfWork($this);
}

/**
* Inject a Doctrine 2 object manager
*
* @param ObjectManager $om
* @param ObjectManager $objectManager
*/
public function setEntityManager(ObjectManager $om)
public function setObjectManager(ObjectManager $objectManager)
{
$this->entityManager = $om;
$this->objectManager = $objectManager;
}

/**
* @return ObjectManager|\Doctrine\ORM\EntityManager
* @return ObjectManager
*/
public function getEntityManager()
public function getObjectManager()
{
return $this->entityManager;
return $this->objectManager;
}

/**
Expand Down Expand Up @@ -332,4 +330,25 @@ public function detach($object)
public function refresh($object)
{
}


/**
* Inject a Doctrine 2 object manager
*
* @deprecated
* @param ObjectManager $om
*/
public function setEntityManager(ObjectManager $om)
{
$this->setObjectManager($om);
}

/**
* @deprecated
* @return ObjectManager|\Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
return $this->getObjectManager();
}
}