1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Modulate\Artisan\Interceptor\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Events\ArtisanStarting; |
8
|
|
|
use Illuminate\Console\Events\CommandStarting; |
9
|
|
|
use Illuminate\Console\Events\CommandFinished; |
10
|
|
|
use Illuminate\Support\Facades\Event; |
11
|
|
|
use Modulate\Artisan\Interceptor\Contracts\HandlerStack as HandlerStackContract; |
12
|
|
|
use Modulate\Artisan\Interceptor\InterceptedCommand; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Modulate\Artisan\Interceptor\Contracts\Interceptor as InterceptorContract; |
16
|
|
|
use Modulate\Artisan\Interceptor\Facades\ArtisanInterceptor; |
17
|
|
|
use Modulate\Artisan\Interceptor\HandlerStack; |
18
|
|
|
use Modulate\Artisan\Interceptor\Interceptor; |
19
|
|
|
use Modulate\Artisan\Interceptor\OptionBuilder; |
20
|
|
|
|
21
|
|
|
class InterceptorServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
protected $artisan; |
25
|
|
|
|
26
|
8 |
|
public function register() |
27
|
|
|
{ |
28
|
|
|
if ( |
29
|
8 |
|
$this->app->runningInConsole() |
30
|
8 |
|
|| $this->app->runningUnitTests() |
31
|
|
|
) { |
32
|
8 |
|
$this->app->bind(HandlerStackContract::class, HandlerStack::class); |
33
|
8 |
|
$this->app->singleton(InterceptorContract::class, Interceptor::class); |
34
|
|
|
|
35
|
8 |
|
$this->app->bind(OptionBuilder::class, function() { |
36
|
2 |
|
return new OptionBuilder(); |
37
|
8 |
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
public function boot() |
43
|
|
|
{ |
44
|
|
|
if ( |
45
|
8 |
|
$this->app->runningInConsole() |
46
|
8 |
|
|| $this->app->runningUnitTests() |
47
|
|
|
) { |
48
|
8 |
|
Event::listen(ArtisanStarting::class, function(ArtisanStarting $e) { |
49
|
8 |
|
$this |
50
|
8 |
|
->app |
51
|
8 |
|
->make(InterceptorContract::class) |
52
|
8 |
|
->starting($e->artisan); |
53
|
8 |
|
}); |
54
|
|
|
|
55
|
8 |
|
Event::listen(CommandStarting::class, function(CommandStarting $e) { |
56
|
6 |
|
$this |
57
|
6 |
|
->app |
58
|
6 |
|
->make(InterceptorContract::class) |
59
|
6 |
|
->handleBefore(new InterceptedCommand($e->command, $e->input, $e->output)); |
|
|
|
|
60
|
8 |
|
}); |
61
|
8 |
|
Event::listen(CommandFinished::class, function(CommandFinished $e) { |
62
|
6 |
|
$this |
63
|
6 |
|
->app |
64
|
6 |
|
->make(InterceptorContract::class) |
65
|
6 |
|
->handleAfter(new InterceptedCommand($e->command, $e->input, $e->output, $e->exitCode)); |
|
|
|
|
66
|
8 |
|
}); |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|