|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Configuration\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\Storage\RelationalDatabase; |
|
6
|
|
|
use Magium\Configuration\File\Configuration\ConfigurationFileRepository; |
|
7
|
|
|
use Magium\Configuration\Manager\CacheFactory; |
|
8
|
|
|
use Zend\Db\Adapter\Adapter; |
|
9
|
|
|
|
|
10
|
|
|
class BuilderFactory implements BuilderFactoryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $configuration; |
|
13
|
|
|
protected $adapter; |
|
14
|
|
|
|
|
15
|
2 |
|
public function __construct(\SimpleXMLElement $configuration) |
|
16
|
|
|
{ |
|
17
|
2 |
|
$this->configuration = $configuration; |
|
18
|
2 |
|
} |
|
19
|
|
|
|
|
20
|
2 |
|
protected function getCache(\SimpleXMLElement $element) |
|
21
|
|
|
{ |
|
22
|
2 |
|
$cacheFactory = new CacheFactory(); |
|
23
|
2 |
|
return $cacheFactory->getCache($element); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function getAdapter() |
|
27
|
|
|
{ |
|
28
|
2 |
|
if (!$this->adapter instanceof Adapter) { |
|
29
|
2 |
|
$config = json_encode($this->configuration->persistenceConfiguration); |
|
30
|
2 |
|
$config = json_decode($config, true); |
|
31
|
2 |
|
$this->adapter = new Adapter($config); |
|
32
|
|
|
} |
|
33
|
2 |
|
return $this->adapter; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public function getPersistence() |
|
37
|
|
|
{ |
|
38
|
2 |
|
$persistence = new RelationalDatabase($this->getAdapter()); |
|
39
|
2 |
|
return $persistence; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
protected function getSecureBaseDirectories() |
|
43
|
|
|
{ |
|
44
|
2 |
|
$config = json_encode($this->configuration->configurationDirectories); |
|
45
|
2 |
|
$config = json_decode($config, true); |
|
46
|
2 |
|
$baseDirs = []; |
|
47
|
2 |
|
if (is_array($config)) { |
|
48
|
|
|
// This code depends on chdir() having been called in MagiumConfigurationFactory |
|
49
|
2 |
|
foreach ($config as $dir) { |
|
50
|
1 |
|
$path = realpath($dir); |
|
51
|
1 |
|
if (!is_dir($path)) { |
|
52
|
1 |
|
throw new InvalidConfigurationLocationException('A secure configuration path cannot be determined for the directory: ' . $dir); |
|
53
|
|
|
} |
|
54
|
|
|
$baseDirs[] = $path; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
1 |
|
return $baseDirs; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
protected function getConfigurationFiles(array $secureBaseDirectories = []) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$config = json_encode($this->configuration->configurationFiles); |
|
63
|
1 |
|
$config = json_decode($config, true); |
|
64
|
1 |
|
$files = []; |
|
65
|
1 |
|
foreach ($config as $file) { |
|
66
|
|
|
$found = false; |
|
67
|
|
|
foreach ($secureBaseDirectories as $base) { |
|
68
|
|
|
chdir($base); |
|
69
|
|
|
$path = realpath($file); |
|
70
|
|
|
if ($path) { |
|
71
|
|
|
$found = true; |
|
72
|
|
|
$files[] = $path; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
if (!$found) { |
|
76
|
|
|
throw new InvalidConfigurationLocationException('Could not find file: ' . $file); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
1 |
|
return $files; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
public function getBuilder() |
|
83
|
|
|
{ |
|
84
|
|
|
// This method expects that chdir() has been called on the same level as the magium-configuration.xml file |
|
85
|
2 |
|
$cache = $this->getCache($this->configuration->cache); |
|
86
|
2 |
|
$persistence = $this->getPersistence(); |
|
87
|
2 |
|
$secureBases = $this->getSecureBaseDirectories(); |
|
88
|
1 |
|
$configurationFiles = $this->getConfigurationFiles($secureBases); |
|
89
|
1 |
|
$repository = new ConfigurationFileRepository($secureBases, $configurationFiles); |
|
90
|
|
|
|
|
91
|
|
|
/* |
|
92
|
|
|
* We only populate up to the secureBases because adding a DIC or service manager by configuration starts |
|
93
|
|
|
* making the configuration-based approach pay off less. If you need a DIC or service manager for your |
|
94
|
|
|
* configuration builder (which you will if you use object/method callbacks for value filters) then you need |
|
95
|
|
|
* to wire the Builder object with your own code. |
|
96
|
|
|
*/ |
|
97
|
|
|
|
|
98
|
1 |
|
$builder = new Builder( |
|
99
|
|
|
$cache, |
|
100
|
|
|
$persistence, |
|
101
|
|
|
$repository |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
1 |
|
return $builder; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|