-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protolus_Resource.class.php
executable file
·213 lines (195 loc) · 8.88 KB
/
Protolus_Resource.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
function startsWith($haystack, $needle){
return $needle === "" || strpos($haystack, $needle) === 0;
}
function endsWith($haystack, $needle){
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
class Protolus_Resource{
public static $packages = array();
public static $handlers = array();
public static $combined = false;
public static function head($callback){
$result = '';
foreach(Protolus_Resource::$handlers as $type => $handler){
if($type == 'js'){
$result .= '<script>'.file_get_contents(dirname(__FILE__).'/browser-shim.js').'</script>';
}
foreach(Protolus_Resource::$packages as $name => $package){
$names = $package->names($type, $includeDependencies);
if(Protolus_Resource::$combined){
$list = explode($names, "|");
$result .= $handlers[$type]->html($list);
}else{
foreach($names as $index=>$name){
$names[$index] = Protolus_Resource::$handlers[$type]->html($name);
}
$result .= implode("\n", $names);
}
}
}
$callback($result);
}
public static function handle($request, $response, $callback){
$parts = explode('/', $_SERVER['REQUEST_URI']);
if(Protolus_Resource::$handlers[$parts[1]]){
$package = Protolus_Resource::$packages[$parts[2]];
$files = $package->files( $parts[1], true);
echo(
Protolus_Resource::$handlers[$parts[1]]->body(
//$parts[2],
$files
)
);
}else{
$callback();
}
}
public static function enable($type){
require(dirname(__file__).'/protolus-resource/'.$type.'-handler.class.php');
}
public static function req($component){
return Protolus_Resource::reqire($component);
}
public static function reqire($component){
Protolus_Resource::$packages[$component] = new Protolus_Resource($component);
Protolus_Resource::$packages[$component]->package(true);
}
protected $dependencies = array();
public function __construct($name, $options = array()){
$this->name = $name;
if(!$options) $options = array();
}
public function dependencies(){
$result = array();
return $result;
}
public function all($type, $includeDependencies, $callback){
$results = array();
$results = array_merge($results, $callback($type, $this));
if($includeDependencies){
foreach($this->contents['dependencies'] as $name => $version){
$dependency = $this->packages[$name]?$this->packages[$name]:new Protolus_Resource($name);
$results = array_merge($results, $dependency->all($type, $includeDependencies, $callback));
}
foreach($this->contents['optionalDependencies'] as $name => $version){
$dependency = $this->packages[$name]?$this->packages[$name]:new Protolus_Resource($name);
$results = array_merge($results, $dependency->all($type, $includeDependencies, $callback));
}
}
return $results;
}
public function files($type, $includeDependencies){
return $this->all($type, $includeDependencies, function($type, $resource){
$results = array();
if(strtolower($type) == 'js' && $resource->contents['main']){
if(!$resource->contents['autogenerated']){
$results[] = '!'.getcwd().'/node_modules/'.$resource->name.'/'.$resource->contents['main'];
}else{
$results[] = '!'.getcwd().'/node_modules/'.$resource->contents['main'];
}
}
foreach($resource->contents['resources'] as $index => $name){
if(endsWith($name, '.'.$type))
$results[] = getcwd().'/node_modules/'.$resource->name.'/'.$name;
}
return $results;
}, $pre, $post);
}
public function names($type, $includeDependencies){
return $this->all($type, $includeDependencies, function($type, $resource){
$results[] = $resource->name;
return $results;
});
}
public function html($type, $includeDependencies){
$files = $this->files($type, $includeDependencies);
if(Protolus_Resource::$combined){
$list = implode($files, "-");
return '<script src="/'.$type.'/'.$list.'" protolus="'.$list.'"></script>';
}else{
array_map(function($name){
return '<script src="/'.$type.'/'.$name.'"></script>';
}, $files);
return implode($files, "\n");
}
}
public function package($includeDependencies){
$this->path = './node_modules/'.$this->name.'/';
try{
$manifest = file_get_contents($this->path.'package.json');
$data = json_decode($manifest, true);
$this->contents = $data;
}catch(Exception $ex){
if(file_exists('./node_modules/'.$this->name.'.js')){
$this->contents = array();
$this->contents['name'] = $this->name;
$this->contents['main'] = '/'.$this->name.'.js';
$this->contents['autogenerated'] = true;
}
}
if(!$this->contents['dependencies']) $this->contents['dependencies'] = array();
if(!$this->contents['optionalDependencies']) $this->contents['optionalDependencies'] = array();
if(!$this->contents['devDependencies']) $this->contents['devDependencies'] = array();
if(!$this->contents['resources']) $this->contents['resources'] = array();
if($includeDependencies){
foreach($this->contents['dependencies'] as $name => $version){
Protolus_Resource::reqire($name);
$this->dependencies[$name] = $version;
}
foreach($this->contents['optionalDependencies'] as $name => $version){
Protolus_Resource::reqyre($name);
$this->dependencies[$name] = $version;
}
}
}
}
class Protolus_HTTP_Response{
public $statusCode = '200';
public $headers = array();
public function setHeader($name, $value){
$this->headers[$name] = $value;
}
public function getHeader($name){
return $this->headers[$name];
}
public function removeHeader($name){
unset($this->headers[$name]);
}
public function setTrailers($object){
//throw new Error('chunked encodings not supported');
}
public function write($value, $encoding){
if(!$this->writeStarted){
http_response_code($this->statusCode);
foreach($this->headers as $name=>$value){
header($name.':'.$value);
}
$this->writeStarted = true;
}
echo($value);
}
public function end($value, $encoding){
if($value) echo($value);
}
}
class Protolus_HTTP_Request{
public function __construct(){
$this->method = $_SERVER['REQUEST_METHOD'];
$s = &$_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($s['HTTP_X_FORWARDED_HOST']) ?
$s['HTTP_X_FORWARDED_HOST'] : isset($s['HTTP_HOST']) ?
$s['HTTP_HOST'] : $s['SERVER_NAME'];
$this->url = $protocol . '://' . $host . $port . $s['REQUEST_URI'];
$this->httpVersion = $_SERVER['SERVER_PROTOCOL'];
$this->headers = apache_request_headers();
$this->get = $_GET;
$this->post = $_POST;
$this->cookies = $_COOKIES;
}
}