1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration\Config; |
4
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\Storage\Mongo; |
6
|
|
|
use Magium\Configuration\Config\Storage\RelationalDatabase; |
7
|
|
|
use Magium\Configuration\File\Configuration\ConfigurationFileRepository; |
8
|
|
|
use Magium\Configuration\File\Context\AbstractContextConfigurationFile; |
9
|
|
|
use Magium\Configuration\InvalidConfigurationException; |
10
|
|
|
use Magium\Configuration\Manager\CacheFactory; |
11
|
|
|
use MongoDB\Client; |
12
|
|
|
use Zend\Db\Adapter\Adapter; |
13
|
|
|
|
14
|
|
|
class BuilderFactory implements BuilderFactoryInterface |
15
|
|
|
{ |
16
|
|
|
protected $configuration; |
17
|
|
|
protected $adapter; |
18
|
|
|
protected $contextFile; |
19
|
|
|
protected $baseDirectory; |
20
|
|
|
|
21
|
16 |
|
public function __construct( |
22
|
|
|
\SplFileInfo $baseDirectory, |
23
|
|
|
\SimpleXMLElement $configuration, |
24
|
|
|
AbstractContextConfigurationFile $contextConfigurationFile |
25
|
|
|
) |
26
|
|
|
{ |
27
|
16 |
|
$this->configuration = $configuration; |
28
|
16 |
|
$this->contextFile = $contextConfigurationFile; |
29
|
16 |
|
if (!$baseDirectory->isDir()) { |
30
|
1 |
|
throw new InvalidConfigurationException('Base directory must be a directory'); |
31
|
|
|
} |
32
|
15 |
|
$this->baseDirectory = $baseDirectory; |
33
|
15 |
|
} |
34
|
|
|
|
35
|
3 |
|
protected function getCache(\SimpleXMLElement $element) |
36
|
|
|
{ |
37
|
3 |
|
$cacheFactory = new CacheFactory(); |
38
|
3 |
|
return $cacheFactory->getCache($element); |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
public function getDatabaseConfiguration() |
42
|
|
|
{ |
43
|
7 |
|
$config = json_encode($this->configuration->persistenceConfiguration); |
44
|
7 |
|
$config = json_decode($config, true); |
45
|
7 |
|
return $config; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
public function getRelationalAdapter() |
49
|
|
|
{ |
50
|
3 |
|
if (!class_exists(Adapter::class)) { |
51
|
|
|
throw new \Exception('Please make sure you have zendframework/zend-db installed for data storage'); |
52
|
|
|
} |
53
|
3 |
|
if (!$this->adapter instanceof Adapter) { |
54
|
3 |
|
$config = $this->getDatabaseConfiguration(); |
55
|
3 |
|
$this->adapter = new Adapter($config); |
56
|
|
|
} |
57
|
3 |
|
return $this->adapter; |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
public function getMongoDsnString() |
61
|
|
|
{ |
62
|
4 |
|
$config = $this->getDatabaseConfiguration(); |
63
|
4 |
|
$dsn = 'mongodb://'; //[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]'; |
64
|
4 |
|
$at = false; |
65
|
4 |
|
if (!empty($config['username'])) { |
66
|
1 |
|
$dsn .= $config['username']; |
67
|
1 |
|
$at = true; |
68
|
|
|
} |
69
|
4 |
|
if (!empty($config['password'])) { |
70
|
1 |
|
$dsn .= ':'.$config['password']; |
71
|
1 |
|
$at = true; |
72
|
|
|
} |
73
|
4 |
|
if ($at) { |
74
|
1 |
|
$dsn .= '@'; |
75
|
|
|
} |
76
|
4 |
|
$dsn .= $config['hostname']; |
77
|
4 |
|
if (!empty($config['port'])) { |
78
|
1 |
|
$dsn .= ':' . $config['port']; |
79
|
|
|
} |
80
|
4 |
|
return $dsn; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function getMongoAdapter() |
84
|
|
|
{ |
85
|
2 |
|
if (!class_exists(Client::class)) { |
86
|
|
|
throw new \Exception('Please make sure you have mongodb/mongodb installed for data storage'); |
87
|
|
|
} |
88
|
2 |
|
$config = $this->getDatabaseConfiguration(); |
89
|
2 |
|
$dsn = $this->getMongoDsnString(); |
90
|
2 |
|
$client = new Client($dsn); |
91
|
2 |
|
$collection = empty($config['table'])?Mongo::TABLE:$config['table']; |
92
|
2 |
|
return new Mongo($client->selectCollection($config['database'], $collection)); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
public function getPersistence() |
96
|
|
|
{ |
97
|
5 |
|
if (empty($this->configuration->persistenceConfiguration->driver)) { |
98
|
1 |
|
throw new \Exception('Please set your driver type either corresponding to its Zend DB adapter ' |
99
|
|
|
. 'name or the specific document database, such as mongo. Ensure that you have either installed ' |
100
|
1 |
|
. 'zendframework/zend-db or mongodb/mongodb depending on where you want to store your configuration.'); |
101
|
|
|
} |
102
|
4 |
|
if (empty($this->configuration->persistenceConfiguration->database)) { |
103
|
1 |
|
throw new \Exception('You must specify a database in your persistenceConfiguration'); |
104
|
|
|
} |
105
|
3 |
|
if (stripos($this->configuration->persistenceConfiguration->driver, 'mongo') === 0) { |
106
|
|
|
return $this->getMongoAdapter(); |
107
|
|
|
} |
108
|
3 |
|
$persistence = new RelationalDatabase($this->getRelationalAdapter(), $this->contextFile); |
109
|
3 |
|
return $persistence; |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
public function getSecureBaseDirectories() |
113
|
|
|
{ |
114
|
6 |
|
$cwd = getcwd(); |
115
|
6 |
|
$path = $this->baseDirectory->getRealPath(); |
116
|
6 |
|
chdir($path); |
117
|
6 |
|
$config = $this->configuration->configurationDirectories; |
|
|
|
|
118
|
6 |
|
$config = json_encode($config); |
119
|
6 |
|
$config = json_decode($config, true); |
120
|
6 |
|
$baseDirs = []; |
121
|
6 |
|
if (is_array($config) && isset($config['directory'])) { |
122
|
4 |
View Code Duplication |
if (!is_array($config['directory'])) { |
|
|
|
|
123
|
3 |
|
$config['directory'] = [$config['directory']]; |
124
|
|
|
} |
125
|
4 |
|
foreach ($config['directory'] as $dir) { |
126
|
4 |
|
$path = realpath($dir); |
127
|
4 |
|
if (!is_dir($path)) { |
128
|
1 |
|
throw new InvalidConfigurationLocationException('A secure configuration path cannot be determined for the directory: ' . $dir); |
129
|
|
|
} |
130
|
3 |
|
$baseDirs[] = $path; |
131
|
|
|
} |
132
|
|
|
} |
133
|
5 |
|
chdir($cwd); |
134
|
5 |
|
return $baseDirs; |
135
|
|
|
} |
136
|
|
|
|
137
|
5 |
|
public function getConfigurationFiles(array $secureBaseDirectories = []) |
138
|
|
|
{ |
139
|
5 |
|
$config = json_encode($this->configuration->configurationFiles); |
140
|
5 |
|
$config = json_decode($config, true); |
141
|
5 |
|
$files = []; |
142
|
5 |
|
if (!empty($config['file'])) { |
143
|
3 |
View Code Duplication |
if (!is_array($config['file'])) { |
|
|
|
|
144
|
1 |
|
$config['file'] = [$config['file']]; |
145
|
|
|
} |
146
|
3 |
|
foreach ($config['file'] as $file) { |
147
|
3 |
|
$found = false; |
148
|
3 |
|
foreach ($secureBaseDirectories as $base) { |
149
|
3 |
|
chdir($base); |
150
|
3 |
|
$path = realpath($file); |
151
|
3 |
|
if ($path) { |
152
|
3 |
|
$found = true; |
153
|
3 |
|
$files[] = $path; |
154
|
|
|
} |
155
|
|
|
} |
156
|
3 |
|
if (!$found) { |
157
|
|
|
throw new InvalidConfigurationLocationException('Could not find file: ' . $file); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
5 |
|
return $files; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
public function getBuilder() |
165
|
|
|
{ |
166
|
|
|
// This method expects that chdir() has been called on the same level as the magium-configuration.xml file |
167
|
3 |
|
$cache = $this->getCache($this->configuration->cache); |
168
|
3 |
|
$persistence = $this->getPersistence(); |
169
|
3 |
|
$secureBases = $this->getSecureBaseDirectories(); |
170
|
2 |
|
$configurationFiles = $this->getConfigurationFiles($secureBases); |
171
|
2 |
|
$repository = ConfigurationFileRepository::getInstance($secureBases, $configurationFiles); |
172
|
|
|
|
173
|
|
|
/* |
174
|
|
|
* We only populate up to the secureBases because adding a DIC or service manager by configuration starts |
175
|
|
|
* making the configuration-based approach pay off less. If you need a DIC or service manager for your |
176
|
|
|
* configuration builder (which you will if you use object/method callbacks for value filters) then you need |
177
|
|
|
* to wire the Builder object with your own code. |
178
|
|
|
*/ |
179
|
|
|
|
180
|
2 |
|
$builder = new Builder( |
181
|
2 |
|
$cache, |
182
|
2 |
|
$persistence, |
183
|
2 |
|
$repository |
184
|
|
|
); |
185
|
|
|
|
186
|
2 |
|
return $builder; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.