1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/workflow-doctrine |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\Spi\Doctrine\EntityManagerFactory; |
7
|
|
|
|
8
|
|
|
use \Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Interface EntityManagerFactoryInterface |
12
|
|
|
* |
13
|
|
|
* @package OldTown\Workflow\Spi\Doctrine\EntityManagerFactory |
14
|
|
|
*/ |
15
|
|
|
class SimpleEntityManagerFactory implements EntityManagerFactoryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EntityManagerInterface |
19
|
|
|
*/ |
20
|
|
|
protected static $defaultEntityManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EntityManagerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $entityManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Создает менеджер сущностей доктрины |
29
|
|
|
* |
30
|
|
|
* @param array $options |
31
|
|
|
* |
32
|
|
|
* @return EntityManagerInterface |
33
|
|
|
* |
34
|
|
|
* @throws Exception\RuntimeException |
35
|
|
|
*/ |
36
|
|
|
public function factory(array $options = []) |
37
|
|
|
{ |
38
|
|
|
$em = $this->getEntityManager(); |
39
|
|
|
if ($em) { |
40
|
|
|
return $em; |
41
|
|
|
} |
42
|
|
|
return static::getDefaultEntityManager(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return EntityManagerInterface |
47
|
|
|
* |
48
|
|
|
* @throws Exception\RuntimeException |
49
|
|
|
*/ |
50
|
|
|
public static function getDefaultEntityManager() |
51
|
|
|
{ |
52
|
|
|
if (!static::$defaultEntityManager instanceof EntityManagerInterface) { |
53
|
|
|
$errMsg = 'Entity Manager not found'; |
54
|
|
|
throw new Exception\RuntimeException($errMsg); |
55
|
|
|
} |
56
|
|
|
return static::$defaultEntityManager; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param EntityManagerInterface $defaultEntityManager |
61
|
|
|
*/ |
62
|
|
|
public static function setDefaultEntityManager(EntityManagerInterface $defaultEntityManager) |
63
|
|
|
{ |
64
|
|
|
static::$defaultEntityManager = $defaultEntityManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return EntityManagerInterface |
69
|
|
|
*/ |
70
|
|
|
public function getEntityManager() |
71
|
|
|
{ |
72
|
|
|
return $this->entityManager; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param EntityManagerInterface $entityManager |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager) |
81
|
|
|
{ |
82
|
|
|
$this->entityManager = $entityManager; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|