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
|
|
|
use Doctrine\Common\Util\ClassUtils; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ObjectManagerService |
15
|
|
|
* |
16
|
|
|
* @package Nnx\Doctrine\Service |
17
|
|
|
*/ |
18
|
|
|
class ObjectManagerService implements ObjectManagerServiceInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Сервис позволяющий получить ObjectManager по имени класса |
23
|
|
|
* |
24
|
|
|
* @var ObjectManagerAutoDetectorInterface |
25
|
|
|
*/ |
26
|
|
|
protected $objectManagerAutoDetector; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Менеджер для создания сущностей по интерфейсу |
30
|
|
|
* |
31
|
|
|
* @var EntityManagerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $entityManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ObjectManagerService constructor. |
37
|
|
|
* |
38
|
|
|
* @param ObjectManagerAutoDetectorInterface $objectManagerAutoDetector |
39
|
|
|
* @param EntityManagerInterface $entityManager |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ObjectManagerAutoDetectorInterface $objectManagerAutoDetector, EntityManagerInterface $entityManager) |
42
|
|
|
{ |
43
|
|
|
$this->setObjectManagerAutoDetector($objectManagerAutoDetector); |
44
|
|
|
$this->setEntityManager($entityManager); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
* |
50
|
|
|
* @param $entityName |
51
|
|
|
* |
52
|
|
|
* @return ObjectRepository |
53
|
|
|
*/ |
54
|
|
|
public function getRepository($entityName) |
55
|
|
|
{ |
56
|
|
|
$resolvedEntityName = $this->getEntityManager()->getEntityClassByInterface($entityName); |
57
|
|
|
$objectManager = $this->getObjectManagerAutoDetector()->getObjectManagerByClassName($resolvedEntityName); |
58
|
|
|
return $objectManager->getRepository($resolvedEntityName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
* |
64
|
|
|
* @param mixed $entityObject |
65
|
|
|
* |
66
|
|
|
* @throws Exception\InvalidEntityObjectException |
67
|
|
|
*/ |
68
|
|
|
public function saveEntityObject($entityObject, $flagFlush = self::FLAG_PERSIST) |
69
|
|
|
{ |
70
|
|
|
if (!is_object($entityObject)) { |
71
|
|
|
$errMsg = sprintf('Entity type %s is invalid.', gettype($entityObject)); |
72
|
|
|
throw new Exception\InvalidEntityObjectException($errMsg); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$className = ClassUtils::getClass($entityObject); |
76
|
|
|
$objectManager = $this->getObjectManagerAutoDetector()->getObjectManagerByClassName($className); |
77
|
|
|
|
78
|
|
|
$objectManager->persist($entityObject); |
79
|
|
|
if ($flagFlush) { |
80
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
81
|
|
|
//@TODO На уровне интерфейса ObjectManager'a возможности передать сущность в flush нет, есть только в \Doctrine\ORM\EntityManager::flush. Убрать @noinspection, когда наведут порядок в Doctrine |
82
|
|
|
$objectManager->flush($flagFlush === self::FLAG_FLUSH_ALL ? null : $entityObject); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
* |
90
|
|
|
* @param string $entityName |
91
|
|
|
* |
92
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
93
|
|
|
* @throws \Nnx\Doctrine\Service\Exception\InvalidEntityObjectException |
94
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
95
|
|
|
*/ |
96
|
|
|
public function createEntityObject($entityName, array $options = []) |
97
|
|
|
{ |
98
|
|
|
return $this->getEntityManager()->get($entityName, $options); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Возвращает сервис позволяющий получить ObjectManager по имени класса |
103
|
|
|
* |
104
|
|
|
* @return ObjectManagerAutoDetectorInterface |
105
|
|
|
*/ |
106
|
|
|
public function getObjectManagerAutoDetector() |
107
|
|
|
{ |
108
|
|
|
return $this->objectManagerAutoDetector; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Устанавливает сервис позволяющий получить ObjectManager по имени класса |
113
|
|
|
* |
114
|
|
|
* @param ObjectManagerAutoDetectorInterface $objectManagerAutoDetector |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setObjectManagerAutoDetector(ObjectManagerAutoDetectorInterface $objectManagerAutoDetector) |
119
|
|
|
{ |
120
|
|
|
$this->objectManagerAutoDetector = $objectManagerAutoDetector; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Возвращает менеджер для создания сущностей по интерфейсу |
127
|
|
|
* |
128
|
|
|
* @return EntityManagerInterface |
129
|
|
|
*/ |
130
|
|
|
public function getEntityManager() |
131
|
|
|
{ |
132
|
|
|
return $this->entityManager; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Устанавливает менеджер для создания сущностей по интерфейсу |
137
|
|
|
* |
138
|
|
|
* @param EntityManagerInterface $entityManager |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager) |
143
|
|
|
{ |
144
|
|
|
$this->entityManager = $entityManager; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.