1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\Water; |
4
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
6
|
|
|
use Saltwater\Salt\Module; |
7
|
|
|
use Saltwater\Salt\Provider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Navigator |
11
|
|
|
* |
12
|
|
|
* @package Saltwater\Water |
13
|
|
|
* |
14
|
|
|
* List of known providers: |
15
|
|
|
* |
16
|
|
|
* @property \Saltwater\Root\Provider\Context $context |
17
|
|
|
* @property \Saltwater\RedBean\Provider\Entity $entity |
18
|
|
|
* @property \Saltwater\Root\Provider\Service $service |
19
|
|
|
* |
20
|
|
|
* @property \RedBean_Instance $db |
21
|
|
|
* |
22
|
|
|
* @property \Saltwater\RedBean\Provider\Log $log |
23
|
|
|
* @property \Saltwater\App\Provider\Response $response |
24
|
|
|
* @property \Saltwater\App\Provider\Route $route |
25
|
|
|
* |
26
|
|
|
* @property \Saltwater\App\Common\Config $config |
27
|
|
|
* |
28
|
|
|
* Same as the above, but as methods for injecting a caller |
29
|
|
|
* |
30
|
|
|
* @method \Saltwater\Root\Provider\Context context($caller = null) |
31
|
|
|
* @method \Saltwater\RedBean\Provider\Entity entity($caller = null) |
32
|
|
|
* @method \Saltwater\Root\Provider\Service service($caller = null) |
33
|
|
|
* |
34
|
|
|
* @method \RedBean_Instance db($caller = null) |
35
|
|
|
* |
36
|
|
|
* @method \Saltwater\RedBean\Provider\Log log($caller = null) |
37
|
|
|
* @method \Saltwater\App\Provider\Response response($caller = null) |
38
|
|
|
* @method \Saltwater\App\Provider\Route route($caller = null) |
39
|
|
|
* |
40
|
|
|
* @method \Saltwater\App\Common\Config config($caller = null) |
41
|
|
|
*/ |
42
|
|
|
class Navigator |
43
|
|
|
{ |
44
|
|
|
/** @var ModuleStack */ |
45
|
|
|
public $modules = array(); |
46
|
|
|
|
47
|
|
|
/** @var Registry */ |
48
|
|
|
public $registry = array(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Initiate the navigator by setting up a ModuleStack and a Registry |
52
|
|
|
* |
53
|
|
|
* @return void |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->modules = new ModuleStack(); |
58
|
|
|
|
59
|
|
|
$this->registry = new Registry(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Magic property to get a provider without a preset caller |
64
|
|
|
* |
65
|
|
|
* @param string $type |
66
|
|
|
* |
67
|
|
|
* @return Provider |
68
|
|
|
*/ |
69
|
|
|
public function __get($type) |
70
|
|
|
{ |
71
|
|
|
return $this->provider($type); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Magic function to get a provider with a preset caller |
76
|
|
|
* |
77
|
|
|
* @param string $type |
78
|
|
|
* @param mixed $args |
79
|
|
|
* |
80
|
|
|
* @return Provider |
81
|
|
|
*/ |
82
|
|
|
public function __call($type, $args) |
83
|
|
|
{ |
84
|
|
|
$caller = empty($args) ? null : array_shift($args); |
85
|
|
|
|
86
|
|
|
return $this->provider($type, $caller); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Store the navigator within a cache file |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
*/ |
94
|
|
|
public function storeCache($path) |
95
|
|
|
{ |
96
|
|
|
$info = pathinfo($path); |
97
|
|
|
|
98
|
|
|
if (!is_dir($info['dirname'])) { |
99
|
|
|
mkdir($info['dirname'], 0744, true); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return file_put_contents($path, serialize($this)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Recreate the navigator from a cache file |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function loadCache($path) |
113
|
|
|
{ |
114
|
|
|
$cache = unserialize(file_get_contents($path)); |
115
|
|
|
|
116
|
|
|
foreach ($cache as $k => $v) { |
117
|
|
|
$this->$k = $v; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the Module that provides a context |
125
|
|
|
* |
126
|
|
|
* @param string $name plain name of the context |
127
|
|
|
* |
128
|
|
|
* @return Module|null |
129
|
|
|
*/ |
130
|
|
|
public function getContextModule($name) |
131
|
|
|
{ |
132
|
|
|
return $this->modules->get( |
133
|
|
|
$this->modules->finder->getSaltModule( |
|
|
|
|
134
|
|
|
$this->registry->bit('context.' . $name) |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Generic call for a type of provider |
141
|
|
|
* |
142
|
|
|
* @param string $type |
143
|
|
|
* @param string $caller Caller module name |
144
|
|
|
* |
145
|
|
|
* @return Provider |
146
|
|
|
*/ |
147
|
|
|
public function provider($type, $caller = null) |
148
|
|
|
{ |
149
|
|
|
$salt = 'provider.' . $type; |
150
|
|
|
|
151
|
|
|
if (!$bit = $this->registry->bit($salt)) { |
152
|
|
|
S::halt(500, 'provider does not exist: ' . $type); |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
if (empty($caller)) { |
156
|
|
|
$caller = $this->modules->finder->find(Backtrace::lastCaller(), |
|
|
|
|
157
|
|
|
$salt); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->modules->finder->provider($bit, $caller, $type); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.