|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Drest package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Lee Davis |
|
9
|
|
|
* @copyright Copyright (c) Lee Davis <@leedavis81> |
|
10
|
|
|
* @link https://github.com/leedavis81/drest/blob/master/LICENSE |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT X License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Drest; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\Common\Persistence\AbstractManagerRegistry; |
|
16
|
|
|
use Doctrine\ORM\EntityManager; |
|
17
|
|
|
use Doctrine\ORM\ORMException; |
|
18
|
|
|
/** |
|
19
|
|
|
* Drest EntityManagerRegistry |
|
20
|
|
|
*/ |
|
21
|
|
|
class EntityManagerRegistry extends AbstractManagerRegistry |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Simple array container |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $container; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Fetches/creates the given services. |
|
31
|
|
|
* |
|
32
|
|
|
* A service in this context is connection or a manager instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name The name of the service. |
|
35
|
|
|
* @return object The instance of the given service. |
|
36
|
|
|
*/ |
|
37
|
26 |
|
protected function getService($name) |
|
38
|
|
|
{ |
|
39
|
26 |
|
if (!isset($this->container[$name])) |
|
40
|
26 |
|
{ |
|
41
|
|
|
throw new \InvalidArgumentException(sprintf('Service named "%s" does not exist.', $name)); |
|
42
|
|
|
} |
|
43
|
26 |
|
return $this->container[$name]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Resets the given services. |
|
48
|
|
|
* |
|
49
|
|
|
* A service in this context is connection or a manager instance. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $name The name of the service. |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function resetService($name) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->container[$name] = null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Resolves a registered namespace alias to the full namespace. |
|
61
|
|
|
* |
|
62
|
|
|
* This method looks for the alias in all registered object managers. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $alias The alias. |
|
65
|
|
|
* @return string The full namespace. |
|
66
|
|
|
* @throws ORMException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getAliasNamespace($alias) |
|
69
|
|
|
{ |
|
70
|
|
|
foreach (array_keys($this->getManagers()) as $name) { |
|
71
|
|
|
try { |
|
72
|
|
|
if (($em = $this->getManager($name)) instanceof EntityManager) |
|
73
|
|
|
{ |
|
74
|
|
|
return $em->getConfiguration()->getEntityNamespace($alias); |
|
75
|
|
|
} |
|
76
|
|
|
} catch (ORMException $e) { |
|
77
|
|
|
// If any exception is throw when attempting to retrieve then have our custom one thrown |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
throw ORMException::unknownEntityNamespace($alias); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set the service container |
|
86
|
|
|
* @param $container |
|
87
|
|
|
*/ |
|
88
|
31 |
|
public function setContainer($container) |
|
89
|
|
|
{ |
|
90
|
31 |
|
$this->container = $container; |
|
91
|
31 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get a simple manager registry if you only use one $em |
|
95
|
|
|
* It's advised you either extend or implement your own version of AbstractManagerRegistry |
|
96
|
|
|
* for custom handling of varied services |
|
97
|
|
|
* @param EntityManager $em |
|
98
|
|
|
* @return EntityManagerRegistry |
|
99
|
|
|
*/ |
|
100
|
31 |
|
public static function getSimpleManagerRegistry(EntityManager $em) |
|
101
|
|
|
{ |
|
102
|
31 |
|
$registry = new self( |
|
103
|
31 |
|
'drestApp', |
|
104
|
31 |
|
[], |
|
105
|
31 |
|
array('defaultManager' => 'default'), |
|
106
|
31 |
|
null, |
|
107
|
31 |
|
'defaultManager', |
|
108
|
|
|
'\Doctrine\ORM\Proxy\Proxy' |
|
109
|
31 |
|
); |
|
110
|
31 |
|
$registry->setContainer(['default' => $em]); |
|
111
|
31 |
|
return $registry; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |