|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saltwater; |
|
4
|
|
|
|
|
5
|
|
|
use Saltwater\Water\Navigator; |
|
6
|
|
|
|
|
7
|
|
|
class Server |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var Navigator */ |
|
10
|
|
|
public static $n; |
|
11
|
|
|
|
|
12
|
|
|
/** @var float */ |
|
13
|
|
|
public static $start; |
|
14
|
|
|
|
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
public static $env = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Kick off the server with a set of modules. |
|
20
|
|
|
* |
|
21
|
|
|
* The first module is automatically the root module. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $path filepath to deployment directory |
|
24
|
|
|
* @param string[] $modules array of class names of modules to include |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function bootstrap($path = null, $modules = array()) |
|
29
|
|
|
{ |
|
30
|
|
|
self::start(); |
|
31
|
|
|
|
|
32
|
|
|
self::$env['name'] = basename($path); |
|
33
|
|
|
|
|
34
|
|
|
self::$env['root-path'] = $path; |
|
35
|
|
|
|
|
36
|
|
|
self::detectEnv(); |
|
37
|
|
|
|
|
38
|
|
|
if ($modules) { |
|
|
|
|
|
|
39
|
|
|
self::addModules($modules); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Detect some basic information about our deployment |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function detectEnv() |
|
49
|
|
|
{ |
|
50
|
|
|
self::$env['gt36'] = version_compare(phpversion(), '5.3.6', '>='); |
|
51
|
|
|
self::$env['gt54'] = version_compare(phpversion(), '5.4.0', '>='); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Init Server from Cache |
|
56
|
|
|
* |
|
57
|
|
|
* @param $modules |
|
58
|
|
|
* @param $cache |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
private static function initCached($modules, $cache) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if (self::loadCache($cache)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
self::addModules($modules); |
|
69
|
|
|
|
|
70
|
|
|
self::$n->storeCache($cache); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set timestamp and Navigator instance |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function start() |
|
79
|
|
|
{ |
|
80
|
|
|
if (!empty(self::$start)) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** @var float $start */ |
|
85
|
|
|
self::$start = microtime(true); |
|
86
|
|
|
|
|
87
|
|
|
self::$n = new Navigator(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $cache |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
private static function loadCache($cache) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!file_exists($cache)) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return self::$n->loadCache($cache); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add one or more modules to the Saltwater\Navigator module stack |
|
106
|
|
|
* |
|
107
|
|
|
* Proxy for Saltwater\Navigator::addModule() |
|
108
|
|
|
* |
|
109
|
|
|
* @param string[] $array |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool|null |
|
112
|
|
|
*/ |
|
113
|
|
|
private static function addModules($array) |
|
114
|
|
|
{ |
|
115
|
|
|
if (empty(self::$start)) { |
|
116
|
|
|
self::bootstrap(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (!is_array($array)) { |
|
120
|
|
|
$array = array($array); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
foreach ($array as $i => $module) { |
|
124
|
|
|
self::$n->modules->append($module, $i == 0); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Add a module to the Saltwater\Navigator module stack |
|
130
|
|
|
* |
|
131
|
|
|
* Proxy for Saltwater\Navigator::addModule() |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $class |
|
134
|
|
|
* @param bool $master |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool|null |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function addModule($class, $master = true) |
|
139
|
|
|
{ |
|
140
|
|
|
return self::$n->modules->append($class, $master); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Halt the server and send a html header response |
|
145
|
|
|
* |
|
146
|
|
|
* @param int $code |
|
147
|
|
|
* @param string $message |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function halt($code, $message) |
|
152
|
|
|
{ |
|
153
|
|
|
header("HTTP/1.1 " . $code . " " . $message); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Forget everything |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function destroy() |
|
162
|
|
|
{ |
|
163
|
|
|
self::$n = null; |
|
164
|
|
|
|
|
165
|
|
|
self::$start = null; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.