1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Util\Configuration; |
4
|
|
|
|
5
|
|
|
class ClassConfigurationReader extends AbstractConfigurationReader |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
protected $configurationDir; |
9
|
|
|
protected $object; |
10
|
|
|
|
11
|
|
|
public function __construct( |
12
|
|
|
$configurationDir = null |
13
|
|
|
) |
14
|
|
|
{ |
15
|
|
|
$this->configurationDir = $configurationDir; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param null $configurationDir |
20
|
|
|
*/ |
21
|
|
|
public function setConfigurationDir($configurationDir) |
22
|
|
|
{ |
23
|
|
|
$this->configurationDir = $configurationDir; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function __set($name, $value) |
28
|
|
|
{ |
29
|
|
|
$this->object->$name = $value; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __call($method, $args) |
33
|
|
|
{ |
34
|
|
|
return call_user_func_array([$this->object, $method], $args); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function configure(ConfigurableObjectInterface $config) |
39
|
|
|
{ |
40
|
|
|
$this->object = $config; |
41
|
|
|
$configurationDir = $this->configurationDir; |
42
|
|
|
if ($configurationDir === null) { |
43
|
|
|
$testParts = explode(DIRECTORY_SEPARATOR, realpath(__DIR__.'/../../..')); |
44
|
|
|
$isVendor = array_pop($testParts) == 'vendor'; |
45
|
|
|
|
46
|
|
|
if ($isVendor) { |
47
|
|
|
$path = realpath(__DIR__ . '/../../../../..'); // Get out of the Magium directories |
48
|
|
|
} else { |
49
|
|
|
$path = realpath(__DIR__ . '/../../..'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$count = 0; |
53
|
|
|
while ($count++ < 10) { |
54
|
|
|
$filename = "{$path}/configuration"; |
55
|
|
|
$realpath = realpath($filename); |
56
|
|
|
if ($realpath !== false && is_dir($realpath)) { |
57
|
|
|
// The equality check is due to case-insensitive file systems *ahem* Windows and OS X HFS+ |
58
|
|
|
$directories = glob(realpath($filename.'/../').'/*', GLOB_ONLYDIR); |
59
|
|
|
foreach ($directories as $directory) { |
60
|
|
|
$directory = realpath($directory); |
61
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $directory); |
62
|
|
|
$lastPart = array_pop($parts); |
63
|
|
|
if ($lastPart == 'configuration') { |
64
|
|
|
$configurationDir = $realpath; |
65
|
|
|
break 2; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
$path .= '/../'; |
70
|
|
|
$path = realpath($path); // More for debugging clarity. |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!is_dir($configurationDir)) return; |
75
|
|
|
|
76
|
|
|
$classes = $this->introspectClass($config); |
77
|
|
|
|
78
|
|
|
foreach ($classes as $class) { |
79
|
|
|
$configurationFile = $class . '.php'; |
80
|
|
|
$configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile); |
81
|
|
|
|
82
|
|
|
if (file_exists($configurationFile)) { |
83
|
|
|
include $configurationFile; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|