internal/phpx: Decrease server code size
This commit is contained in:
parent
bcd1805001
commit
e4a46658ae
2 changed files with 26 additions and 22 deletions
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -233,6 +234,8 @@ var serverPHP string
|
||||||
|
|
||||||
// pre-process the server.php code to make it shorter
|
// pre-process the server.php code to make it shorter
|
||||||
func init() {
|
func init() {
|
||||||
|
minifier := regexp.MustCompile(`\s*([=)(.,{}])\s*`)
|
||||||
|
|
||||||
// remove the first '<?php' line
|
// remove the first '<?php' line
|
||||||
lines := strings.Split(serverPHP, "\n")[1:]
|
lines := strings.Split(serverPHP, "\n")[1:]
|
||||||
for i, line := range lines {
|
for i, line := range lines {
|
||||||
|
|
@ -244,5 +247,5 @@ func init() {
|
||||||
return !strings.HasPrefix(line, "//")
|
return !strings.HasPrefix(line, "//")
|
||||||
})
|
})
|
||||||
|
|
||||||
serverPHP = strings.Join(lines, "")
|
serverPHP = minifier.ReplaceAllString(strings.Join(lines, ""), "$1")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,43 +4,44 @@
|
||||||
// It is passed as a *command line literal * directly to 'drush:script'.
|
// It is passed as a *command line literal * directly to 'drush:script'.
|
||||||
//
|
//
|
||||||
// As such it is preprocessed and shortened.
|
// As such it is preprocessed and shortened.
|
||||||
|
// This preprocessing is script-specific, and any changes in here might break that optimization.
|
||||||
// It should only contain comments at the beginning of each line, and only starting with '//'.
|
// It should only contain comments at the beginning of each line, and only starting with '//'.
|
||||||
// See server.go.
|
// See server.go.
|
||||||
|
|
||||||
|
// define a json_encode alias, this saves a single character!
|
||||||
|
// (we also reuse it in the error string)
|
||||||
|
$E = 'json_encode';
|
||||||
|
|
||||||
// prevent STDIN from being buffered
|
// prevent STDIN from being buffered
|
||||||
stream_set_read_buffer(STDIN,0);
|
stream_set_read_buffer(STDIN,0);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
// stop outputting errors when executing
|
// stop outputting errors when executing
|
||||||
ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE);
|
ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE);
|
||||||
|
|
||||||
|
|
||||||
while(1){
|
|
||||||
// read the next line to get an end-of-line marker
|
// read the next line to get an end-of-line marker
|
||||||
$marker = fgets(STDIN);
|
$m = fgets(STDIN) or exit(0);
|
||||||
if (!$marker) break;
|
|
||||||
|
|
||||||
// accumulate the buffer until the marker is reached
|
// accumulate the buffer until the marker is reached
|
||||||
// bail out if there is an unexpected end of input
|
// bail out if there is an unexpected end of input
|
||||||
$buffer = "";
|
for($b = $l = ""; $l !== $m;) {
|
||||||
while(1) {
|
$b .= $l;
|
||||||
$line = fgets(STDIN);
|
$l = fgets(STDIN) or exit(1);
|
||||||
if (!$line) break 2;
|
|
||||||
if ($line === $marker) break;
|
|
||||||
$buffer .= $line . "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute it
|
// execute the code, and json_encode it
|
||||||
try {
|
try {
|
||||||
$json = json_encode([eval($buffer),""]);
|
$j = $E([eval($b),""]);
|
||||||
} catch(Throwable $t) {
|
} catch(Throwable $t) {
|
||||||
$json = json_encode([null,(string)$t]);
|
$j = $E([null,(string)$t]);
|
||||||
}
|
}
|
||||||
if($json===false) {
|
|
||||||
$json = '[null,"Error encoding result"]';
|
// if something went wrong, return an error.
|
||||||
|
if($j === false) {
|
||||||
|
$j = '[null,"' . $E . ' Error"]';
|
||||||
}
|
}
|
||||||
|
|
||||||
// and write out the result
|
// and write out the result
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
fwrite(STDOUT,"$json\n");
|
fwrite(STDOUT,"$j\n");
|
||||||
ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE);
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue