-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReact.php
209 lines (178 loc) · 5.66 KB
/
React.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
<?php
/**
* Class description
*
* @author Safouane Ahmed Salah
* @license MIT
*/
namespace React;
/**
* Register dynamic class
*/
spl_autoload_register(['React\\React', 'register_class']);
abstract class React{
/**
* Associative array that holds the props passed to components
*
* @var array
*/
protected $props = [];
/**
* List of already imported css and js files
*
* @var array
*/
private static $imported = [];
/**
* Extension handlers when import file
*
* @var array [ext => handler callback]
*/
private static $importHandler = [];
/**
* is Setup done
*
* @var bool
*/
private static $isSetup = false;
/**
* Setup the javascript for handling state
*
* @param string $class_name
*/
private static function setup(): void {
if(self::$isSetup) return;
self::$isSetup = true;
Tag::setup(); //Tag component
//Default Extension handlers [css, js]
self::registerExtHandler('css', function($uri,$file,$isUrl, $ver){ echo new Tag\link(['href'=> $uri. ($ver? "?v=$ver" : '') , 'rel'=> 'stylesheet']); });
self::registerExtHandler('js', function($uri,$file,$isUrl, $ver){ echo new Tag\script(['src'=> $uri. ($ver? "?v=$ver" : ''), 'defer'=> 'true']); });
Component::setup(); //setup component
}
/**
* Autoload class of htmlTag or function component
*
* @param string $class_name
*/
static function register_class($class_name){
self::setup();
$isTag = Tag::isValid($class_name);
if(!$isTag && !function_exists($class_name)) return;
$extend = '\\React\\'.($isTag ? 'Tag' : 'Func');
$namspace = '';
$class_arr = explode('\\', $class_name);
$class = array_pop($class_arr);
if($class_arr) $namspace = 'namespace '. implode('\\', $class_arr).';';
eval("$namspace class $class extends $extend {}");
}
/**
* register Extension Handler
*
* @param string $ext file extension
* @param callable $handler file content
*
* @return void
*/
static function registerExtHandler(string $ext,callable $handler): void {
self::$importHandler[$ext] = $handler;
}
static function getExtHandler(string $ext): ?callable {
return self::$importHandler[$ext] ?? null;
}
/**
* parsed html string
* rule: lowercase and remove whitespace and specialchars
*
* @param string|array $tags one or list of custom html tags to be parse
* @return array
*/
protected static function parseTags($tags) : array {
return array_map(function($tag){ return strtolower(self::parseAttribute($tag)); }, (array)$tags);
}
/**
* Parse html attribute
* allow only [words or dash] for attribute or tag
*
* @param string $attr the string to be parse
* @return string
*/
static function parseAttribute(string $attr): string {
return preg_replace('/[^\w-]/','', $attr); //allow only [words or dash]
}
/**
* Import the assets you need
*
* @param string $file url or relative path to file
* @param string $ver version of imported file
* @return void
*/
static function import(string $file, String $ver = ''): void{
if(Component::isSetState()) return;
self::setup();
$headers = @get_headers($file);
//if url
if(strpos(@$headers[0],'200')!==false){
$realpath = $uri = $file;
$isUrl = true;
}else{
$bt = debug_backtrace();
$dir = dirname($bt[0]['file']);
$realpath = realpath($dir.'/'. $file);
if(!file_exists($realpath)) return;
$uri = str_replace($_SERVER['DOCUMENT_ROOT'], '', $realpath);
$isUrl = false;
}
if(in_array($uri, self::$imported)) return;
self::$imported[] = $uri;
$ext = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
$fn = self::getExtHandler($ext);
if($fn){
$fn($uri, $realpath, $isUrl, $ver);
}
}
function render(){}
/**
* Convert a component to html string
*
* @return string
*/
function __toString(): string {
return $this->render();
}
/**
* Check if array is associative array
*
* @return bool
*/
private function isProps(array $array): bool{
return !empty($array) && array_keys($array) !== range(0, count($array) - 1);
}
/**
* Construct the tag with list of child component and props
*
* 2 possible usage: Component($children, $props) or Component($props)
*
* @param string|array|Component $args
* 1- string|[string] allowed only if html tag
* 2- associative array then it will be considered props
* 3- Component|[Component]
* @param array $props associative array of key=> value
*
*/
function __construct($args = [], $children = []){
if(!is_array($args)) $args = [$args];
if(!is_array($children)) $children = [$children];
$isProps = $this->isProps($args);
$props = [];
if($isProps){
$props = $args;
if(!array_key_exists('children', $props)) $props['children'] = [];
if(!is_array($props['children'])) $props['children'] = [$props['children']];
if($children) $props['children'] = $children;
}else{
$props['children'] = $args;
}
//set properties
$this->props = (object)array_merge((array)$this->props, $props);
}
}