From 983988c9e11fd38e54098e5d4edcf603a8d6feca Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 8 Feb 2019 16:44:54 +0200 Subject: [PATCH] ConsoleCommand framework --- Gruntfile.js | 2 +- application/Espo/Core/Application.php | 6 ++ .../Espo/Core/Console/CommandManager.php | 52 +++++++++++++++++ .../Core/Console/Commands/AuthTokenCheck.php | 56 +++++++++++++++++++ .../Espo/Core/Console/Commands/Base.php | 45 +++++++++++++++ .../Espo/Core/Console/Commands/ClearCache.php | 39 +++++++++++++ .../Espo/Core/Console/Commands/Rebuild.php | 39 +++++++++++++ .../Core/Loaders/ConsoleCommandManager.php | 40 +++++++++++++ application/Espo/Core/WebSocket/Pusher.php | 2 +- clear_cache.php | 6 +- auth_token_check.php => command.php | 32 +++-------- cron.php | 5 +- daemon.php | 5 +- rebuild.php | 9 +-- websocket.php | 2 +- 15 files changed, 294 insertions(+), 46 deletions(-) create mode 100644 application/Espo/Core/Console/CommandManager.php create mode 100644 application/Espo/Core/Console/Commands/AuthTokenCheck.php create mode 100644 application/Espo/Core/Console/Commands/Base.php create mode 100644 application/Espo/Core/Console/Commands/ClearCache.php create mode 100644 application/Espo/Core/Console/Commands/Rebuild.php create mode 100644 application/Espo/Core/Loaders/ConsoleCommandManager.php rename auth_token_check.php => command.php (75%) diff --git a/Gruntfile.js b/Gruntfile.js index c1ef10d830..ce2a595ec3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -179,7 +179,7 @@ module.exports = function (grunt) { 'upgrade.php', 'extension.php', 'websocket.php', - 'auth_token_check.php', + 'command.php', 'index.php', 'LICENSE.txt', '.htaccess', diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 0ce3b6f092..1ccf026277 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -212,6 +212,12 @@ class Application $dataManager->clearCache(); } + public function runCommand(string $command) + { + $consoleCommandManager = $this->getContainer()->get('consoleCommandManager'); + return $consoleCommandManager->run($command); + } + public function isInstalled() { $config = $this->getConfig(); diff --git a/application/Espo/Core/Console/CommandManager.php b/application/Espo/Core/Console/CommandManager.php new file mode 100644 index 0000000000..7f3395adbc --- /dev/null +++ b/application/Espo/Core/Console/CommandManager.php @@ -0,0 +1,52 @@ +container = $container; + } + + public function run(string $command) + { + $className = '\\Espo\\Core\\Console\\Commands\\' . $command; + if (!class_exists($className)) { + $msg = "Command '{$command}' does not exist."; + echo $msg . "\n"; + throw new \Espo\Core\Exceptions\Error($msg); + } + $impl = new $className($this->container); + return $impl->run(); + } +} diff --git a/application/Espo/Core/Console/Commands/AuthTokenCheck.php b/application/Espo/Core/Console/Commands/AuthTokenCheck.php new file mode 100644 index 0000000000..d1187a31aa --- /dev/null +++ b/application/Espo/Core/Console/Commands/AuthTokenCheck.php @@ -0,0 +1,56 @@ +getContainer()->get('entityManager'); + + $authToken = $entityManager->getRepository('AuthToken')->where([ + 'token' => $token, + 'isActive' => true, + ])->findOne(); + + if (!$authToken) return; + if (!$authToken->get('userId')) return; + + $userId = $authToken->get('userId'); + + $user = $entityManager->getEntity('User', $userId); + if (!$user) return; + + return $user->id; + } +} diff --git a/application/Espo/Core/Console/Commands/Base.php b/application/Espo/Core/Console/Commands/Base.php new file mode 100644 index 0000000000..0ceba316c6 --- /dev/null +++ b/application/Espo/Core/Console/Commands/Base.php @@ -0,0 +1,45 @@ +container = $container; + } + + protected function getContainer() + { + return $this->container; + } +} diff --git a/application/Espo/Core/Console/Commands/ClearCache.php b/application/Espo/Core/Console/Commands/ClearCache.php new file mode 100644 index 0000000000..aefae86f1d --- /dev/null +++ b/application/Espo/Core/Console/Commands/ClearCache.php @@ -0,0 +1,39 @@ +getContainer()->get('dataManager')->clearCache(); + echo "Cache has been cleared.\n"; + } +} diff --git a/application/Espo/Core/Console/Commands/Rebuild.php b/application/Espo/Core/Console/Commands/Rebuild.php new file mode 100644 index 0000000000..47bd937573 --- /dev/null +++ b/application/Espo/Core/Console/Commands/Rebuild.php @@ -0,0 +1,39 @@ +getContainer()->get('dataManager')->rebuild(); + echo "Rebuild has been done.\n"; + } +} diff --git a/application/Espo/Core/Loaders/ConsoleCommandManager.php b/application/Espo/Core/Loaders/ConsoleCommandManager.php new file mode 100644 index 0000000000..e50a0ea533 --- /dev/null +++ b/application/Espo/Core/Loaders/ConsoleCommandManager.php @@ -0,0 +1,40 @@ +getContainer() + ); + } +} diff --git a/application/Espo/Core/WebSocket/Pusher.php b/application/Espo/Core/WebSocket/Pusher.php index e2bee512a4..c24d94b817 100644 --- a/application/Espo/Core/WebSocket/Pusher.php +++ b/application/Espo/Core/WebSocket/Pusher.php @@ -177,7 +177,7 @@ class Pusher implements WampServerInterface private function getUserIdByAuthToken($authToken) { - return shell_exec($this->phpExecutablePath . " auth_token_check.php " . $authToken); + return shell_exec($this->phpExecutablePath . " command.php AuthTokenCheck " . $authToken); } protected function closeConnection(ConnectionInterface $connection) diff --git a/clear_cache.php b/clear_cache.php index dba31455d7..834483b371 100644 --- a/clear_cache.php +++ b/clear_cache.php @@ -27,11 +27,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -$sapiName = php_sapi_name(); - -if (substr($sapiName, 0, 3) != 'cli') { - die("Rebuild can be run only via CLI"); -} +if (substr(php_sapi_name(), 0, 3) != 'cli') die('ClearCache can be run only via CLI.'); include "bootstrap.php"; diff --git a/auth_token_check.php b/command.php similarity index 75% rename from auth_token_check.php rename to command.php index f79799719a..34e1778760 100644 --- a/auth_token_check.php +++ b/command.php @@ -27,33 +27,19 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -ob_start(); - if (substr(php_sapi_name(), 0, 3) != 'cli') exit; -$token = isset($_SERVER['argv'][1]) ? trim($_SERVER['argv'][1]) : null; -if (empty($token)) exit; +ob_start(); + +$command = isset($_SERVER['argv'][1]) ? trim($_SERVER['argv'][1]) : null; +if (empty($command)) exit; include "bootstrap.php"; - $app = new \Espo\Core\Application(); -$entityManager = $app->getContainer()->get('entityManager'); - -$authToken = $entityManager->getRepository('AuthToken')->where([ - 'token' => $token, - 'isActive' => true, -])->findOne(); - -if (!$authToken) exit; -if (!$authToken->get('userId')) exit; - -$userId = $authToken->get('userId'); - -$user = $entityManager->getEntity('User', $userId); -if (!$user) exit; - -ob_end_clean(); - -echo $user->id; +$result = $app->runCommand($command); +if (is_string($result)) { + ob_end_clean(); + echo $result; +} exit; diff --git a/cron.php b/cron.php index d3335c2d47..9c317afcda 100644 --- a/cron.php +++ b/cron.php @@ -27,10 +27,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -$sapiName = php_sapi_name(); -if (substr($sapiName, 0, 3) != 'cli') { - die("Daemon can be run only via CLI"); -} +if (substr(php_sapi_name(), 0, 3) != 'cli') die('Cron can be run only via CLI.'); include "bootstrap.php"; diff --git a/daemon.php b/daemon.php index e34a6dd3e6..10fab42006 100644 --- a/daemon.php +++ b/daemon.php @@ -27,10 +27,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -$sapiName = php_sapi_name(); -if (substr($sapiName, 0, 3) != 'cli') { - die("Cron can be run only via CLI"); -} +if (substr(php_sapi_name(), 0, 3) != 'cli') die('Daemon can be run only via CLI.'); include "bootstrap.php"; diff --git a/rebuild.php b/rebuild.php index 54cc8cfde9..56df0c733a 100644 --- a/rebuild.php +++ b/rebuild.php @@ -1,4 +1,4 @@ -runRebuild(); - diff --git a/websocket.php b/websocket.php index d83bef24cf..60fa22e85f 100644 --- a/websocket.php +++ b/websocket.php @@ -27,7 +27,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -if (substr(php_sapi_name(), 0, 3) != 'cli') die('Cron can be run only via CLI'); +if (substr(php_sapi_name(), 0, 3) != 'cli') die('WebSocket can be run only via CLI.'); include "bootstrap.php";