merged branch lsmith77/ManagerRegistry (PR #2244)

Commits
-------

dc5772d use ORM master
4364463 use doctrine-common master
55b572d fixed getting the alias for a namespace
2b89e15 use getObjectNamespace() in getEntityNamespace()
0217a0e updated base class name
e8f3c21 updated vendors to point to lsmith77's fork of doctrine-common until its merged
6e87d01 fix tests
13c2f33 added a default implementation of the ManagerRegistry integrating the container

Discussion
----------

[Doctrine] added a default implementation of the ManagerRegistry

Bug fix: no
Feature addition: yes
Backwards compatibility break: yes (minor change in the interface see below)
Symfony2 tests pass: yes
Fixes the following tickets: -

added a default implementation of the ManagerRegistry integrating the container

attempted to maintain BC as good as possible, but RegistryInterface::getRepository() had to be dropped from RegistryInterface. Its still part of the ManagerRegistry, so its only a BC break for people using RegistryInterface to create their own implementation as I ran into https://bugs.php.net/bug.php?id=43200

all implementation (ORM/ODM) will need to match the changes to the ClassMetadataFactory interface

ORM, PHPCR, CouchDB have been upgraded already.
The Bundles also need to be updated. ORM is covered with this PR, I have a PR ready for PHPCR:
https://github.com/symfony-cmf/symfony-cmf/pull/108

also note that before merging the change to vendors.php needs to be fixed to point to the right repo again

For MongoDB it currently does not yet have a registry and I can take care of CouchDB once this is all merged.

---------------------------------------------------------------------------

by lsmith77 at 2011/09/23 00:40:07 -0700

still a few failing tests and details still need to be discussed ..

---------------------------------------------------------------------------

by lsmith77 at 2011/09/23 00:53:23 -0700

ok .. tests are passing now

---------------------------------------------------------------------------

by lsmith77 at 2011/10/11 10:27:52 -0700

ok Doctrine/ORM updates are done .. PR updated .. ready to be merged.
This commit is contained in:
Fabien Potencier 2011-10-15 03:38:50 +02:00
commit ec45893c2d
5 changed files with 93 additions and 167 deletions

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Doctrine;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\AbstractManagerRegistry;
/**
* References Doctrine connections and entity/document managers.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
abstract class ManagerRegistry extends AbstractManagerRegistry implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @inheritdoc
*/
protected function getService($name)
{
return $this->container->get($name);
}
/**
* @inheritdoc
*/
protected function resetService($name)
{
$this->container->set($name, null);
}
/**
* @inheritdoc
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}

View File

@ -15,43 +15,15 @@ use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\ORMException;
use Doctrine\Common\Persistence\ConnectionRegistry as ConnectionRegistryInterface;
/**
* References Doctrine connections and entity managers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RegistryInterface
interface RegistryInterface extends ConnectionRegistryInterface
{
/**
* Gets the default connection name.
*
* @return string The default connection name
*/
function getDefaultConnectionName();
/**
* Gets the named connection.
*
* @param string $name The connection name (null for the default one)
*
* @return Connection
*/
function getConnection($name = null);
/**
* Gets an array of all registered connections
*
* @return array An array of Connection instances
*/
function getConnections();
/**
* Gets all connection names.
*
* @return array An array of connection names
*/
function getConnectionNames();
/**
* Gets the default entity manager name.
*
@ -114,16 +86,6 @@ interface RegistryInterface
*/
function getEntityManagerNames();
/**
* Gets the EntityRepository for an entity.
*
* @param string $entityName The name of the entity.
* @param string $entityManagerNAme The entity manager name (null for the default one)
*
* @return Doctrine\ORM\EntityRepository
*/
function getRepository($entityName, $entityManagerName = null);
/**
* Gets the entity manager associated with a given class.
*

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\DoctrineBundle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\ORMException;
@ -22,86 +23,25 @@ use Doctrine\ORM\ORMException;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Registry implements RegistryInterface
class Registry extends ManagerRegistry implements RegistryInterface
{
private $container;
private $connections;
private $entityManagers;
private $defaultConnection;
private $defaultEntityManager;
public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
{
$this->container = $container;
$this->connections = $connections;
$this->entityManagers = $entityManagers;
$this->defaultConnection = $defaultConnection;
$this->defaultEntityManager = $defaultEntityManager;
}
$this->setContainer($container);
/**
* Gets the default connection name.
*
* @return string The default connection name
*/
public function getDefaultConnectionName()
{
return $this->defaultConnection;
}
/**
* Gets the named connection.
*
* @param string $name The connection name (null for the default one)
*
* @return Connection
*/
public function getConnection($name = null)
{
if (null === $name) {
$name = $this->defaultConnection;
}
if (!isset($this->connections[$name])) {
throw new \InvalidArgumentException(sprintf('Doctrine Connection named "%s" does not exist.', $name));
}
return $this->container->get($this->connections[$name]);
}
/**
* Gets an array of all registered connections
*
* @return array An array of Connection instances
*/
public function getConnections()
{
$connections = array();
foreach ($this->connections as $name => $id) {
$connections[$name] = $this->container->get($id);
}
return $connections;
}
/**
* Gets all connection names.
*
* @return array An array of connection names
*/
public function getConnectionNames()
{
return $this->connections;
parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
}
/**
* Gets the default entity manager name.
*
* @return string The default entity manager name
*
* @deprecated
*/
public function getDefaultEntityManagerName()
{
return $this->defaultEntityManager;
return $this->getDefaultManagerName();
}
/**
@ -110,33 +50,24 @@ class Registry implements RegistryInterface
* @param string $name The entity manager name (null for the default one)
*
* @return EntityManager
*
* @deprecated
*/
public function getEntityManager($name = null)
{
if (null === $name) {
$name = $this->defaultEntityManager;
}
if (!isset($this->entityManagers[$name])) {
throw new \InvalidArgumentException(sprintf('Doctrine EntityManager named "%s" does not exist.', $name));
}
return $this->container->get($this->entityManagers[$name]);
return $this->getManager($name);
}
/**
* Gets an array of all registered entity managers
*
* @return array An array of EntityManager instances
*
* @deprecated
*/
public function getEntityManagers()
{
$ems = array();
foreach ($this->entityManagers as $name => $id) {
$ems[$name] = $this->container->get($id);
}
return $ems;
return $this->getManagers();
}
/**
@ -158,17 +89,21 @@ class Registry implements RegistryInterface
*/
public function resetEntityManager($name = null)
{
if (null === $name) {
$name = $this->defaultEntityManager;
}
$this->resetManager($name);
}
if (!isset($this->entityManagers[$name])) {
throw new \InvalidArgumentException(sprintf('Doctrine EntityManager named "%s" does not exist.', $name));
}
// force the creation of a new entity manager
// if the current one is closed
$this->container->set($this->entityManagers[$name], null);
/**
* Resolves a registered namespace alias to the full namespace.
*
* This method looks for the alias in all registered entity managers.
*
* @param string $alias The alias
*
* @return string The full namespace
*/
public function getEntityNamespace($alias)
{
return $this->getAliasNamespace($alias);
}
/**
@ -182,11 +117,11 @@ class Registry implements RegistryInterface
*
* @see Configuration::getEntityNamespace
*/
public function getEntityNamespace($alias)
public function getAliasNamespace($alias)
{
foreach (array_keys($this->entityManagers) as $name) {
foreach (array_keys($this->getManagers()) as $name) {
try {
return $this->getEntityManager($name)->getConfiguration()->getEntityNamespace($alias);
return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
} catch (ORMException $e) {
}
}
@ -201,20 +136,7 @@ class Registry implements RegistryInterface
*/
public function getEntityManagerNames()
{
return $this->entityManagers;
}
/**
* Gets the EntityRepository for an entity.
*
* @param string $entityName The name of the entity.
* @param string $entityManagerNAme The entity manager name (null for the default one)
*
* @return Doctrine\ORM\EntityRepository
*/
public function getRepository($entityName, $entityManagerName = null)
{
return $this->getEntityManager($entityManagerName)->getRepository($entityName);
return $this->getManagerNames();
}
/**
@ -226,17 +148,6 @@ class Registry implements RegistryInterface
*/
public function getEntityManagerForClass($class)
{
$proxyClass = new \ReflectionClass($class);
if ($proxyClass->implementsInterface('Doctrine\ORM\Proxy\Proxy')) {
$class = $proxyClass->getParentClass()->getName();
}
foreach ($this->entityManagers as $id) {
$em = $this->container->get($id);
if (!$em->getConfiguration()->getMetadataDriverImpl()->isTransient($class)) {
return $em;
}
}
return $this->getManagerForClass($class);
}
}

View File

@ -64,7 +64,7 @@ class RegistryTest extends TestCase
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$registry = new Registry($container, array(), array(), 'default', 'default');
$this->setExpectedException('InvalidArgumentException', 'Doctrine Connection named "default" does not exist.');
$this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Connection named "default" does not exist.');
$registry->getConnection('default');
}
@ -109,7 +109,7 @@ class RegistryTest extends TestCase
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$registry = new Registry($container, array(), array(), 'default', 'default');
$this->setExpectedException('InvalidArgumentException', 'Doctrine EntityManager named "default" does not exist.');
$this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Manager named "default" does not exist.');
$registry->getEntityManager('default');
}
@ -140,7 +140,7 @@ class RegistryTest extends TestCase
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$registry = new Registry($container, array(), array(), 'default', 'default');
$this->setExpectedException('InvalidArgumentException', 'Doctrine EntityManager named "default" does not exist.');
$this->setExpectedException('InvalidArgumentException', 'Doctrine ORM Manager named "default" does not exist.');
$registry->resetEntityManager('default');
}
}

View File

@ -26,9 +26,9 @@ if (!is_dir($vendorDir = dirname(__FILE__).'/vendor')) {
}
$deps = array(
array('doctrine', 'http://github.com/doctrine/doctrine2.git', 'origin/2.1.x'),
array('doctrine', 'http://github.com/doctrine/doctrine2.git', 'origin/master'),
array('doctrine-dbal', 'http://github.com/doctrine/dbal.git', 'origin/2.1.x'),
array('doctrine-common', 'http://github.com/doctrine/common.git', 'origin/2.1.x'),
array('doctrine-common', 'http://github.com/doctrine/common.git', 'origin/master'),
array('monolog', 'http://github.com/Seldaek/monolog.git', '1.0.1'),
array('swiftmailer', 'http://github.com/swiftmailer/swiftmailer.git', 'v4.1.2'),
array('twig', 'http://github.com/fabpot/Twig.git', 'v1.2.0'),