1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\StructureBundle\Manager; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Starkerxp\StructureBundle\Entity\Entity; |
9
|
|
|
use Starkerxp\StructureBundle\Manager\Exception\ObjectClassNotAllowedException; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
|
12
|
|
|
abstract class AbstractManager implements ManagerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var EntityManager */ |
15
|
|
|
protected $entityManager; |
16
|
|
|
|
17
|
|
|
/** @var EntityRepository */ |
18
|
|
|
protected $repository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param EntityManager $entityManager |
22
|
|
|
* @param $entity |
23
|
|
|
*/ |
24
|
|
|
public function __construct(EntityManager $entityManager, $entity) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->entityManager = $entityManager; |
27
|
|
|
$this->repository = $this->entityManager->getRepository($entity); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Entity $object |
32
|
|
|
* |
33
|
|
|
* @return Entity|boolean |
34
|
|
|
* |
35
|
|
|
* @throws ObjectClassNotAllowedException |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function insert(Entity $object) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (!$this->getSupport($object)) { |
40
|
|
|
throw new ObjectClassNotAllowedException(); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
$object->setCreatedAt(new DateTime()); |
44
|
|
|
$this->entityManager->persist($object); |
45
|
|
|
$this->entityManager->flush(); |
46
|
|
|
|
47
|
|
|
return $object; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Entity $object |
52
|
|
|
* |
53
|
|
|
* @return Entity|boolean |
54
|
|
|
* |
55
|
|
|
* @throws ObjectClassNotAllowedException |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function update(Entity $object) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (!$this->getSupport($object)) { |
60
|
|
|
throw new ObjectClassNotAllowedException(); |
61
|
|
|
} |
62
|
|
|
$object->setUpdatedAt(new DateTime()); |
63
|
|
|
$this->entityManager->flush(); |
64
|
|
|
|
65
|
|
|
return $object; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Finds an entity by its primary key / identifier. |
70
|
|
|
* |
71
|
|
|
* @param mixed $id The identifier. |
72
|
|
|
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
73
|
|
|
* or NULL if no specific lock mode should be used |
74
|
|
|
* during the search. |
75
|
|
|
* @param int|null $lockVersion The lock version. |
76
|
|
|
* |
77
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
78
|
|
|
*/ |
79
|
|
|
public function find($id, $lockMode = null, $lockVersion = null) |
80
|
|
|
{ |
81
|
|
|
return $this->repository->find($id, $lockMode, $lockVersion); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Finds a single entity by a set of criteria. |
86
|
|
|
* |
87
|
|
|
* @param array $criteria |
88
|
|
|
* @param array|null $orderBy |
89
|
|
|
* |
90
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
91
|
|
|
*/ |
92
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
93
|
|
|
{ |
94
|
|
|
return $this->repository->findOneBy($criteria, $orderBy); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Finds entities by a set of criteria. |
99
|
|
|
* |
100
|
|
|
* @param array $criteria |
101
|
|
|
* @param array|null $orderBy |
102
|
|
|
* @param int|null $limit |
103
|
|
|
* @param int|null $offset |
104
|
|
|
* |
105
|
|
|
* @return array The objects. |
106
|
|
|
*/ |
107
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
108
|
|
|
{ |
109
|
|
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Finds all entities in the repository. |
114
|
|
|
* |
115
|
|
|
* @return array The entities. |
116
|
|
|
*/ |
117
|
|
|
public function findAll() |
118
|
|
|
{ |
119
|
|
|
return $this->repository->findAll(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return EntityRepository |
124
|
|
|
*/ |
125
|
|
|
public function getRepository() |
126
|
|
|
{ |
127
|
|
|
return $this->repository; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
public function getPutOptionResolver(OptionsResolver $resolver = null) |
132
|
|
|
{ |
133
|
|
|
$resolver = $this->getPostOptionResolver($resolver); |
134
|
|
|
$resolver->setRequired( |
135
|
|
|
[ |
136
|
|
|
'id', |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return $resolver; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: