mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
AbstractManagerRegistry.php
Event
9 months ago
Mapping
8 months ago
Reflection
9 months ago
AbstractManagerRegistry.php
9 months ago
ConnectionRegistry.php
9 months ago
ManagerRegistry.php
9 months ago
NotifyPropertyChanged.php
9 months ago
ObjectManager.php
9 months ago
ObjectManagerDecorator.php
9 months ago
ObjectRepository.php
9 months ago
PropertyChangedListener.php
9 months ago
Proxy.php
9 months ago
index.php
3 years ago
AbstractManagerRegistry.php
135 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\Persistence; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use InvalidArgumentException; |
| 6 | use ReflectionClass; |
| 7 | use function assert; |
| 8 | use function sprintf; |
| 9 | abstract class AbstractManagerRegistry implements ManagerRegistry |
| 10 | { |
| 11 | private $name; |
| 12 | private $connections; |
| 13 | private $managers; |
| 14 | private $defaultConnection; |
| 15 | private $defaultManager; |
| 16 | private $proxyInterfaceName; |
| 17 | public function __construct(string $name, array $connections, array $managers, string $defaultConnection, string $defaultManager, string $proxyInterfaceName) |
| 18 | { |
| 19 | $this->name = $name; |
| 20 | $this->connections = $connections; |
| 21 | $this->managers = $managers; |
| 22 | $this->defaultConnection = $defaultConnection; |
| 23 | $this->defaultManager = $defaultManager; |
| 24 | $this->proxyInterfaceName = $proxyInterfaceName; |
| 25 | } |
| 26 | protected abstract function getService(string $name); |
| 27 | protected abstract function resetService(string $name); |
| 28 | public function getName() |
| 29 | { |
| 30 | return $this->name; |
| 31 | } |
| 32 | public function getConnection(?string $name = null) |
| 33 | { |
| 34 | if ($name === null) { |
| 35 | $name = $this->defaultConnection; |
| 36 | } |
| 37 | if (!isset($this->connections[$name])) { |
| 38 | throw new InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name)); |
| 39 | } |
| 40 | return $this->getService($this->connections[$name]); |
| 41 | } |
| 42 | public function getConnectionNames() |
| 43 | { |
| 44 | return $this->connections; |
| 45 | } |
| 46 | public function getConnections() |
| 47 | { |
| 48 | $connections = []; |
| 49 | foreach ($this->connections as $name => $id) { |
| 50 | $connections[$name] = $this->getService($id); |
| 51 | } |
| 52 | return $connections; |
| 53 | } |
| 54 | public function getDefaultConnectionName() |
| 55 | { |
| 56 | return $this->defaultConnection; |
| 57 | } |
| 58 | public function getDefaultManagerName() |
| 59 | { |
| 60 | return $this->defaultManager; |
| 61 | } |
| 62 | public function getManager(?string $name = null) |
| 63 | { |
| 64 | if ($name === null) { |
| 65 | $name = $this->defaultManager; |
| 66 | } |
| 67 | if (!isset($this->managers[$name])) { |
| 68 | throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)); |
| 69 | } |
| 70 | $service = $this->getService($this->managers[$name]); |
| 71 | assert($service instanceof ObjectManager); |
| 72 | return $service; |
| 73 | } |
| 74 | public function getManagerForClass(string $class) |
| 75 | { |
| 76 | $proxyClass = new ReflectionClass($class); |
| 77 | if ($proxyClass->isAnonymous()) { |
| 78 | return null; |
| 79 | } |
| 80 | if ($proxyClass->implementsInterface($this->proxyInterfaceName)) { |
| 81 | $parentClass = $proxyClass->getParentClass(); |
| 82 | if ($parentClass === \false) { |
| 83 | return null; |
| 84 | } |
| 85 | $class = $parentClass->getName(); |
| 86 | } |
| 87 | foreach ($this->managers as $id) { |
| 88 | $manager = $this->getService($id); |
| 89 | assert($manager instanceof ObjectManager); |
| 90 | if (!$manager->getMetadataFactory()->isTransient($class)) { |
| 91 | return $manager; |
| 92 | } |
| 93 | } |
| 94 | return null; |
| 95 | } |
| 96 | public function getManagerNames() |
| 97 | { |
| 98 | return $this->managers; |
| 99 | } |
| 100 | public function getManagers() |
| 101 | { |
| 102 | $managers = []; |
| 103 | foreach ($this->managers as $name => $id) { |
| 104 | $manager = $this->getService($id); |
| 105 | assert($manager instanceof ObjectManager); |
| 106 | $managers[$name] = $manager; |
| 107 | } |
| 108 | return $managers; |
| 109 | } |
| 110 | public function getRepository(string $persistentObject, ?string $persistentManagerName = null) |
| 111 | { |
| 112 | return $this->selectManager($persistentObject, $persistentManagerName)->getRepository($persistentObject); |
| 113 | } |
| 114 | public function resetManager(?string $name = null) |
| 115 | { |
| 116 | if ($name === null) { |
| 117 | $name = $this->defaultManager; |
| 118 | } |
| 119 | if (!isset($this->managers[$name])) { |
| 120 | throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)); |
| 121 | } |
| 122 | // force the creation of a new document manager |
| 123 | // if the current one is closed |
| 124 | $this->resetService($this->managers[$name]); |
| 125 | return $this->getManager($name); |
| 126 | } |
| 127 | private function selectManager(string $persistentObject, ?string $persistentManagerName = null) : ObjectManager |
| 128 | { |
| 129 | if ($persistentManagerName !== null) { |
| 130 | return $this->getManager($persistentManagerName); |
| 131 | } |
| 132 | return $this->getManagerForClass($persistentObject) ?? $this->getManager(); |
| 133 | } |
| 134 | } |
| 135 |