1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/vuongxuongminh/laravel-async |
4
|
|
|
* |
5
|
|
|
* @copyright (c) Vuong Xuong Minh |
6
|
|
|
* @license [MIT](https://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace VXM\Async; |
10
|
|
|
|
11
|
|
|
use Illuminate\Contracts\Support\DeferrableProvider; |
12
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
13
|
|
|
use VXM\Async\Commands\JobMakeCommand; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Vuong Minh <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class AsyncServiceProvider extends BaseServiceProvider implements DeferrableProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Package boot. |
23
|
|
|
*/ |
24
|
|
|
public function boot(): void |
25
|
|
|
{ |
26
|
|
|
$this->publishConfigs(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Publish async config files. |
31
|
|
|
*/ |
32
|
|
|
protected function publishConfigs(): void |
33
|
|
|
{ |
34
|
|
|
$this->publishes([ |
35
|
|
|
__DIR__.'/../config/async.php' => config_path('async.php'), |
36
|
|
|
], 'config'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function register(): void |
43
|
|
|
{ |
44
|
|
|
$this->mergeDefaultConfigs(); |
45
|
|
|
$this->registerServices(); |
46
|
|
|
$this->registerCommands(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Merge default async config to config service. |
51
|
|
|
*/ |
52
|
|
|
protected function mergeDefaultConfigs(): void |
53
|
|
|
{ |
54
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/async.php', 'async'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register package services. |
59
|
|
|
*/ |
60
|
|
|
protected function registerServices(): void |
61
|
|
|
{ |
62
|
|
|
$this->app->singleton('command.async.make', JobMakeCommand::class); |
63
|
|
|
|
64
|
|
|
$this->app->singleton('async', function ($app) { |
65
|
|
|
return new Async($app['async.pool'], $app['events']); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->app->singleton('async.pool', function ($app) { |
69
|
|
|
$pool = new Pool(); |
70
|
|
|
$config = $app['config']->get('async'); |
71
|
|
|
$pool->autoload($config['autoload'] ?? __DIR__.'/Runtime/RuntimeAutoload.php'); |
72
|
|
|
$pool->concurrency($config['concurrency']); |
73
|
|
|
$pool->timeout($config['timeout']); |
74
|
|
|
$pool->sleepTime($config['sleepTime']); |
75
|
|
|
$pool->defaultOutputLength($config['defaultOutputLength']); |
76
|
|
|
$pool->withBinary($config['withBinary']); |
77
|
|
|
|
78
|
|
|
return $pool; |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register package commands. |
84
|
|
|
*/ |
85
|
|
|
protected function registerCommands(): void |
86
|
|
|
{ |
87
|
|
|
if ($this->app->runningInConsole()) { |
88
|
|
|
$this->commands(['command.async.make']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function provides(): array |
96
|
|
|
{ |
97
|
|
|
return ['async', 'async.pool', 'command.async.make']; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|