Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBundle] introduced a Registry class
The Registry knows all connections and entity managers declared into a service container. The Registry is available via the 'doctrine' service. If can be used to get connections and entity managers by name: Before: $em = $this->get('doctrine.orm.entity_manager'); $em = $this->get('doctrine.orm.foobar_entity_manager'); After: $em = $this->get('doctrine')->getEntityManager(); $em = $this->get('doctrine')->getEntityManager('foobar'); It is better for several reasons: * we rely less on naming conventions * IDEs will be able to auto-complete methods
- Loading branch information
Showing
20 changed files
with
276 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?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\Bundle\DoctrineBundle; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Doctrine\DBAL\Connection; | ||
|
||
/** | ||
* References all Doctrine connections and entity managers in a given Container. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class Registry | ||
{ | ||
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; | ||
|
||
if (!isset($this->connections[$defaultConnection])) { | ||
throw new \LogicException(sprintf('Default connection "%s" is not defined.', $defaultConnection)); | ||
} | ||
$this->defaultConnection = $defaultConnection; | ||
|
||
if (!isset($this->entityManagers[$defaultEntityManager])) { | ||
throw new \LogicException(sprintf('Default entity manager "%s" is not defined.', $defaultEntityManager)); | ||
} | ||
$this->defaultEntityManager = $defaultEntityManager; | ||
} | ||
|
||
/** | ||
* 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) { | ||
return $this->container->get($this->connections[$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 all connection names. | ||
* | ||
* @return array An array of connection names | ||
*/ | ||
public function getConnectionNames() | ||
{ | ||
return $this->connections; | ||
} | ||
|
||
/** | ||
* Gets the default entity manager name. | ||
* | ||
* @return string The default entity manager name | ||
*/ | ||
public function getDefaultEntityManagerName() | ||
{ | ||
return $this->defaultEntityManager; | ||
} | ||
|
||
/** | ||
* Gets the named entity manager. | ||
* | ||
* @param string $name The entity manager name (null for the default one) | ||
* | ||
* @return EntityManager | ||
*/ | ||
public function getEntityManager($name = null) | ||
{ | ||
if (null === $name) { | ||
return $this->container->get($this->entityManagers[$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]); | ||
} | ||
|
||
/** | ||
* Gets all connection names. | ||
* | ||
* @return array An array of connection names | ||
*/ | ||
public function getEntityManagerNames() | ||
{ | ||
return $this->entityManagers; | ||
} | ||
} |
Oops, something went wrong.
6b5438a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's really better.
6b5438a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on this one. It will definitely makes sf2 easier to learn and to develop with.