-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAbrouterServiceProvider.php
155 lines (128 loc) · 4.23 KB
/
AbrouterServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types = 1);
namespace Abrouter\LaravelClient\Providers;
use Abrouter\Client\Config\Config;
use Abrouter\Client\Config\ParallelRunConfig;
use Abrouter\Client\Contracts\KvStorageContract;
use Abrouter\Client\Contracts\TaskManagerContract;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use RuntimeException;
use Illuminate\Config\Repository;
use Abrouter\LaravelClient\Bridge\KvStorage;
/**
* Class AbrouterServiceProvider
* @package Abrouter\LaravelClient
*/
class AbrouterServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
$this->configPath() => $this->getApplicationConfigPath(),
], 'abrouter');
}
}
public function register(): void
{
$this->mergeConfigFrom($this->configPath(), 'abrouter');
$config = new Config(
(string) $this->getConfig()->get('abrouter.token'),
(string) $this->getConfig()->get('abrouter.host')
);
$kvStorageConfigured = false;
if ($this->configureKvStorage()) {
$config->setKvStorageConfig($this->getContainer()->make(KvStorageContract::class));
$kvStorageConfigured = true;
}
if ($kvStorageConfigured && $this->isParallelRunningEnabled()) {
$this->configureParallelRunning();
$config->setParallelRunConfig(new ParallelRunConfig(
true,
$this->getContainer()->make(TaskManagerContract::class),
));
}
$this->getContainer()->singleton(Config::class, function () use ($config) {
return $config;
});
}
/**
* @return string
*/
protected function configPath(): string
{
return __DIR__ . '/../../Config/abrouter.php';
}
/**
* Gets Illuminate Container.
*
* @return Application|Container
*/
protected function getContainer()
{
if ($this->app instanceof Container) {
return $this->app;
}
if (function_exists('app')) {
$app = app();
if ($app instanceof Container) {
$this->app = $app;
return $app;
}
}
throw new RuntimeException('Unable to locate Illuminate Container');
}
/**
* @return string
*/
private function getApplicationConfigPath(): string
{
return $this->app->configPath('abrouter.php');
}
private function configureKvStorage(): bool
{
$kvStorage = $this->getConfig()->get('abrouter.kvStorage');
if (empty($kvStorage)) {
return false;
}
$this->getContainer()->singleton(KvStorageContract::class, function () use ($kvStorage) {
return $this->getContainer()->make($kvStorage);
});
return true;
}
private function isParallelRunningEnabled(): bool
{
$parallelRunningConfig = $this->getConfig()->get('abrouter.parallelRunning');
if (!is_array($parallelRunningConfig)) {
return false;
}
if (!isset($parallelRunningConfig['enabled'])) {
return false;
}
if (empty($parallelRunningConfig['taskManager'])) {
return false;
}
return (bool) $parallelRunningConfig['enabled'];
}
private function configureParallelRunning(): void
{
$taskManager = $this->getConfig()->get('abrouter.parallelRunning.taskManager');
if (empty($taskManager)) {
throw new RuntimeException('TaskManager class is not configured');
}
$this->getContainer()->singleton(TaskManagerContract::class, function () use ($taskManager) {
return $this->getContainer()->make($taskManager);
});
$this->getContainer()->register(EventServiceProvider::class);
}
private function getConfig(): Repository
{
/**
* @var Repository $configRepository
*/
$configRepository = $this->getContainer()->make(Repository::class);
return $configRepository;
}
}