From 9c2893388250a6fe9eff489fee66df5536bec0cc Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 17 Aug 2020 17:50:42 +0300 Subject: [PATCH] preload --- Gruntfile.js | 1 + .../Espo/Core/ApplicationRunners/Preload.php | 50 +++++++ application/Espo/Core/Utils/Preload.php | 131 ++++++++++++++++++ preload.php | 45 ++++++ 4 files changed, 227 insertions(+) create mode 100644 application/Espo/Core/ApplicationRunners/Preload.php create mode 100644 application/Espo/Core/Utils/Preload.php create mode 100644 preload.php diff --git a/Gruntfile.js b/Gruntfile.js index e2360ca944..c21d351790 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -211,6 +211,7 @@ module.exports = function (grunt) { 'daemon.php', 'rebuild.php', 'clear_cache.php', + 'preload.php', 'upgrade.php', 'extension.php', 'websocket.php', diff --git a/application/Espo/Core/ApplicationRunners/Preload.php b/application/Espo/Core/ApplicationRunners/Preload.php new file mode 100644 index 0000000000..78f81c912d --- /dev/null +++ b/application/Espo/Core/ApplicationRunners/Preload.php @@ -0,0 +1,50 @@ +process(); + } +} diff --git a/application/Espo/Core/Utils/Preload.php b/application/Espo/Core/Utils/Preload.php new file mode 100644 index 0000000000..1f67f89638 --- /dev/null +++ b/application/Espo/Core/Utils/Preload.php @@ -0,0 +1,131 @@ +dirList as $dir) { + $this->processForDir($dir); + } + + echo "Success." . PHP_EOL; + echo "Files preloaded: " . (string) $this->counter . "." . PHP_EOL; + } + + protected function processForDir(string $dir) + { + $directory = new RecursiveDirectoryIterator($dir); + $fullTree = new RecursiveIteratorIterator($directory); + $phpFiles = new RegexIterator($fullTree, '/.+((? $file) { + $this->processFile($file[0]); + } + } + + protected function processFile(string $file) + { + if ($this->isFileToBeIgnored($file)) { + return; + } + + try { + require_once($file); + + $this->counter ++; + } + catch (Throwable $e) { + $this->processException($e); + + throw $e; + } + } + + protected function processException(Throwable $e) + { + echo "Error occured." . PHP_EOL; + + $msg = $e->getMessage(); + + if ($msg) { + echo "Message: {$msg}" . PHP_EOL; + } + + $file = $e->getFile(); + + if ($file) { + echo "File: {$file}" . PHP_EOL; + } + + echo "Line: " . $e->getLine() . PHP_EOL; + } + + protected function isFileToBeIgnored(string $file) : bool + { + $file = str_replace('\\', '/', $file); + + foreach ($this->ignoreList as $item) { + if (strpos($file, $item) === 0) { + return true; + } + } + + return false; + + return in_array($file, $this->ignoreList); + } +} diff --git a/preload.php b/preload.php new file mode 100644 index 0000000000..3c4cdaad9c --- /dev/null +++ b/preload.php @@ -0,0 +1,45 @@ +run(Preload::class);