-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9b95af
commit 565e563
Showing
3 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\ORM; | ||
|
||
use Doctrine\Common\Persistence\AbstractManagerRegistry; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\ORMException; | ||
use Doctrine\ORM\Proxy\Proxy; | ||
use Nette\DI\Container; | ||
|
||
class ManagerRegistry extends AbstractManagerRegistry | ||
{ | ||
|
||
/** @var Container */ | ||
private $container; | ||
|
||
/** | ||
* @param Connection $connection | ||
* @param EntityManager $em | ||
* @param Container $container | ||
*/ | ||
public function __construct(Connection $connection, EntityManager $em, Container $container) | ||
{ | ||
$defaultConnection = $container->findByType(get_class($connection))[0]; | ||
$defaultManager = $container->findByType(get_class($em))[0]; | ||
|
||
$connections = ['default' => $defaultConnection]; | ||
$managers = ['default' => $defaultManager]; | ||
|
||
parent::__construct('ORM', $connections, $managers, 'default', 'default', Proxy::class); | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return object | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
protected function getService($name): object | ||
{ | ||
return $this->container->getService($name); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return void | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
protected function resetService($name): void | ||
{ | ||
$this->container->removeService($name); | ||
} | ||
|
||
/** | ||
* @param string $alias | ||
* @return string | ||
* @throws ORMException | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function getAliasNamespace($alias): string | ||
{ | ||
foreach (array_keys($this->getManagers()) as $name) { | ||
try { | ||
/** @var EntityManager $entityManager */ | ||
$entityManager = $this->getManager($name); | ||
return $entityManager->getConfiguration()->getEntityNamespace($alias); | ||
} catch (ORMException $e) { | ||
// Ignore | ||
} | ||
} | ||
|
||
throw ORMException::unknownEntityNamespace($alias); | ||
} | ||
|
||
} |