forked from assistunion/oxygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOxygen.php
134 lines (118 loc) · 4.16 KB
/
Oxygen.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
<?php
namespace oxygen;
use oxygen\controller\Controller;
use oxygen\scope\Scope;
class Oxygen {
public static function __class_construct($scope) {
/* Intentionally left blank. No code here */
}
private static $stack = array();
private static $sp = 0;
public static function push($call){
self::$stack[self::$sp++] = $call;
}
public static function pop() {
return self::$stack[--self::$sp];
}
public static function peek() {
if (self::$sp > 0) {
return self::$stack[self::$sp-1];
} else {
return null;
}
}
public static function open($tag = 'div', $data = array()){
if(is_array($tag)) {
$data = $tag;
$tag = 'div';
}
preg_match_all('/(([A-Za-z_]+)="([^"]+)")/', $tag, $attrs);
preg_match_all('/#([A-Za-z_0-9\-]+)/', $tag, $ids);
preg_match_all('/\.([A-Za-z_0-9\-]+)/', $tag, $classes);
$ids = $ids[1];
$classes = $classes[1];
preg_match('/^[A-Za-z:_0-9]+/', $tag, $tagm);
$tag = $tagm[0];
$attrs = $attrs[1];
$call = self::$stack[self::$sp-1];
$sp = self::$sp-1;
if(!method_exists($call->instance, 'go')){
while($sp >= 0 && !self::$stack[$sp]->instance instanceof Controller) {
$sp--;
}
if ($sp < 0) throw new \Exception("No controller available");
}
$remote = self::$stack[$sp]->instance->go();
if($remote != '/')$remote = $remote.'/';
$data['remote'] = $remote;
$data['component'] = $call->name;
$call->stack[$call->sp++] = $tag;
$id = '';
foreach($ids as $_id) {
$id = ' id="'. $_id .'"';
}
echo '<' . $tag . $id . ' class="' . self::getCssClass();
foreach($classes as $class) {
echo ' '. $class;
}
echo '"';
foreach ($attrs as $a) {
echo ' '.$a;
}
if(is_array($data)) {
foreach ($data as $key => $value) {
if(!is_string($value)){
$value = json_encode($value);
}else{
$value_ = json_decode($value);
if($value_!==null){
/*if(!preg_match('/^\//',$value)){
$value = json_encode($value);
}*/
}else{
if(preg_match('/^[0]+/',$value)){
$value = json_encode($value);
}
}
}
echo ' data-' . $key . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '"';
}
}
echo '>';
}
public static function cssClassFor($class, $name) {
return 'css-' . implode('_', explode('\\', $class)) . '-' . $name;
}
public static function close() {
$call = self::$stack[self::$sp-1];
$tag = $call->stack[--$call->sp];
echo '</' . $tag . '>';
}
public static function closeAll() {
$call = self::$stack[self::$sp-1];
while ($call->sp > 0) {
$tag = $call->stack[--$call->sp];
echo '</' . $tag . '>';
}
}
public static function getCssClass() {
if(self::$sp === 0) throw new \Exception(
'getCssClass() call is valid only within template code'
);
$call = self::$stack[self::$sp-1];
if ($call->component === false) {
return $call->component = self::cssClassFor(
$call->class,
$call->name
);
} else {
return $call->component;
}
}
public static function Generate() {
$scope = Scope::getRoot();
$generator = $scope->Generator();
$generator->generate();
}
}
?>