|
1
|
|
|
<?php |
|
2
|
|
|
namespace Del\Common; |
|
3
|
|
|
|
|
4
|
|
|
use Pimple\Container as PimpleContainer; |
|
5
|
|
|
use Del\Common\Container\RegistrationInterface; |
|
6
|
|
|
use Del\Common\Config\DbCredentials; |
|
7
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
|
9
|
|
|
|
|
10
|
|
|
class ContainerService |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var PimpleContainer |
|
14
|
|
|
*/ |
|
15
|
|
|
private $container; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var DbCredentials |
|
19
|
|
|
*/ |
|
20
|
|
|
private $credentials; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $paths; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(){} |
|
28
|
|
|
public function __clone(){} |
|
29
|
|
|
|
|
30
|
7 |
|
public static function getInstance() |
|
31
|
|
|
{ |
|
32
|
7 |
|
static $inst = null; |
|
33
|
7 |
|
if($inst === null) |
|
34
|
7 |
|
{ |
|
35
|
1 |
|
$inst = new ContainerService(); |
|
36
|
1 |
|
$inst->container = new PimpleContainer(); |
|
37
|
1 |
|
$inst->paths = ['src/Entity']; |
|
38
|
1 |
|
} |
|
39
|
7 |
|
return $inst; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return PimpleContainer |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function getContainer() |
|
47
|
|
|
{ |
|
48
|
5 |
|
$this->container['db.credentials'] = $this->getDbCredentials()->toArray(); |
|
49
|
|
|
|
|
50
|
5 |
|
$this->container['entity.paths'] = $this->getEntityPaths(); |
|
51
|
|
|
|
|
52
|
|
|
// The Doctrine Entity Manager |
|
53
|
5 |
|
$this->container['doctrine.entity_manager'] = $this->container->factory(function ($c) { |
|
54
|
|
|
|
|
55
|
2 |
|
$isDevMode = false; |
|
56
|
|
|
|
|
57
|
2 |
|
$paths = $c['entity.paths']; |
|
58
|
|
|
|
|
59
|
2 |
|
$dbParams = $c['db.credentials']; |
|
60
|
|
|
|
|
61
|
2 |
|
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); |
|
62
|
2 |
|
$entityManager = EntityManager::create($dbParams, $config); |
|
63
|
|
|
|
|
64
|
2 |
|
return $entityManager; |
|
65
|
5 |
|
}); |
|
66
|
5 |
|
return $this->container; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $path |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function addEntityPath($path) |
|
76
|
|
|
{ |
|
77
|
3 |
|
$this->paths[] = $path; |
|
78
|
3 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
6 |
|
public function getEntityPaths() |
|
85
|
|
|
{ |
|
86
|
6 |
|
return $this->paths; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return DbCredentials |
|
91
|
|
|
*/ |
|
92
|
6 |
|
public function getDbCredentials() |
|
93
|
|
|
{ |
|
94
|
6 |
|
return $this->credentials ? $this->credentials : new DbCredentials(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param DbCredentials $credentials |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function setDbCredentials(DbCredentials $credentials) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$this->credentials = $credentials; |
|
104
|
2 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
2 |
|
public function registerToContainer(RegistrationInterface $config) |
|
109
|
1 |
|
{ |
|
110
|
2 |
|
if($config->hasEntityPath()) { |
|
111
|
1 |
|
$this->addEntityPath($config->getEntityPath()); |
|
112
|
1 |
|
} |
|
113
|
2 |
|
$config->addToContainer($this->container); |
|
114
|
2 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|