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