1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/doctrine |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\Doctrine\Service; |
7
|
|
|
|
8
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
9
|
|
|
use Nnx\Doctrine\EntityManager\EntityManagerInterface; |
10
|
|
|
use Nnx\Doctrine\ObjectManager\ObjectManagerAutoDetectorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ObjectManagerService |
14
|
|
|
* |
15
|
|
|
* @package Nnx\Doctrine\Service |
16
|
|
|
*/ |
17
|
|
|
class ObjectManagerService implements ObjectManagerServiceInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Сервис позволяющий получить ObjectManager по имени класса |
22
|
|
|
* |
23
|
|
|
* @var ObjectManagerAutoDetectorInterface |
24
|
|
|
*/ |
25
|
|
|
protected $objectManagerAutoDetector; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Менеджер для создания сущностей по интерфейсу |
29
|
|
|
* |
30
|
|
|
* @var EntityManagerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $entityManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ObjectManagerService constructor. |
36
|
|
|
* |
37
|
|
|
* @param ObjectManagerAutoDetectorInterface $objectManagerAutoDetector |
38
|
|
|
* @param EntityManagerInterface $entityManager |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ObjectManagerAutoDetectorInterface $objectManagerAutoDetector, EntityManagerInterface $entityManager) |
41
|
|
|
{ |
42
|
|
|
$this->setObjectManagerAutoDetector($objectManagerAutoDetector); |
43
|
|
|
$this->setEntityManager($entityManager); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
* |
49
|
|
|
* @param $entityName |
50
|
|
|
* |
51
|
|
|
* @return ObjectRepository |
52
|
|
|
*/ |
53
|
|
|
public function getRepository($entityName) |
54
|
|
|
{ |
55
|
|
|
$resolvedEntityName = $this->getEntityManager()->getEntityClassByInterface($entityName); |
56
|
|
|
$objectManager = $this->getObjectManagerAutoDetector()->getObjectManagerByClassName($resolvedEntityName); |
57
|
|
|
return $objectManager->getRepository($resolvedEntityName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* |
63
|
|
|
* @param mixed $entityObject |
64
|
|
|
* |
65
|
|
|
* @throws Exception\InvalidEntityObjectException |
66
|
|
|
*/ |
67
|
|
|
public function saveEntityObject($entityObject) |
68
|
|
|
{ |
69
|
|
|
if (!is_object($entityObject)) { |
70
|
|
|
$errMsg = sprintf('Entity type %s is invalid.', gettype($entityObject)); |
71
|
|
|
throw new Exception\InvalidEntityObjectException($errMsg); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$className = get_class($entityObject); |
75
|
|
|
$objectManager = $this->getObjectManagerAutoDetector()->getObjectManagerByClassName($className); |
76
|
|
|
|
77
|
|
|
$objectManager->persist($entityObject); |
78
|
|
|
$objectManager->flush(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
* |
85
|
|
|
* @param string $entityName |
86
|
|
|
* |
87
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
88
|
|
|
* @throws \Nnx\Doctrine\Service\Exception\InvalidEntityObjectException |
89
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
90
|
|
|
*/ |
91
|
|
|
public function createEntityObject($entityName, array $options = []) |
92
|
|
|
{ |
93
|
|
|
return $this->getEntityManager()->get($entityName, $options); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Возвращает сервис позволяющий получить ObjectManager по имени класса |
98
|
|
|
* |
99
|
|
|
* @return ObjectManagerAutoDetectorInterface |
100
|
|
|
*/ |
101
|
|
|
public function getObjectManagerAutoDetector() |
102
|
|
|
{ |
103
|
|
|
return $this->objectManagerAutoDetector; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Устанавливает сервис позволяющий получить ObjectManager по имени класса |
108
|
|
|
* |
109
|
|
|
* @param ObjectManagerAutoDetectorInterface $objectManagerAutoDetector |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setObjectManagerAutoDetector(ObjectManagerAutoDetectorInterface $objectManagerAutoDetector) |
114
|
|
|
{ |
115
|
|
|
$this->objectManagerAutoDetector = $objectManagerAutoDetector; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Возвращает менеджер для создания сущностей по интерфейсу |
122
|
|
|
* |
123
|
|
|
* @return EntityManagerInterface |
124
|
|
|
*/ |
125
|
|
|
public function getEntityManager() |
126
|
|
|
{ |
127
|
|
|
return $this->entityManager; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Устанавливает менеджер для создания сущностей по интерфейсу |
132
|
|
|
* |
133
|
|
|
* @param EntityManagerInterface $entityManager |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager) |
138
|
|
|
{ |
139
|
|
|
$this->entityManager = $entityManager; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.