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