diff --git a/compiler/Compiler.php b/compiler/Compiler.php index c13ee19..549f728 100644 --- a/compiler/Compiler.php +++ b/compiler/Compiler.php @@ -1,16 +1,20 @@ baseDir = "{$dir}/src"; - $this->compileFilePath = "{$dir}/dist/prober.php"; + $this->ROOT = $dir; + $this->BASE_DIR = "{$dir}/src"; + $this->COMPONENTS_DIR = "{$this->BASE_DIR}/Components"; + $this->COMPILE_FILE_PATH = "{$dir}/dist/prober.php"; // lang $this->languageGeneration($dir); @@ -19,7 +23,7 @@ class Compiler $code = ''; - foreach ($this->yieldFiles($this->baseDir) as $filePath) { + foreach ($this->yieldFiles($this->COMPONENTS_DIR) as $filePath) { if (\is_dir($filePath) || false === \strpos($filePath, '.php')) { continue; } @@ -30,6 +34,7 @@ class Compiler $preDefineCode = $this->preDefine([ $this->getTimerCode(), + $this->getDevMode(), $this->getDebugCode(), $this->getLangLoaderCode(), ]); @@ -38,6 +43,15 @@ class Compiler $code = \preg_replace("/(\r|\n)+/", "\n", $code); if (true === $this->writeFile($code)) { + new ScriptGeneration([ + 'scriptFilePath' => "{$this->ROOT}/tmp/app.js", + 'distFilePath' => $this->COMPILE_FILE_PATH, + ]); + new StyleGeneration([ + 'styleFilePath' => "{$this->ROOT}/tmp/app.css", + 'distFilePath' => $this->COMPILE_FILE_PATH, + ]); + $this->isDev() || $this->writeFile(\php_strip_whitespace($this->COMPILE_FILE_PATH)); echo 'Compiled!'; } else { echo 'Failed.'; @@ -49,7 +63,7 @@ class Compiler echo "Generating I18n language pack...\n"; $langGen = new LanguageGeneration("{$dir}/languages"); - $status = $langGen->writeJsonFile("{$dir}/src/I18n/Lang.json"); + $status = $langGen->writeJsonFile("{$this->COMPONENTS_DIR}/I18n/Lang.json"); if ( ! $status) { die("Error: can not generate languages.\n"); @@ -100,31 +114,40 @@ class Compiler { $codeStr = \implode("\n", $code); - return <<isDev() ? 'true' : 'false'; + + return <<isDev() ? 'true' : 'false'; - return <<baseDir . '/I18n/Lang.json'; + $filePath = $this->COMPONENTS_DIR . '/I18n/Lang.json'; if ( ! \is_readable($filePath)) { die('Language is missing.'); @@ -144,16 +167,15 @@ EOT; } $json = \base64_encode(\json_encode($json)); - $json = <<baseDir . '/*'); + $dirs = \glob($this->COMPONENTS_DIR . '/*'); if ( ! $dirs) { return ''; @@ -169,14 +191,14 @@ EOT; continue; } - if ('Entry' === $basename) { + if ('Bootstrap' === $basename) { continue; } - $files[] = "new \\InnStudio\\Prober\\{$basename}\\{$basename}();"; + $files[] = "new \\InnStudio\\Prober\\Components\\{$basename}\\{$basename}();"; } - $files[] = 'new \\InnStudio\\Prober\\Entry\\Entry();'; + $files[] = 'new \\InnStudio\\Prober\\Components\\Bootstrap\\Bootstrap();'; return \implode("\n", $files); } @@ -214,12 +236,12 @@ EOT; private function writeFile(string $data): bool { - $dir = \dirname($this->compileFilePath); + $dir = \dirname($this->COMPILE_FILE_PATH); if ( ! \is_dir($dir)) { \mkdir($dir, 0755, true); } - return (bool) \file_put_contents($this->compileFilePath, $data); + return (bool) \file_put_contents($this->COMPILE_FILE_PATH, $data); } } diff --git a/compiler/LanguageGeneration.php b/compiler/LanguageGeneration.php index 9a34aa2..d7a97e2 100644 --- a/compiler/LanguageGeneration.php +++ b/compiler/LanguageGeneration.php @@ -1,6 +1,6 @@ $this->scriptFilePath, + 'distFilePath' => $this->distFilePath, + ] = $args; + + if ( ! \is_file($this->scriptFilePath)) { + $this->die("File not found: {$this->scriptFilePath}"); + } + + if ( ! \is_file($this->distFilePath)) { + $this->die("File not found: {$this->distFilePath}"); + } + + if ( ! $this->setScript($this->getScript())) { + $this->die('Error: can not write script content to dist.'); + } + + $this->die('Script content wrote successful.', false); + } + + private function getScript(): string + { + return (string) \file_get_contents($this->scriptFilePath); + } + + private function setScript(string $script): bool + { + $dist = (string) \file_get_contents($this->distFilePath); + + if ( ! $dist) { + return false; + } + + $dist = \str_replace('{INN_SCRIPT}', $script, $dist); + + return (bool) \file_put_contents($this->distFilePath, $dist); + } + + private function die(string $msg, bool $die = true): void + { + $msg = "[ScriptGeneration] {$msg}\n"; + + if ($die) { + die($msg); + } + + echo $msg; + } +} diff --git a/compiler/StyleGeneration.php b/compiler/StyleGeneration.php new file mode 100644 index 0000000..a97071d --- /dev/null +++ b/compiler/StyleGeneration.php @@ -0,0 +1,61 @@ + $this->styleFilePath, + 'distFilePath' => $this->distFilePath, + ] = $args; + + if ( ! \is_file($this->styleFilePath)) { + $this->die("File not found: {$this->styleFilePath}"); + } + + if ( ! \is_file($this->distFilePath)) { + $this->die("File not found: {$this->distFilePath}"); + } + + if ( ! $this->setStyle($this->getStyle())) { + $this->die('Error: can not write style content to dist.'); + } + + $this->die('Style content wrote successful.', false); + } + + private function getStyle(): string + { + return (string) \file_get_contents($this->styleFilePath); + } + + private function setStyle(string $style): bool + { + $dist = (string) \file_get_contents($this->distFilePath); + + if ( ! $dist) { + return false; + } + + $dist = \str_replace('{INN_STYLE}', $style, $dist); + + return (bool) \file_put_contents($this->distFilePath, $dist); + } + + private function die(string $msg, bool $die = true): void + { + $msg = "[StyleGeneration] {$msg}\n"; + + if ($die) { + die($msg); + } + + echo $msg; + } +}