1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MallardDuck\DynamicEcho; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Facades\Blade; |
7
|
|
|
use Illuminate\Support\Facades\Broadcast; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use MallardDuck\DynamicEcho\Collections\{ChannelAwareEventCollection, ChannelEventCollection}; |
10
|
|
|
use MallardDuck\DynamicEcho\Commands\InstallExamplesCommand; |
11
|
|
|
use MallardDuck\DynamicEcho\Commands\PrintChannels; |
12
|
|
|
use MallardDuck\DynamicEcho\Composer\CacheResolver; |
13
|
|
|
use MallardDuck\DynamicEcho\Loader\EventContractLoader; |
14
|
|
|
use MallardDuck\DynamicEcho\ScriptGenerator\ScriptGenerator; |
15
|
|
|
|
16
|
|
|
class DynamicEchoServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Register services. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
28 |
|
public function register(): void |
24
|
|
|
{ |
25
|
28 |
|
$this->registerComposerResolver(); |
26
|
28 |
|
$this->mergeConfigFrom(__DIR__ . '/../config/dynamic-echo.php', 'dynamic-echo'); |
27
|
28 |
|
$this->registerServices(); |
28
|
28 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Bootstrap services. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
28 |
|
public function boot(): void |
36
|
|
|
{ |
37
|
28 |
|
$this->registerConfigs(); |
38
|
28 |
|
$this->registerViews(); |
39
|
28 |
|
$this->registerBladeDirectives(); |
40
|
28 |
|
$this->registerEchoChannels(); |
41
|
28 |
|
$this->registerConsoleCommands(); |
42
|
28 |
|
} |
43
|
|
|
|
44
|
28 |
|
protected function registerComposerResolver(): void |
45
|
|
|
{ |
46
|
28 |
|
$this->app->singleton(CacheResolver::class, static function ($app) { |
47
|
28 |
|
return (new CacheResolver( |
48
|
28 |
|
$app->config->get('dynamic-echo.namespace', "App\\Events"), |
49
|
28 |
|
$app->bootstrapPath('cache/dynamic-echo-discovery.php') |
50
|
|
|
)); |
51
|
28 |
|
}); |
52
|
28 |
|
} |
53
|
|
|
|
54
|
28 |
|
protected function registerServices(): void |
55
|
|
|
{ |
56
|
28 |
|
$this->app->singleton(ChannelManager::class, static function () { |
57
|
28 |
|
return new ChannelManager(); |
58
|
28 |
|
}); |
59
|
|
|
|
60
|
28 |
|
$this->app->singleton(EventContractLoader::class, static function ($app) { |
61
|
28 |
|
return new EventContractLoader( |
62
|
28 |
|
$app->make(ChannelManager::class), |
63
|
28 |
|
$app->make(CacheResolver::class)->setFilesystem($app->make(Filesystem::class))->build() |
64
|
|
|
); |
65
|
28 |
|
}); |
66
|
|
|
|
67
|
28 |
|
$this->app->singleton(ScriptGenerator::class, static function () { |
68
|
3 |
|
return new ScriptGenerator(); |
69
|
28 |
|
}); |
70
|
|
|
|
71
|
28 |
|
$this->app->singleton(DynamicEchoService::class, static function ($app) { |
72
|
1 |
|
return new DynamicEchoService( |
73
|
1 |
|
$app->make(ChannelManager::class), |
74
|
1 |
|
$app->make(ScriptGenerator::class), |
75
|
|
|
); |
76
|
28 |
|
}); |
77
|
|
|
|
78
|
28 |
|
$this->app->alias(DynamicEchoService::class, 'dynamic-echo'); |
79
|
28 |
|
$this->app->alias(ChannelManager::class, 'dynamic-echo::channel-manager'); |
80
|
28 |
|
} |
81
|
|
|
|
82
|
28 |
|
protected function registerConfigs(): void |
83
|
|
|
{ |
84
|
28 |
|
if ($this->app->runningInConsole()) { |
85
|
28 |
|
$this->publishes([ |
86
|
28 |
|
__DIR__ . '/../config/dynamic-echo.php' => base_path('config/dynamic-echo.php'), |
87
|
28 |
|
], 'config'); |
88
|
|
|
} |
89
|
28 |
|
} |
90
|
|
|
|
91
|
28 |
|
protected function registerViews(): void |
92
|
|
|
{ |
93
|
28 |
|
if ($this->app->runningInConsole()) { |
94
|
28 |
|
$this->publishes([ |
95
|
28 |
|
__DIR__ . '/../resources/views' => base_path('resources/views/vendor/dynamic-echo-events'), |
96
|
28 |
|
], 'views'); |
97
|
|
|
} |
98
|
|
|
|
99
|
28 |
|
$this->loadViewsFrom( |
100
|
28 |
|
__DIR__ . '/../resources/views', |
101
|
28 |
|
'dynamicEcho' |
102
|
|
|
); |
103
|
28 |
|
} |
104
|
|
|
|
105
|
28 |
|
protected function registerBladeDirectives(): void |
106
|
|
|
{ |
107
|
28 |
|
Blade::directive('dynamicEchoContext', [DynamicEchoBladeDirectives::class, 'dynamicEchoContext']); |
108
|
28 |
|
Blade::directive('dynamicEchoScripts', [DynamicEchoBladeDirectives::class, 'dynamicEchoScripts']); |
109
|
28 |
|
} |
110
|
|
|
|
111
|
28 |
|
protected function registerEchoChannels(): void |
112
|
|
|
{ |
113
|
|
|
/** |
114
|
|
|
* @var EventContractLoader $loader |
115
|
|
|
*/ |
116
|
28 |
|
$eventContractLoader = $this->app->make(EventContractLoader::class); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @var ChannelAwareEventCollection $channels |
120
|
|
|
*/ |
121
|
28 |
|
$channels = $eventContractLoader->load(); |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @var string $channelName |
125
|
|
|
* @var ChannelEventCollection $channelGroup |
126
|
|
|
*/ |
127
|
28 |
|
foreach ($channels as $channelName => $channelGroup) { |
128
|
|
|
Broadcast::channel($channelGroup->getChannelAuthName(), $channelGroup->getChannelAuthCallback()); |
129
|
|
|
} |
130
|
28 |
|
} |
131
|
|
|
|
132
|
28 |
|
protected function registerConsoleCommands(): void |
133
|
|
|
{ |
134
|
28 |
|
if ($this->app->runningInConsole()) { |
135
|
28 |
|
$this->commands([ |
136
|
28 |
|
PrintChannels::class, |
137
|
|
|
InstallExamplesCommand::class, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
28 |
|
} |
141
|
|
|
} |
142
|
|
|
|