Registry::resetService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine;
12
13
use Doctrine\Common\Persistence\AbstractManagerRegistry;
14
use Doctrine\ORM\ORMException;
15
use Kdyby;
16
use Nette;
17
18
19
20
/**
21
 * @author Filip Procházka <[email protected]>
22
 *
23
 * @method EntityManager getManager($name = NULL)
24
 * @method EntityManager getManagerForClass($class)
25
 * @method EntityManager[] getManagers()
26
 * @method EntityRepository getRepository($persistentObjectName, $persistentManagerName = NULL)
27
 * @method Connection getConnection($name = NULL)
28
 * @method Connection[] getConnections()
29
 */
30
class Registry extends AbstractManagerRegistry
31
{
32
33
	use \Kdyby\StrictObjects\Scream;
34
35
	/**
36
	 * @var Nette\DI\Container
37
	 */
38
	private $serviceLocator;
39
40
	/**
41
	 * @param array $connections
42
	 * @param array $managers
43
	 * @param string $defaultConnection
44
	 * @param string $defaultManager
45
	 * @param \Nette\DI\Container $serviceLocator
46
	 */
47
	public function __construct(array $connections, array $managers, $defaultConnection, $defaultManager, Nette\DI\Container $serviceLocator)
48
	{
49
		parent::__construct('ORM', $connections, $managers, $defaultConnection, $defaultManager, \Doctrine\ORM\Proxy\Proxy::class);
50
		$this->serviceLocator = $serviceLocator;
51
	}
52
53
54
55
	/**
56
	 * Fetches/creates the given services.
57
	 *
58
	 * A service in this context is connection or a manager instance.
59
	 *
60
	 * @param string $name The name of the service.
61
	 * @return object The instance of the given service.
62
	 */
63
	protected function getService($name)
64
	{
65
		return $this->serviceLocator->getService($name);
66
	}
67
68
69
70
	/**
71
	 * Resets the given services.
72
	 *
73
	 * A service in this context is connection or a manager instance.
74
	 *
75
	 * @param string $name The name of the service.
76
	 * @return void
77
	 */
78
	protected function resetService($name)
79
	{
80
		$this->serviceLocator->removeService($name);
81
	}
82
83
84
85
	/**
86
	 * Resolves a registered namespace alias to the full namespace.
87
	 *
88
	 * This method looks for the alias in all registered entity managers.
89
	 *
90
	 * @see \Doctrine\ORM\Configuration::getEntityNamespace
91
	 * @param string $alias The alias
92
	 * @throws \Doctrine\ORM\ORMException
93
	 * @return string The full namespace
94
	 */
95
	public function getAliasNamespace($alias)
96
	{
97
		foreach (array_keys($this->getManagers()) as $name) {
98
			try {
99
				/** @var \Doctrine\ORM\EntityManager $entityManager */
100
				$entityManager = $this->getManager($name);
101
				return $entityManager->getConfiguration()->getEntityNamespace($alias);
102
			} catch (ORMException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
			}
104
		}
105
106
		throw ORMException::unknownEntityNamespace($alias);
107
	}
108
109
}
110