Merge pull request #695 from doctrine/RepositoryFactory
Implemented support for RepositoryFactory.
This commit is contained in:
commit
7903a2b513
@ -19,20 +19,22 @@
|
|||||||
|
|
||||||
namespace Doctrine\ORM;
|
namespace Doctrine\ORM;
|
||||||
|
|
||||||
use Doctrine\Common\Cache\Cache;
|
|
||||||
use Doctrine\Common\Cache\ArrayCache;
|
|
||||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
|
||||||
use Doctrine\Common\Annotations\AnnotationReader;
|
use Doctrine\Common\Annotations\AnnotationReader;
|
||||||
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
|
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||||
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
|
||||||
use Doctrine\ORM\Mapping\QuoteStrategy;
|
|
||||||
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
|
|
||||||
use Doctrine\ORM\Mapping\NamingStrategy;
|
|
||||||
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
|
|
||||||
use Doctrine\ORM\Mapping\EntityListenerResolver;
|
|
||||||
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
|
|
||||||
use Doctrine\Common\Annotations\SimpleAnnotationReader;
|
|
||||||
use Doctrine\Common\Annotations\CachedReader;
|
use Doctrine\Common\Annotations\CachedReader;
|
||||||
|
use Doctrine\Common\Annotations\SimpleAnnotationReader;
|
||||||
|
use Doctrine\Common\Cache\ArrayCache;
|
||||||
|
use Doctrine\Common\Cache\Cache;
|
||||||
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
|
||||||
|
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
|
||||||
|
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
|
||||||
|
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
|
||||||
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
||||||
|
use Doctrine\ORM\Mapping\EntityListenerResolver;
|
||||||
|
use Doctrine\ORM\Mapping\NamingStrategy;
|
||||||
|
use Doctrine\ORM\Mapping\QuoteStrategy;
|
||||||
|
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
|
||||||
|
use Doctrine\ORM\Repository\RepositoryFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration container for all configuration options of Doctrine.
|
* Configuration container for all configuration options of Doctrine.
|
||||||
@ -779,4 +781,28 @@ class Configuration extends \Doctrine\DBAL\Configuration
|
|||||||
|
|
||||||
return $this->_attributes['entityListenerResolver'];
|
return $this->_attributes['entityListenerResolver'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the entity repository factory.
|
||||||
|
*
|
||||||
|
* @since 2.4
|
||||||
|
* @param \Doctrine\ORM\Repository\RepositoryFactory $repositoryFactory
|
||||||
|
*/
|
||||||
|
public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
|
||||||
|
{
|
||||||
|
$this->_attributes['repositoryFactory'] = $repositoryFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the entity repository factory.
|
||||||
|
*
|
||||||
|
* @since 2.4
|
||||||
|
* @return \Doctrine\ORM\Repository\RepositoryFactory
|
||||||
|
*/
|
||||||
|
public function getRepositoryFactory()
|
||||||
|
{
|
||||||
|
return isset($this->_attributes['repositoryFactory'])
|
||||||
|
? $this->_attributes['repositoryFactory']
|
||||||
|
: new DefaultRepositoryFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,13 +85,6 @@ use Doctrine\Common\Util\ClassUtils;
|
|||||||
*/
|
*/
|
||||||
private $metadataFactory;
|
private $metadataFactory;
|
||||||
|
|
||||||
/**
|
|
||||||
* The EntityRepository instances.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $repositories = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The UnitOfWork used to coordinate object-level transactions.
|
* The UnitOfWork used to coordinate object-level transactions.
|
||||||
*
|
*
|
||||||
@ -120,6 +113,13 @@ use Doctrine\Common\Util\ClassUtils;
|
|||||||
*/
|
*/
|
||||||
private $proxyFactory;
|
private $proxyFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The repository factory used to create dynamic repositories.
|
||||||
|
*
|
||||||
|
* @var \Doctrine\ORM\Repository\RepositoryFactory
|
||||||
|
*/
|
||||||
|
private $repositoryFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The expression builder instance used to generate query expressions.
|
* The expression builder instance used to generate query expressions.
|
||||||
*
|
*
|
||||||
@ -151,9 +151,9 @@ use Doctrine\Common\Util\ClassUtils;
|
|||||||
*/
|
*/
|
||||||
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
|
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
|
||||||
{
|
{
|
||||||
$this->conn = $conn;
|
$this->conn = $conn;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->eventManager = $eventManager;
|
$this->eventManager = $eventManager;
|
||||||
|
|
||||||
$metadataFactoryClassName = $config->getClassMetadataFactoryName();
|
$metadataFactoryClassName = $config->getClassMetadataFactoryName();
|
||||||
|
|
||||||
@ -161,8 +161,9 @@ use Doctrine\Common\Util\ClassUtils;
|
|||||||
$this->metadataFactory->setEntityManager($this);
|
$this->metadataFactory->setEntityManager($this);
|
||||||
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());
|
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());
|
||||||
|
|
||||||
$this->unitOfWork = new UnitOfWork($this);
|
$this->repositoryFactory = $config->getRepositoryFactory();
|
||||||
$this->proxyFactory = new ProxyFactory(
|
$this->unitOfWork = new UnitOfWork($this);
|
||||||
|
$this->proxyFactory = new ProxyFactory(
|
||||||
$this,
|
$this,
|
||||||
$config->getProxyDir(),
|
$config->getProxyDir(),
|
||||||
$config->getProxyNamespace(),
|
$config->getProxyNamespace(),
|
||||||
@ -758,28 +759,11 @@ use Doctrine\Common\Util\ClassUtils;
|
|||||||
*
|
*
|
||||||
* @param string $entityName The name of the entity.
|
* @param string $entityName The name of the entity.
|
||||||
*
|
*
|
||||||
* @return EntityRepository The repository class.
|
* @return \Doctrine\ORM\EntityRepository The repository class.
|
||||||
*/
|
*/
|
||||||
public function getRepository($entityName)
|
public function getRepository($entityName)
|
||||||
{
|
{
|
||||||
$entityName = ltrim($entityName, '\\');
|
return $this->repositoryFactory->getRepository($this, $entityName);
|
||||||
|
|
||||||
if (isset($this->repositories[$entityName])) {
|
|
||||||
return $this->repositories[$entityName];
|
|
||||||
}
|
|
||||||
|
|
||||||
$metadata = $this->getClassMetadata($entityName);
|
|
||||||
$repositoryClassName = $metadata->customRepositoryClassName;
|
|
||||||
|
|
||||||
if ($repositoryClassName === null) {
|
|
||||||
$repositoryClassName = $this->config->getDefaultRepositoryClassName();
|
|
||||||
}
|
|
||||||
|
|
||||||
$repository = new $repositoryClassName($this, $metadata);
|
|
||||||
|
|
||||||
$this->repositories[$entityName] = $repository;
|
|
||||||
|
|
||||||
return $repository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
77
lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Normal file
77
lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?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\ORM\Repository;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This factory is used to create default repository objects for entities at runtime.
|
||||||
|
*
|
||||||
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||||
|
* @since 2.4
|
||||||
|
*/
|
||||||
|
class DefaultRepositoryFactory implements RepositoryFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The list of EntityRepository instances.
|
||||||
|
*
|
||||||
|
* @var array<\Doctrine\Common\Persistence\ObjectRepository>
|
||||||
|
*/
|
||||||
|
private $repositoryList = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getRepository(EntityManagerInterface $entityManager, $entityName)
|
||||||
|
{
|
||||||
|
$entityName = ltrim($entityName, '\\');
|
||||||
|
|
||||||
|
if (isset($this->repositoryList[$entityName])) {
|
||||||
|
return $this->repositoryList[$entityName];
|
||||||
|
}
|
||||||
|
|
||||||
|
$repository = $this->createRepository($entityManager, $entityName);
|
||||||
|
|
||||||
|
$this->repositoryList[$entityName] = $repository;
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new repository instance for an entity class.
|
||||||
|
*
|
||||||
|
* @param \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance.
|
||||||
|
* @param string $entityName The name of the entity.
|
||||||
|
*
|
||||||
|
* @return \Doctrine\Common\Persistence\ObjectRepository
|
||||||
|
*/
|
||||||
|
protected function createRepository(EntityManagerInterface $entityManager, $entityName)
|
||||||
|
{
|
||||||
|
$metadata = $entityManager->getClassMetadata($entityName);
|
||||||
|
$repositoryClassName = $metadata->customRepositoryClassName;
|
||||||
|
|
||||||
|
if ($repositoryClassName === null) {
|
||||||
|
$configuration = $entityManager->getConfiguration();
|
||||||
|
$repositoryClassName = $configuration->getDefaultRepositoryClassName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new $repositoryClassName($entityManager, $metadata);
|
||||||
|
}
|
||||||
|
}
|
41
lib/Doctrine/ORM/Repository/RepositoryFactory.php
Normal file
41
lib/Doctrine/ORM/Repository/RepositoryFactory.php
Normal file
@ -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\ORM\Repository;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for entity repository factory.
|
||||||
|
*
|
||||||
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||||
|
* @since 2.4
|
||||||
|
*/
|
||||||
|
interface RepositoryFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Gets the repository for an entity class.
|
||||||
|
*
|
||||||
|
* @param \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance.
|
||||||
|
* @param string $entityName The name of the entity.
|
||||||
|
*
|
||||||
|
* @return \Doctrine\Common\Persistence\ObjectRepository
|
||||||
|
*/
|
||||||
|
public function getRepository(EntityManagerInterface $entityManager, $entityName);
|
||||||
|
}
|
@ -19,18 +19,21 @@ class DDC2359Test extends \PHPUnit_Framework_TestCase
|
|||||||
$mockDriver = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\Driver\\MappingDriver');
|
$mockDriver = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\Driver\\MappingDriver');
|
||||||
$mockMetadata = $this->getMock('Doctrine\\ORM\\Mapping\\ClassMetadata', array(), array(), '', false);
|
$mockMetadata = $this->getMock('Doctrine\\ORM\\Mapping\\ClassMetadata', array(), array(), '', false);
|
||||||
$entityManager = $this->getMock('Doctrine\\ORM\\EntityManager', array(), array(), '', false);
|
$entityManager = $this->getMock('Doctrine\\ORM\\EntityManager', array(), array(), '', false);
|
||||||
|
|
||||||
/* @var $metadataFactory \Doctrine\ORM\Mapping\ClassMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
|
/* @var $metadataFactory \Doctrine\ORM\Mapping\ClassMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
$metadataFactory = $this->getMock(
|
$metadataFactory = $this->getMock(
|
||||||
'Doctrine\\ORM\\Mapping\\ClassMetadataFactory',
|
'Doctrine\\ORM\\Mapping\\ClassMetadataFactory',
|
||||||
array('newClassMetadataInstance', 'wakeupReflection')
|
array('newClassMetadataInstance', 'wakeupReflection')
|
||||||
);
|
);
|
||||||
$configuration = $this->getMock('Doctrine\\ORM\\Configuration');
|
|
||||||
|
$configuration = $this->getMock('Doctrine\\ORM\\Configuration', array('getMetadataDriverImpl'));
|
||||||
$connection = $this->getMock('Doctrine\\DBAL\\Connection', array(), array(), '', false);
|
$connection = $this->getMock('Doctrine\\DBAL\\Connection', array(), array(), '', false);
|
||||||
|
|
||||||
$configuration
|
$configuration
|
||||||
->expects($this->any())
|
->expects($this->any())
|
||||||
->method('getMetadataDriverImpl')
|
->method('getMetadataDriverImpl')
|
||||||
->will($this->returnValue($mockDriver));
|
->will($this->returnValue($mockDriver));
|
||||||
|
|
||||||
$entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
|
$entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
|
||||||
$entityManager->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
|
$entityManager->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
|
||||||
$entityManager
|
$entityManager
|
||||||
|
Loading…
x
Reference in New Issue
Block a user