From f86a493ba6892550ff3c5ccca8a6b38c60ac9bfa Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 15 Jul 2020 17:33:18 +0300 Subject: [PATCH] app runners refactoring --- api/v1/index.php | 7 ++++- api/v1/portal-access/index.php | 7 ++++- application/Espo/Core/Application.php | 27 +++++-------------- .../Core/ApplicationRunners/EntryPoint.php | 2 +- application/Espo/Core/Portal/Application.php | 11 -------- application/Espo/Core/Utils/Cron/JobTask.php | 11 +++++--- application/Espo/EntryPoints/Portal.php | 3 ++- .../Espo/Resources/metadata/app/metadata.json | 1 - clear_cache.php | 7 ++++- command.php | 7 ++++- cron.php | 7 ++++- daemon.php | 7 ++++- index.php | 12 ++++++--- portal/index.php | 11 +++++--- rebuild.php | 7 ++++- 15 files changed, 76 insertions(+), 51 deletions(-) diff --git a/api/v1/index.php b/api/v1/index.php index fce4db8c4e..211cfa1f08 100644 --- a/api/v1/index.php +++ b/api/v1/index.php @@ -29,4 +29,9 @@ require_once('../../bootstrap.php'); -(new \Espo\Core\Application())->run('api'); +use Espo\Core\{ + Application, + ApplicationRunners\Api, +}; + +(new Application())->run(Api::class); diff --git a/api/v1/portal-access/index.php b/api/v1/portal-access/index.php index 73519a2dd6..e46870665c 100644 --- a/api/v1/portal-access/index.php +++ b/api/v1/portal-access/index.php @@ -29,10 +29,15 @@ require_once('../../../bootstrap.php'); +use Espo\Core\{ + Portal\Application, + Portal\ApplicationRunners\Api, +}; + if (!empty($_GET['portalId'])) { $portalId = $_GET['portalId']; } else { $portalId = explode('/', $_SERVER['REQUEST_URI'])[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1]; } -(new \Espo\Core\Portal\Application($portalId))->run('api'); +(new Application($portalId))->run(Api::class); diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 7a14cadc68..48569025c0 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -78,15 +78,13 @@ class Application } /** - * Run an application through a specific runner. - * You can find runner classes in `Espo\Core\ApplicationRunners`. + * Run a specific application runner. + * You can find runner classes at `Espo\Core\ApplicationRunners`. */ - public function run(string $runnerName, ?object $params = null) + public function run(string $className, ?object $params = null) { - $className = $this->getRunnerClassName($runnerName); - if (!$className) { - $this->getLog()->error("Application runner '{$runnerName}' does not exist."); + $this->getLog()->error("Application runner '{$className}' does not exist."); return; } @@ -94,9 +92,10 @@ class Application if ($class->getStaticPropertyValue('cli', false)) { if (substr(php_sapi_name(), 0, 3) !== 'cli') { - die("Application runner '{$runnerName}' can be run only via CLI."); + die("Can be run only via CLI."); } } + if ($class->getStaticPropertyValue('setupSystemUser', false)) { $this->setupSystemUser(); } @@ -106,20 +105,6 @@ class Application $runner->run($params); } - protected function getRunnerClassName(string $runnerName) : ?string - { - $className = 'Espo\\Core\\ApplicationRunners\\' . ucfirst($runnerName); - - if (!class_exists($className)) { - $className = $this->getMetadata()->get(['app', 'runners', $runnerName, 'className']); - if (!$className || !class_exists($className)) { - return null; - } - } - - return $className; - } - /** * Whether an application is installed. */ diff --git a/application/Espo/Core/ApplicationRunners/EntryPoint.php b/application/Espo/Core/ApplicationRunners/EntryPoint.php index 63e7c1ea3c..761e50654e 100644 --- a/application/Espo/Core/ApplicationRunners/EntryPoint.php +++ b/application/Espo/Core/ApplicationRunners/EntryPoint.php @@ -99,7 +99,7 @@ class EntryPoint implements ApplicationRunner if ($portalId = $this->detectPortalId()) { $app = new PortalApplication($portalId); $app->setClientBasePath($this->clientManager->getBasePath()); - $app->run('entryPoint', (object) [ + $app->run(EntryPoint::class, (object) [ 'entryPoint' => $entryPoint, 'data' => $data, 'final' => true, diff --git a/application/Espo/Core/Portal/Application.php b/application/Espo/Core/Portal/Application.php index 1a9fa235c5..18262017de 100644 --- a/application/Espo/Core/Portal/Application.php +++ b/application/Espo/Core/Portal/Application.php @@ -94,17 +94,6 @@ class Application extends BaseApplication return $this->portal; } - protected function getRunnerClassName(string $runnerName) : ?string - { - $className = 'Espo\\Core\\Portal\\ApplicationRunners\\' . ucfirst($runnerName); - - if (class_exists($className)) { - return $className; - } - - return parent::getRunnerClassName($runnerName); - } - protected function initPreloads() { parent::initPreloads(); diff --git a/application/Espo/Core/Utils/Cron/JobTask.php b/application/Espo/Core/Utils/Cron/JobTask.php index 3ace136215..f3ffe158fb 100644 --- a/application/Espo/Core/Utils/Cron/JobTask.php +++ b/application/Espo/Core/Utils/Cron/JobTask.php @@ -29,9 +29,14 @@ namespace Espo\Core\Utils\Cron; -use Espo\Core\Application; +use Espo\Core\{ + Application, + ApplicationRunners\Job as JobRunner, +}; -class JobTask extends \Spatie\Async\Task +use Spatie\Async\Task as AsyncTask; + +class JobTask extends AsyncTask { private $jobId; @@ -48,7 +53,7 @@ class JobTask extends \Spatie\Async\Task { $app = new Application(); try { - $app->run('job', (object) [ + $app->run(JobRunner::class, (object) [ 'id' => $this->jobId, ]); } catch (\Throwable $e) { diff --git a/application/Espo/EntryPoints/Portal.php b/application/Espo/EntryPoints/Portal.php index 15fb52c873..bf06ed71df 100644 --- a/application/Espo/EntryPoints/Portal.php +++ b/application/Espo/EntryPoints/Portal.php @@ -40,6 +40,7 @@ use Espo\Core\{ Utils\ClientManager, Utils\Config, Portal\Application as PortalApplication, + Portal\ApplicationRunners\Client, Api\Request, Api\Response, }; @@ -84,6 +85,6 @@ class Portal implements EntryPoint $application = new PortalApplication($id); $application->setClientBasePath($this->clientManager->getBasePath()); - $application->run('client'); + $application->run(Client::class); } } diff --git a/application/Espo/Resources/metadata/app/metadata.json b/application/Espo/Resources/metadata/app/metadata.json index fd7e82003c..d70514a30c 100644 --- a/application/Espo/Resources/metadata/app/metadata.json +++ b/application/Espo/Resources/metadata/app/metadata.json @@ -13,7 +13,6 @@ ["app", "templateHelpers"], ["app", "appParams"], ["app", "cleanup"], - ["app", "runners"], ["app", "auth2FAMethods", "__ANY__", "implementationClassName"], ["app", "auth2FAMethods", "__ANY__", "implementationUserClassName"], ["authenticationMethods", "__ANY__", "implementationClassName"] diff --git a/clear_cache.php b/clear_cache.php index fcf8d453fb..c1d2f0065e 100644 --- a/clear_cache.php +++ b/clear_cache.php @@ -29,4 +29,9 @@ include "bootstrap.php"; -(new \Espo\Core\Application())->run('clearCache'); +use Espo\Core\{ + Application, + ApplicationRunners\ClearCache, +}; + +(new Application())->run(ClearCache::class); diff --git a/command.php b/command.php index 4ba76747fd..0cb00bfebe 100644 --- a/command.php +++ b/command.php @@ -29,9 +29,14 @@ include "bootstrap.php"; +use Espo\Core\{ + Application, + ApplicationRunners\Command, +}; + ob_start(); -$result = (new \Espo\Core\Application())->run('command'); +$result = (new Application())->run(Command::class); if (is_string($result)) { ob_end_clean(); diff --git a/cron.php b/cron.php index 7ac8e6138e..7b50eba8ad 100644 --- a/cron.php +++ b/cron.php @@ -29,4 +29,9 @@ include "bootstrap.php"; -(new \Espo\Core\Application())->run('cron'); +use Espo\Core\{ + Application, + ApplicationRunners\Cron, +}; + +(new Application())->run(Cron::class); diff --git a/daemon.php b/daemon.php index 3c9c9e5580..5b07405e9e 100644 --- a/daemon.php +++ b/daemon.php @@ -29,4 +29,9 @@ include "bootstrap.php"; -(new \Espo\Core\Application())->run('daemon'); +use Espo\Core\{ + Application, + ApplicationRunners\Daemon, +}; + +(new Application())->run(Daemon::class); diff --git a/index.php b/index.php index 8c825ee082..4b09c71df7 100644 --- a/index.php +++ b/index.php @@ -29,7 +29,13 @@ include "bootstrap.php"; -$app = new \Espo\Core\Application(); +use Espo\Core\{ + Application, + ApplicationRunners\Client, + ApplicationRunners\EntryPoint, +}; + +$app = new Application(); if (!$app->isInstalled()) { header("Location: install/"); @@ -37,8 +43,8 @@ if (!$app->isInstalled()) { } if (!empty($_GET['entryPoint'])) { - $app->run('entryPoint'); + $app->run(EntryPoint::class); exit; } -$app->run('client'); +$app->run(Client::class); diff --git a/portal/index.php b/portal/index.php index d3faa09087..155c20bbc1 100644 --- a/portal/index.php +++ b/portal/index.php @@ -29,7 +29,12 @@ include "../bootstrap.php"; -$app = new \Espo\Core\Application(); +use Espo\Core\{ + Application, + ApplicationRunners\EntryPoint, +}; + +$app = new Application(); if (!$app->isInstalled()) { exit; } @@ -70,10 +75,10 @@ if (strpos($requestUri, '/') !== false) { } if (!empty($_GET['entryPoint'])) { - $app->run('entryPoint'); + $app->run(EntryPoint::class); exit; } -$app->run('entryPoint', (object) [ +$app->run(EntryPoint::class, (object) [ 'entryPoint' => 'portal', ]); diff --git a/rebuild.php b/rebuild.php index 7c36636501..50b32032a1 100644 --- a/rebuild.php +++ b/rebuild.php @@ -29,4 +29,9 @@ include "bootstrap.php"; -(new \Espo\Core\Application())->run('rebuild'); +use Espo\Core\{ + Application, + ApplicationRunners\Rebuild, +}; + +(new Application())->run(Rebuild::class);