Skip to content

Commit 53d3e95

Browse files
committed
Команды.
1 parent 1bde756 commit 53d3e95

File tree

6 files changed

+412
-1
lines changed

6 files changed

+412
-1
lines changed

bin/rabbit.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Bitrix\Main\Loader;
4+
use Proklung\RabbitMq\Integration\CLI\CommandsSetup;
5+
use Proklung\RabbitMq\Integration\CLI\ConsoleCommandConfigurator;
6+
use Proklung\RabbitMq\Integration\CLI\LoaderBitrix;
7+
use Proklung\RabbitMq\Integration\DI\Services;
8+
use Symfony\Component\Console\Application;
9+
10+
@set_time_limit(0);
11+
12+
$_SERVER['DOCUMENT_ROOT'] = __DIR__. DIRECTORY_SEPARATOR . '..';
13+
$GLOBALS['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
14+
15+
$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
16+
17+
/** @noinspection PhpIncludeInspection */
18+
require_once $autoloadPath;
19+
20+
/**
21+
* Загрузить Битрикс.
22+
*/
23+
$loaderBitrix = new LoaderBitrix();
24+
$loaderBitrix->setDocumentRoot($_SERVER['DOCUMENT_ROOT']);
25+
$loaderBitrix->initializeBitrix();
26+
27+
if (!$loaderBitrix->isBitrixLoaded()) {
28+
exit('Bitrix not initialized.');
29+
}
30+
31+
Loader::includeModule('proklung.rabbitmq');
32+
33+
$application = new ConsoleCommandConfigurator(
34+
new Application(),
35+
CommandsSetup::load(Services::getInstance())
36+
);
37+
38+
$application->init();
39+
$application->run();

lib/Integration/CLI/Commands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public static function onCommandsLoad(Event $event)
3636
$command->setContainer($container);
3737
}
3838

39-
return new EventResult(EventResult::SUCCESS, $commands, 'yngc0der.rabbitmq');
39+
return new EventResult(EventResult::SUCCESS, $commands, 'proklung.rabbitmq');
4040
}
4141
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Proklung\RabbitMq\Integration\CLI;
4+
5+
use Proklung\RabbitMq\Command;
6+
use Symfony\Component\DependencyInjection\ContainerInterface;
7+
8+
/**
9+
* Class CommandsSetup
10+
* @package Proklung\RabbitMq\CLI
11+
*/
12+
class CommandsSetup
13+
{
14+
/**
15+
* @param ContainerInterface $container
16+
*
17+
* @return array
18+
*/
19+
public static function load(ContainerInterface $container)
20+
{
21+
$commands = [
22+
new Command\ConsumerCommand(),
23+
new Command\DeleteCommand(),
24+
new Command\PurgeConsumerCommand(),
25+
new Command\SetupFabricCommand(),
26+
new Command\StdInProducerCommand(),
27+
];
28+
29+
foreach ($commands as $command) {
30+
if (!$command instanceof Command\BaseRabbitMqCommand) {
31+
continue;
32+
}
33+
34+
$command->setContainer($container);
35+
}
36+
37+
return $commands;
38+
}
39+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Proklung\RabbitMq\Integration\CLI;
4+
5+
use Exception;
6+
use IteratorAggregate;
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Command\Command;
9+
10+
/**
11+
* Class ConsoleCommandConfigurator
12+
* @package Proklung\RabbitMq\Integration\CLI
13+
*
14+
*/
15+
class ConsoleCommandConfigurator
16+
{
17+
/**
18+
* @var Application $application Конфигуратор консольных команд.
19+
*/
20+
private $application;
21+
22+
/**
23+
* @var Command[] $commands Команды.
24+
*/
25+
private $commands;
26+
27+
/**
28+
* ConsoleCommandConfigurator constructor.
29+
*
30+
* @param Application $application Конфигуратор консольных команд.
31+
* @param Command ...$commands Команды.
32+
*/
33+
public function __construct(
34+
Application $application,
35+
Command ...$commands
36+
) {
37+
$this->application = $application;
38+
$this->commands = $commands;
39+
}
40+
41+
/**
42+
* Инициализация команд.
43+
*
44+
* @return $this
45+
*/
46+
public function init() : self
47+
{
48+
foreach ($this->commands as $command) {
49+
$this->application->add($command);
50+
}
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Запуск команд.
57+
*
58+
* @throws Exception
59+
*/
60+
public function run() : void
61+
{
62+
$this->application->run();
63+
}
64+
65+
/**
66+
* Добавить команды.
67+
*
68+
* @param mixed $commands Команды
69+
*
70+
* @return void
71+
*
72+
* @throws Exception
73+
* @since 24.12.2020 Рефакторинг.
74+
*/
75+
public function add(...$commands) : void
76+
{
77+
$result = [];
78+
79+
foreach ($commands as $command) {
80+
$array = $command;
81+
if ($command instanceof IteratorAggregate) {
82+
$iterator = $command->getIterator();
83+
$array = iterator_to_array($iterator);
84+
}
85+
86+
$result[] = $array;
87+
}
88+
89+
/** @psalm-suppress InvalidPropertyAssignmentValue */
90+
$this->commands = array_merge($this->commands, $result);
91+
}
92+
}

0 commit comments

Comments
 (0)