This repository was archived by the owner on Feb 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.php
189 lines (174 loc) · 5.01 KB
/
build.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
<?php
//
// Pot.js / PotLite.js
// Simple script compiler
//
// Usage:
// Run server.
// Access http://localhost/{this-directory}/build.php?debug=true&type=full
// Parameters:
// - debug:
// "true" or "false"
// Output PHP comiled code if "debug" is "true".
// - type:
// "lite" or "full"
// Build "PotLite.js" if "type" is "lite".
// Build "Pot.js" if "type" is "full".
// - version:
// Optional.
//
pot_build();
class Pot_Builder {
const POT_SETTINGS = 'pot.package.json';
const POTLITE_SETTINGS = 'potlite.package.json';
const SOURCE_DIR = '../src/';
private $debug;
private $version;
private $date;
private $year;
private $type;
private $settings;
private $code;
private $enclosureCode;
private $fileName;
function __construct() {
$this->init();
if (empty($_GET)) {
die('Error: Need to access with HTTP GET.');
}
$this->type = isset($_GET['type']) ? strtolower($_GET['type']) : 'full';
if ($this->type === 'lite') {
$path = self::POTLITE_SETTINGS;
define('POTLITE', true, true);
define('POT', false, true);
} else {
$this->type = 'full';
$path = self::POT_SETTINGS;
define('POTLITE', false, true);
define('POT', true, true);
}
$this->settings = json_decode(file_get_contents($path));
$this->fileName = $this->settings->fileName;
if (isset($_GET['debug'])) {
$this->debug = (bool)preg_match('{^(?:true|1|on|yes)$}i', $_GET['debug']);
} else if (isset($this->settings->debug)) {
$this->debug = (bool)$this->settings->debug;
} else {
$this->debug = false;
}
define('DEBUG', $this->debug, true);
if (!empty($_GET['version'])) {
$this->version = $_GET['version'];
} else if (isset($this->settings->version)) {
$this->version = (string)$this->settings->version;
} else {
$this->version = '$Version$';
}
$this->code = '';
$this->date = date('Y-m-d');
$this->year = date('Y');
}
function init() {
error_reporting(E_ALL);
@ini_set('display_errors', 1);
@ini_set('log_errors', false);
@ini_set('track_errors', false);
@ini_set('default_charset', 'UTF-8');
if (function_exists('date_default_timezone_set') &&
function_exists('date_default_timezone_get')) {
@date_default_timezone_set(@date_default_timezone_get());
}
header('Content-Type: application/javascript');
}
function build() {
$this->load();
$this->flush();
}
function load() {
$codes = array();
foreach ($this->settings->files as $file) {
$code = file_get_contents(self::SOURCE_DIR . $file);
$codes[] = $this->assign($code);
}
if ($this->debug) {
$this->code = implode("\x0A", $codes);
$wrap = file_get_contents(self::SOURCE_DIR . $this->settings->enclosure);
$enclosure = $this->evaluate($this->assign($wrap));
file_put_contents(sprintf('%s-~obj.js', $this->fileName), $enclosure);
unset($wrap, $enclosure);
}
$this->code = $this->evaluate(implode("\x0A", $codes));
$code = file_get_contents(self::SOURCE_DIR . $this->settings->enclosure);
$this->enclosureCode = $this->assign($code);
$this->code = $this->evaluate($this->enclosureCode);
file_put_contents($this->fileName, $this->code);
}
function flush() {
echo $this->code;
flush();
}
function evaluate($code) {
ob_start();
echo eval(sprintf('?>%s', $code));
$code = ob_get_contents();
ob_end_clean();
return $this->normalize($code);
}
function normalize($code) {
return preg_replace('{(\x0D\x0A|\x0A|\x0D)}', "\x0A", $code);
}
function assign($code) {
if (preg_match('{<[?]|[?]>}', $code)) {
die('Error: Invalid code');
}
$code = preg_replace_callback(
'<
(?:/(?:/[\x09\x20]*|[*])|)
\{\#\s*(\$|)(\w+)\s*([^\}]*)\s*\}
(?:[*]/|)
(\x0D\x0A|\x0A|\x0D|)
>sx',
array($this, 'assignCallback'),
$code
);
return $code;
}
function assignCallback($matches) {
$result = 'throw new Error("error!")';
$nl = "\x0A";
if ($matches[1] === '$') {
$result = sprintf('<?php echo $this->%s ?>%s%s', $matches[2], $nl, $matches[4]);
} else {
$expr = '';
switch (strtolower($matches[2])) {
case 'if':
$expr = sprintf('if(%s):', $matches[3]);
break;
case 'else':
$expr = sprintf('else:');
break;
case 'elseif':
$expr = sprintf('elseif(%s):', $matches[3]);
break;
case 'end':
case 'endif':
$expr = sprintf('endif;');
break;
case 'code':
$expr = sprintf('echo $this->code');
break;
default:
$expr = sprintf('%s', $matches[3]);
break;
}
$result = sprintf('<?php %s ?>%s%s', $expr, $nl, $matches[4]);
}
return $result;
}
}
function pot_build() {
$pb = new Pot_Builder();
$pb->build();
$pb = null;
unset($pb);
}