From 53d3e95a9d902a4a117ff7c6439dbf6304fd3f08 Mon Sep 17 00:00:00 2001 From: ProklUng Date: Tue, 13 Jul 2021 12:27:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/rabbit.php | 39 +++ lib/Integration/CLI/Commands.php | 2 +- lib/Integration/CLI/CommandsSetup.php | 39 +++ .../CLI/ConsoleCommandConfigurator.php | 92 +++++++ lib/Integration/CLI/LoaderBitrix.php | 236 ++++++++++++++++++ lib/Integration/DI/Services.php | 5 + 6 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 bin/rabbit.php create mode 100644 lib/Integration/CLI/CommandsSetup.php create mode 100644 lib/Integration/CLI/ConsoleCommandConfigurator.php create mode 100644 lib/Integration/CLI/LoaderBitrix.php diff --git a/bin/rabbit.php b/bin/rabbit.php new file mode 100644 index 0000000..a7c5ed2 --- /dev/null +++ b/bin/rabbit.php @@ -0,0 +1,39 @@ +setDocumentRoot($_SERVER['DOCUMENT_ROOT']); +$loaderBitrix->initializeBitrix(); + +if (!$loaderBitrix->isBitrixLoaded()) { + exit('Bitrix not initialized.'); +} + +Loader::includeModule('proklung.rabbitmq'); + +$application = new ConsoleCommandConfigurator( + new Application(), + CommandsSetup::load(Services::getInstance()) +); + +$application->init(); +$application->run(); \ No newline at end of file diff --git a/lib/Integration/CLI/Commands.php b/lib/Integration/CLI/Commands.php index 264c217..b3afcb4 100644 --- a/lib/Integration/CLI/Commands.php +++ b/lib/Integration/CLI/Commands.php @@ -36,6 +36,6 @@ public static function onCommandsLoad(Event $event) $command->setContainer($container); } - return new EventResult(EventResult::SUCCESS, $commands, 'yngc0der.rabbitmq'); + return new EventResult(EventResult::SUCCESS, $commands, 'proklung.rabbitmq'); } } diff --git a/lib/Integration/CLI/CommandsSetup.php b/lib/Integration/CLI/CommandsSetup.php new file mode 100644 index 0000000..68a2c24 --- /dev/null +++ b/lib/Integration/CLI/CommandsSetup.php @@ -0,0 +1,39 @@ +setContainer($container); + } + + return $commands; + } +} diff --git a/lib/Integration/CLI/ConsoleCommandConfigurator.php b/lib/Integration/CLI/ConsoleCommandConfigurator.php new file mode 100644 index 0000000..8beab01 --- /dev/null +++ b/lib/Integration/CLI/ConsoleCommandConfigurator.php @@ -0,0 +1,92 @@ +application = $application; + $this->commands = $commands; + } + + /** + * Инициализация команд. + * + * @return $this + */ + public function init() : self + { + foreach ($this->commands as $command) { + $this->application->add($command); + } + + return $this; + } + + /** + * Запуск команд. + * + * @throws Exception + */ + public function run() : void + { + $this->application->run(); + } + + /** + * Добавить команды. + * + * @param mixed $commands Команды + * + * @return void + * + * @throws Exception + * @since 24.12.2020 Рефакторинг. + */ + public function add(...$commands) : void + { + $result = []; + + foreach ($commands as $command) { + $array = $command; + if ($command instanceof IteratorAggregate) { + $iterator = $command->getIterator(); + $array = iterator_to_array($iterator); + } + + $result[] = $array; + } + + /** @psalm-suppress InvalidPropertyAssignmentValue */ + $this->commands = array_merge($this->commands, $result); + } +} diff --git a/lib/Integration/CLI/LoaderBitrix.php b/lib/Integration/CLI/LoaderBitrix.php new file mode 100644 index 0000000..f2d00d6 --- /dev/null +++ b/lib/Integration/CLI/LoaderBitrix.php @@ -0,0 +1,236 @@ +documentRoot === null) { + $this->documentRoot = $this->findBitrixCorePath(); + } + + if ($this->bitrixStatus === static::BITRIX_STATUS_COMPLETE) { + return static::BITRIX_STATUS_COMPLETE; + } elseif (!$this->checkBitrix()) { + return static::BITRIX_STATUS_UNAVAILABLE; + } + + define('BITRIX_CLI', true); + define('NO_KEEP_STATISTIC', true); + define('NOT_CHECK_PERMISSIONS', true); + define('LANGUAGE_ID', 'pa'); + define('LOG_FILENAME', 'php://stderr'); + define('BX_NO_ACCELERATOR_RESET', true); + define('STOP_STATISTICS', true); + define('NO_AGENT_STATISTIC', 'Y'); + define('NO_AGENT_CHECK', true); + defined('PUBLIC_AJAX_MODE') || define('PUBLIC_AJAX_MODE', true); + + try { + /** + * Declare global legacy variables + * + * Including kernel here makes them local by default but some modules depend on them in installation class + */ + + global + /** @noinspection PhpUnusedLocalVariableInspection */ + $DB, $DBType, $DBHost, $DBLogin, $DBPassword, + $DBName, $DBDebug, $DBDebugToFile, $APPLICATION, $USER, $DBSQLServerType; + + require_once $this->documentRoot . '/bitrix/modules/main/include/prolog_before.php'; + + if (defined('B_PROLOG_INCLUDED') && B_PROLOG_INCLUDED === true) { + $this->bitrixStatus = static::BITRIX_STATUS_COMPLETE; + } + + // Альтернативный способ вывода ошибок типа "DB query error.": + $GLOBALS['DB']->debug = true; + + $app = Application::getInstance(); + $con = $app->getConnection(); + $DB->db_Conn = $con->getResource(); + + if (in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) === false) { + echo 'Warning: The console should be invoked via the CLI version of PHP, not the ' + . \PHP_SAPI . ' SAPI' . \PHP_EOL; + } + } catch (ConnectionException $e) { + $this->bitrixStatus = static::BITRIX_STATUS_NO_DB_CONNECTION; + } + + return $this->bitrixStatus; + } + + /** + * Попытка найти путь к ядру Битрикса. + * + * @return string + * + * @throws Exception Когда директория с Битриксом существует, но в ней нет .settings.php. + * + * @since 24.05.2021 + */ + public function findBitrixCorePath(): string + { + foreach ($this->defaultPaths as $path) { + $normalizedPath = $this->normalizePath($path); + + if (file_exists($normalizedPath)) { + $pathBitrix = getcwd() . DIRECTORY_SEPARATOR . $path; + if (!is_file($pathBitrix . '/bitrix/.settings.php')) { + throw new Exception( + 'Path bitrix exist, but file bitrix/.settings.php not exist. Wrong!' + ); + } + + return $pathBitrix; + } + } + + throw new Exception('Wrong document root or bitrix is not found.'); + } + + /** + * Checks readiness of Bitrix for kernel initialize. + * + * @return boolean + */ + public function checkBitrix() : bool + { + if (!is_file($this->documentRoot . '/bitrix/.settings.php')) { + return false; + } + + return true; + } + + /** + * Gets Bitrix status. + * + * @return integer Value of constant `Application::BITRIX_STATUS_*`. + */ + public function getBitrixStatus() : int + { + return $this->bitrixStatus; + } + + /** + * Checks that the Bitrix kernel is loaded. + * + * @return boolean + */ + public function isBitrixLoaded() : bool + { + return $this->bitrixStatus === static::BITRIX_STATUS_COMPLETE; + } + + /** + * Sets path to the document root of site. + * + * @param string $dir Path to document root. + * + * @return void + */ + public function setDocumentRoot(string $dir) : void + { + $_SERVER['DOCUMENT_ROOT'] = $this->documentRoot = $dir; + } + + /** + * Gets document root of site. + * + * @return null|string + */ + public function getDocumentRoot() : ?string + { + return $this->documentRoot; + } + + /** + * Нормализовать путь. + * + * @param string $path Путь. + * + * @return string + */ + private function normalizePath(string $path): string + { + return \realpath( + \implode( + \DIRECTORY_SEPARATOR, + [ + \getcwd(), + $path, + $this->prologPath + ] + ) + ) ?: ''; + } +} diff --git a/lib/Integration/DI/Services.php b/lib/Integration/DI/Services.php index e06fed0..f024d7c 100644 --- a/lib/Integration/DI/Services.php +++ b/lib/Integration/DI/Services.php @@ -116,6 +116,11 @@ public function load() : void $this->containerBuilder->compile(false); } + public function setupCommands() : void + { + + } + /** * Экземпляр контейнера. *