Skip to content

Commit

Permalink
Команды.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 13, 2021
1 parent 1bde756 commit 53d3e95
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 1 deletion.
39 changes: 39 additions & 0 deletions bin/rabbit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Bitrix\Main\Loader;
use Proklung\RabbitMq\Integration\CLI\CommandsSetup;
use Proklung\RabbitMq\Integration\CLI\ConsoleCommandConfigurator;
use Proklung\RabbitMq\Integration\CLI\LoaderBitrix;
use Proklung\RabbitMq\Integration\DI\Services;
use Symfony\Component\Console\Application;

@set_time_limit(0);

$_SERVER['DOCUMENT_ROOT'] = __DIR__. DIRECTORY_SEPARATOR . '..';
$GLOBALS['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];

$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';

/** @noinspection PhpIncludeInspection */
require_once $autoloadPath;

/**
* Загрузить Битрикс.
*/
$loaderBitrix = new LoaderBitrix();
$loaderBitrix->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();
2 changes: 1 addition & 1 deletion lib/Integration/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
39 changes: 39 additions & 0 deletions lib/Integration/CLI/CommandsSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Proklung\RabbitMq\Integration\CLI;

use Proklung\RabbitMq\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class CommandsSetup
* @package Proklung\RabbitMq\CLI
*/
class CommandsSetup
{
/**
* @param ContainerInterface $container
*
* @return array
*/
public static function load(ContainerInterface $container)
{
$commands = [
new Command\ConsumerCommand(),
new Command\DeleteCommand(),
new Command\PurgeConsumerCommand(),
new Command\SetupFabricCommand(),
new Command\StdInProducerCommand(),
];

foreach ($commands as $command) {
if (!$command instanceof Command\BaseRabbitMqCommand) {
continue;
}

$command->setContainer($container);
}

return $commands;
}
}
92 changes: 92 additions & 0 deletions lib/Integration/CLI/ConsoleCommandConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Proklung\RabbitMq\Integration\CLI;

use Exception;
use IteratorAggregate;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;

/**
* Class ConsoleCommandConfigurator
* @package Proklung\RabbitMq\Integration\CLI
*
*/
class ConsoleCommandConfigurator
{
/**
* @var Application $application Конфигуратор консольных команд.
*/
private $application;

/**
* @var Command[] $commands Команды.
*/
private $commands;

/**
* ConsoleCommandConfigurator constructor.
*
* @param Application $application Конфигуратор консольных команд.
* @param Command ...$commands Команды.
*/
public function __construct(
Application $application,
Command ...$commands
) {
$this->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);
}
}
Loading

0 comments on commit 53d3e95

Please sign in to comment.