|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Util\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
class ClassConfigurationReader |
|
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
|
|
|
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
|
|
|
$path = realpath(__DIR__ . '/../'); |
|
44
|
|
|
$count = 0; |
|
45
|
|
|
while ($count++ < 10) { |
|
46
|
|
|
$filename = "{$path}/configuration"; |
|
47
|
|
|
$realpath = realpath($filename); |
|
48
|
|
|
if ($realpath !== false && is_dir($realpath)) { |
|
49
|
|
|
// The equality check is due to case-insensitive file systems *ahem* Windows and OS X HFS+ |
|
50
|
|
|
$directories = glob(realpath($filename.'/../').'/*', GLOB_ONLYDIR); |
|
51
|
|
|
foreach ($directories as $directory) { |
|
52
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $directory); |
|
53
|
|
|
$lastPart = array_pop($parts); |
|
54
|
|
|
if ($lastPart == 'configuration') { |
|
55
|
|
|
$configurationDir = $realpath; |
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
$path .= '/../'; |
|
61
|
|
|
$path = realpath($path); // More for debugging clarity. |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!is_dir($configurationDir)) return; |
|
66
|
|
|
|
|
67
|
|
|
$configurationFile = get_class($config) . '.php'; |
|
68
|
|
|
$configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile); |
|
69
|
|
|
|
|
70
|
|
|
if (file_exists($configurationFile)) { |
|
71
|
|
|
include $configurationFile; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |