1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/workflow-doctrine-zf2 |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\Doctrine\ZF2; |
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use OldTown\Workflow\Spi\Doctrine\EntityManagerFactory\EntityManagerFactoryInterface; |
10
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EntityManagerFactory |
15
|
|
|
* |
16
|
|
|
* @package OldTown\Workflow\Doctrine\ZF2 |
17
|
|
|
*/ |
18
|
|
|
class EntityManagerFactory implements EntityManagerFactoryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const ENTITY_MANAGER_NAME = 'entityManagerName'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Сервис локатор по умолчанию |
27
|
|
|
* |
28
|
|
|
* @var ServiceLocatorInterface |
29
|
|
|
*/ |
30
|
|
|
protected static $defaultServiceLocator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Сервис локатор |
34
|
|
|
* |
35
|
|
|
* @var ServiceLocatorInterface |
36
|
|
|
*/ |
37
|
|
|
protected $serviceLocator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Создает менеджер сущностей доктрины |
41
|
|
|
* |
42
|
|
|
* @param array $options |
43
|
|
|
* |
44
|
|
|
* @return EntityManagerInterface |
45
|
|
|
* |
46
|
|
|
* @throws Exception\InvalidArgumentException |
47
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
48
|
|
|
* @throws Exception\RuntimeException |
49
|
|
|
*/ |
50
|
|
|
public function factory(array $options = []) |
51
|
|
|
{ |
52
|
|
|
if (!array_key_exists(static::ENTITY_MANAGER_NAME, $options)) { |
53
|
|
|
$errMsg = sprintf('Option %s not found', static::ENTITY_MANAGER_NAME); |
54
|
|
|
throw new Exception\InvalidArgumentException($errMsg); |
55
|
|
|
} |
56
|
|
|
$emName = $options[static::ENTITY_MANAGER_NAME]; |
57
|
|
|
|
58
|
|
|
return $this->getServiceLocator()->get($emName); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ServiceLocatorInterface |
63
|
|
|
* |
64
|
|
|
* @throws \OldTown\Workflow\Doctrine\ZF2\Exception\RuntimeException |
65
|
|
|
*/ |
66
|
|
|
public function getServiceLocator() |
67
|
|
|
{ |
68
|
|
|
if ($this->serviceLocator) { |
69
|
|
|
return $this->serviceLocator; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->serviceLocator = static::getDefaultServiceLocator(); |
73
|
|
|
|
74
|
|
|
return $this->serviceLocator; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
83
|
|
|
{ |
84
|
|
|
$this->serviceLocator = $serviceLocator; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return ServiceLocatorInterface |
91
|
|
|
* |
92
|
|
|
* @throws Exception\RuntimeException |
93
|
|
|
*/ |
94
|
|
|
public static function getDefaultServiceLocator() |
95
|
|
|
{ |
96
|
|
|
if (!static::$defaultServiceLocator instanceof ServiceLocatorInterface) { |
97
|
|
|
$errMsg = 'Service locator not found'; |
98
|
|
|
throw new Exception\RuntimeException($errMsg); |
99
|
|
|
} |
100
|
|
|
return static::$defaultServiceLocator; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ServiceLocatorInterface $defaultServiceLocator |
105
|
|
|
*/ |
106
|
|
|
public static function setDefaultServiceLocator(ServiceLocatorInterface $defaultServiceLocator) |
107
|
|
|
{ |
108
|
|
|
static::$defaultServiceLocator = $defaultServiceLocator; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|