1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DoctrineManager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method null|object find($id) |
11
|
|
|
* @method array findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null) |
12
|
|
|
* @method null|object findOneBy(array $criteria) |
13
|
|
|
* @method array findAll() |
14
|
|
|
* @method object persist($object) |
15
|
|
|
* @method flush() |
16
|
|
|
*/ |
17
|
|
|
abstract class Manager |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ManagerRegistry |
21
|
|
|
*/ |
22
|
|
|
protected $managerRegistry; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ObjectManager |
26
|
|
|
*/ |
27
|
|
|
protected $objectManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $class; |
33
|
|
|
|
34
|
|
|
public function __construct(ManagerRegistry $registry, string $class) |
35
|
|
|
{ |
36
|
|
|
$this->managerRegistry = $registry; |
37
|
|
|
$this->class = $class; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $name |
42
|
|
|
* @param array $arguments |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function __call($name, $arguments) |
46
|
|
|
{ |
47
|
|
|
if (method_exists($this->getRepository(), $name)) { |
48
|
|
|
return call_user_func_array([$this->getRepository(), $name], $arguments); |
49
|
|
|
} |
50
|
|
|
if (method_exists($this->getObjectManager(), $name)) { |
51
|
|
|
return call_user_func_array([$this->getObjectManager(), $name], $arguments); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return ObjectRepository |
57
|
|
|
*/ |
58
|
|
|
public function getRepository() : ObjectRepository |
59
|
|
|
{ |
60
|
|
|
return $this->getObjectManager()->getRepository($this->getClass()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ObjectManager |
65
|
|
|
*/ |
66
|
|
|
public function getObjectManager() : ObjectManager |
67
|
|
|
{ |
68
|
|
|
return $this->managerRegistry->getManagerForClass($this->class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $class |
73
|
|
|
* @return Manager |
74
|
|
|
*/ |
75
|
|
|
public function setClass(string $class) : Manager |
76
|
|
|
{ |
77
|
|
|
$this->class = $class; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getClass() : string |
85
|
|
|
{ |
86
|
|
|
if (false !== strpos($this->class, ':')) { |
87
|
|
|
$metadata = $this->getObjectManager()->getClassMetadata($this->class); |
88
|
|
|
$this->class = $metadata->getName(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->class; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns an instantiated entity class |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function create() |
100
|
|
|
{ |
101
|
|
|
$obj = new $this->class(); |
102
|
|
|
return $obj; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $obj The entity |
107
|
|
|
*/ |
108
|
|
|
public function delete($obj) |
109
|
|
|
{ |
110
|
|
|
$this->getObjectManager()->remove($obj); |
111
|
|
|
$this->getObjectManager()->flush(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Will update/save the entity |
116
|
|
|
* |
117
|
|
|
* @param mixed $obj The entity |
118
|
|
|
* @param bool $flush |
119
|
|
|
*/ |
120
|
|
|
public function update($obj, $flush = true) |
121
|
|
|
{ |
122
|
|
|
$this->getObjectManager()->persist($obj); |
123
|
|
|
|
124
|
|
|
if ($flush) { |
125
|
|
|
$this->getObjectManager()->flush(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|