|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\Registry; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; |
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
17
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Collection\ObjectManagerCollectionInterface; |
|
18
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Manager\DoctrineEntityManager; |
|
19
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Manager\ManagerInterface; |
|
20
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Manager\ObjectManager; |
|
21
|
|
|
use Sauls\Component\Collection\ArrayCollection; |
|
22
|
|
|
|
|
23
|
|
|
class ObjectRegistry implements RegistryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ObjectManagerCollectionInterface|ArrayCollection |
|
27
|
|
|
*/ |
|
28
|
|
|
private $objectManagerCollection; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ClassMetadataFactory |
|
31
|
|
|
*/ |
|
32
|
|
|
private $classMetadataFactory; |
|
33
|
|
|
|
|
34
|
3 |
|
public function __construct( |
|
35
|
|
|
ObjectManagerCollectionInterface $objectManagerCollection, |
|
36
|
|
|
EntityManagerInterface $entityManager |
|
37
|
|
|
) { |
|
38
|
3 |
|
$this->objectManagerCollection = $objectManagerCollection; |
|
39
|
3 |
|
$this->classMetadataFactory = $entityManager->getMetadataFactory(); |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
public function getManager(string $class): ManagerInterface |
|
43
|
|
|
{ |
|
44
|
3 |
|
if ($this->hasConcreteManager($class)) { |
|
45
|
1 |
|
return $this->configureManager( |
|
46
|
1 |
|
$class, |
|
47
|
1 |
|
$this->objectManagerCollection->get($class) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
if ($this->isDoctrineObject($class)) { |
|
52
|
1 |
|
return $this->configureManager( |
|
53
|
1 |
|
$class, |
|
54
|
1 |
|
$this->objectManagerCollection->get(DoctrineEntityManager::DOCTRINE_OBJECT_MANAGER_NAME) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return $this->configureManager( |
|
59
|
1 |
|
$class, |
|
60
|
1 |
|
$this->objectManagerCollection->get(ObjectManager::DEFAULT_OBJECT_MANAGER_NAME) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
public function hasConcreteManager(string $class): bool |
|
65
|
|
|
{ |
|
66
|
3 |
|
return $this->objectManagerCollection->keyExists($class); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
private function configureManager(string $class, ManagerInterface $manager): ManagerInterface |
|
70
|
|
|
{ |
|
71
|
3 |
|
$manager->setObjectClass($class); |
|
72
|
3 |
|
return $manager; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function isDoctrineObject(string $class): bool |
|
76
|
|
|
{ |
|
77
|
2 |
|
return false === $this->classMetadataFactory->isTransient($class); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|