ConsoleCommand framework

This commit is contained in:
yuri
2019-02-08 16:44:54 +02:00
parent 790a206e0a
commit 983988c9e1
15 changed files with 294 additions and 46 deletions

View File

@@ -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',

View File

@@ -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();

View File

@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Console;
class CommandManager
{
private $container;
public function __construct(\Espo\Core\Container $container)
{
$this->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();
}
}

View File

@@ -0,0 +1,56 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Console\Commands;
class AuthTokenCheck extends Base
{
public function run()
{
$token = isset($_SERVER['argv'][2]) ? trim($_SERVER['argv'][2]) : null;
if (empty($token)) return;
$entityManager = $this->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;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Console\Commands;
abstract class Base
{
private $container;
public function __construct(\Espo\Core\Container $container)
{
$this->container = $container;
}
protected function getContainer()
{
return $this->container;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Console\Commands;
class ClearCache extends Base
{
public function run()
{
$this->getContainer()->get('dataManager')->clearCache();
echo "Cache has been cleared.\n";
}
}

View File

@@ -0,0 +1,39 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Console\Commands;
class Rebuild extends Base
{
public function run()
{
$this->getContainer()->get('dataManager')->rebuild();
echo "Rebuild has been done.\n";
}
}

View File

@@ -0,0 +1,40 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Loaders;
class ConsoleCommandManager extends Base
{
public function load()
{
return new \Espo\Core\Console\CommandManager(
$this->getContainer()
);
}
}

View File

@@ -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)

View File

@@ -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";

View File

@@ -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;

View File

@@ -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";

View File

@@ -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";

View File

@@ -1,4 +1,4 @@
<?php
<?php
/************************************************************************
* This file is part of EspoCRM.
*
@@ -27,14 +27,9 @@
* 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('Rebuild can be run only via CLI.');
include "bootstrap.php";
$app = new \Espo\Core\Application();
$app->runRebuild();

View File

@@ -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";