adjust language loader

This commit is contained in:
kmvan
2019-01-29 11:31:53 +08:00
parent cabe1295db
commit ee66e008b5
3 changed files with 39 additions and 32 deletions
+30 -29
View File
@@ -23,20 +23,22 @@ class Compiler
$code = '';
foreach ($this->yieldFiles($this->COMPONENTS_DIR) as $filePath) {
if (\is_dir($filePath) || false === \strpos($filePath, '.php')) {
continue;
}
if ( ! $this->isDev()) {
foreach ($this->yieldFiles($this->COMPONENTS_DIR) as $filePath) {
if (\is_dir($filePath) || false === \strpos($filePath, '.php')) {
continue;
}
$content = $this->getCodeViaFilePath($filePath);
$code .= $content;
$content = $this->getCodeViaFilePath($filePath);
$code .= $content;
}
}
$preDefineCode = $this->preDefine([
$this->getTimerCode(),
$this->getDevMode(),
$this->getDebugCode(),
$this->getLangLoaderCode(),
$this->getVendorCode(),
]);
$code = "<?php\n{$preDefineCode}\n{$code}";
$code .= $this->loader();
@@ -52,7 +54,7 @@ class Compiler
'distFilePath' => $this->COMPILE_FILE_PATH,
]);
if ( $this->isDev() || ! $this->isDebug()) {
if ( ! $this->isDev()) {
$this->writeFile(\php_strip_whitespace($this->COMPILE_FILE_PATH));
}
@@ -82,7 +84,7 @@ class Compiler
echo "Packing `{$filePath}...";
if ($this->isDev() || $this->isDebug()) {
if ($this->isDev()) {
$code = \file_get_contents($filePath);
} else {
$code = \php_strip_whitespace($filePath);
@@ -129,35 +131,23 @@ PHP;
$isDev = $this->isDev() ? 'true' : 'false';
return <<<PHP
\define('IS_DEV', {$isDev});
\\define('XPROBER_IS_DEV', {$isDev});
PHP;
}
private function getTimerCode(): string
{
return <<<'PHP'
\define('TIMER', \microtime(true));
PHP;
}
private function isDebug(): bool
{
global $argv;
return \in_array('debug', $argv);
}
private function getDebugCode(): string
{
$debug = $this->isDev() || $this->isDebug() ? 'true' : 'false';
return <<<PHP
\\define('DEBUG', {$debug});
\define('XPROBER_TIMER', \microtime(true));
PHP;
}
private function getLangLoaderCode(): string
{
if ($this->isDev()) {
return '';
}
$filePath = $this->COMPONENTS_DIR . '/I18n/Lang.json';
if ( ! \is_readable($filePath)) {
@@ -177,10 +167,10 @@ PHP;
die('Invalid json format.');
}
$json = \base64_encode(\json_encode($json));
$json = \serialize($json);
return <<<PHP
\\define('LANG', '{$json}');
\\define('XPROBER_LANGUAGES', '{$json}');
PHP;
}
@@ -214,6 +204,17 @@ PHP;
return \implode("\n", $files);
}
private function getVendorCode(): string
{
if ( ! $this->isDev()) {
return '';
}
return <<<PHP
include \dirname(__DIR__) . '/vendor/autoload.php';
PHP;
}
private function yieldFiles(string $dir): \Iterator
{
if (\is_dir($dir)) {
+2 -2
View File
@@ -46,8 +46,8 @@ HTML;
$scriptConf = \json_encode(EventsApi::emit('conf', array()));
$footer = EventsApi::emit('footer', '');
$footerOutline = EventsApi::emit('footerOutline', '');
$scriptUrl = \defined('IS_DEV') && \IS_DEV ? "../tmp/app.js?v={$_SERVER['REQUEST_TIME']}" : "?action=getScript&amp;v={$version}";
$styleUrl = \defined('IS_DEV') && \IS_DEV ? "../tmp/app.css?v={$_SERVER['REQUEST_TIME']}" : "?action=getStyle&amp;v={$version}";
$scriptUrl = \defined('\XPROBER_IS_DEV') && \XPROBER_IS_DEV ? "../tmp/app.js?v={$_SERVER['REQUEST_TIME']}" : "?action=getScript&amp;v={$version}";
$styleUrl = \defined('\XPROBER_IS_DEV') && \XPROBER_IS_DEV ? "../tmp/app.css?v={$_SERVER['REQUEST_TIME']}" : "?action=getStyle&amp;v={$version}";
return <<<HTML
<!DOCTYPE html>
+7 -1
View File
@@ -9,7 +9,13 @@ class I18nApi
static $translation = null;
if (null === $translation) {
$translation = \json_decode(\base64_decode(\LANG), true);
if (\defined('\XPROBER_IS_DEV') && \XPROBER_IS_DEV) {
$translation = \file(__DIR__ . '/Lang.json');
unset($translation[0]);
$translation = \json_decode(\implode("\n", $translation), true);
} else {
$translation = \unserialize(\XPROBER_LANGUAGES);
}
}
$clientLang = self::getClientLang();