1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\Water; |
4
|
|
|
|
5
|
|
|
use Saltwater\Salt\Module; |
6
|
|
|
|
7
|
|
|
class ModuleStack extends MagicArrayObject |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var TempStack |
11
|
|
|
*/ |
12
|
|
|
private $stack; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var ModuleFinder |
16
|
|
|
*/ |
17
|
|
|
public $finder; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->stack = new TempStack; |
22
|
|
|
|
23
|
|
|
$this->finder = new ModuleFinder; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add module to stack and register its Salts |
28
|
|
|
* |
29
|
|
|
* @param Module|string $class full class name |
30
|
|
|
* @param bool $master true if this is also the master module |
31
|
|
|
* |
32
|
|
|
* @return bool|null |
33
|
|
|
*/ |
34
|
|
|
public function append($class, $master = false) |
35
|
|
|
{ |
36
|
|
|
if (!$this->canAppend($class)) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!($module = $this->registerModule($class))) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Push to stack after registering - to preserve order of dependencies |
45
|
|
|
$this[$class::getName()] = $module; |
46
|
|
|
|
47
|
|
|
if ($master) { |
48
|
|
|
$this->stack->setMaster($class::getName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check whether a module can be appended to the stack |
56
|
|
|
* |
57
|
|
|
* @param Module|string $class full class name |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
private function canAppend($class) |
62
|
|
|
{ |
63
|
|
|
return class_exists($class) && !isset($this[$class::getName()]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return a module class by its name |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* |
71
|
|
|
* @return Module|string |
72
|
|
|
*/ |
73
|
|
|
public function get($name) |
74
|
|
|
{ |
75
|
|
|
return $this[$name]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Register a module (before adding it to the stack) |
80
|
|
|
* |
81
|
|
|
* @param string|Module $class |
82
|
|
|
* |
83
|
|
|
* @return Module |
84
|
|
|
*/ |
85
|
|
|
private function registerModule($class) |
86
|
|
|
{ |
87
|
|
|
/** @var Module $module */ |
88
|
|
|
$module = new $class; |
89
|
|
|
|
90
|
|
|
$module->register($class::getName()); |
91
|
|
|
|
92
|
|
|
return $module; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Module[] |
97
|
|
|
*/ |
98
|
|
|
public function precedenceList() |
99
|
|
|
{ |
100
|
|
|
$return = array(); |
101
|
|
|
foreach ($this->stack->modulePrecedence() as $name) { |
102
|
|
|
$return[] = $this[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getStack() |
109
|
|
|
{ |
110
|
|
|
return $this->stack; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|