1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of amqp |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Amqp; |
13
|
|
|
|
14
|
|
|
use Dotenv\Dotenv; |
15
|
|
|
use Slick\ModuleApi\Infrastructure\AbstractModule; |
16
|
|
|
use Slick\ModuleApi\Infrastructure\Console\ConsoleModuleInterface; |
17
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface; |
18
|
|
|
use function Slick\ModuleApi\importSettingsFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AmqpModule |
22
|
|
|
* |
23
|
|
|
* @package Slick\Amqp |
24
|
|
|
*/ |
25
|
|
|
final class AmqpModule extends AbstractModule implements ConsoleModuleInterface, WebModuleInterface |
26
|
|
|
{ |
27
|
|
|
public static string $amqpCnfFile = APP_ROOT . '/config/modules/amqp.php'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns a description of enabling message exchange using AMQP and advanced producer/consumer configurations. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function description(): string |
35
|
|
|
{ |
36
|
|
|
return "Enable message exchange using AMQP and advanced producer/consumer configurations."; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function settings(Dotenv $dotenv): array |
43
|
|
|
{ |
44
|
|
|
$settingsFile = APP_ROOT .'/config/modules/amqp.php'; |
45
|
|
|
$defaultSettings = [ |
46
|
|
|
'amqp' => [ |
47
|
|
|
'server' => "localhost", |
48
|
|
|
'port' => 5672, |
49
|
|
|
'user' => 'guest', |
50
|
|
|
'password' => 'guest' |
51
|
|
|
] |
52
|
|
|
]; |
53
|
|
|
return importSettingsFile($settingsFile, $defaultSettings); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function services(): array |
57
|
|
|
{ |
58
|
|
|
return importSettingsFile(dirname(__DIR__) . '/config/services.php'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function onEnable(array $context = []): void |
65
|
|
|
{ |
66
|
|
|
if (is_file(self::$amqpCnfFile)) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
file_put_contents(self::$amqpCnfFile, file_get_contents(dirname(__DIR__) . '/config/default_settings.php')); |
71
|
|
|
$this->verifyEnvironment(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function verifyEnvironment(): void |
75
|
|
|
{ |
76
|
|
|
$file = APP_ROOT . '/.env'; |
77
|
|
|
$append = [ |
78
|
|
|
'# AMQP ENVIRONMENT.', |
79
|
|
|
'# This will be used in the config/modules/amqp.php settings file.', |
80
|
|
|
'', |
81
|
|
|
'# AMQP_SERVER=localhost', |
82
|
|
|
'# AMQP_PORT=5672', |
83
|
|
|
'# AMQP_USER=guest', |
84
|
|
|
'# AMQP_PASSWORD=guest', |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
if (!file_exists($file)) { |
88
|
|
|
file_put_contents($file, implode("\n", $append)); |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$content = file_get_contents($file); |
93
|
|
|
if (is_string($content) && str_contains($content, 'AMQP_SERVER=')) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
file_put_contents($file, $content . "\n" . implode("\n", $append)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|