|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\LaravelDto\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\Dto\Manipulators\ArrayConverter; |
|
6
|
|
|
use Cerbero\LaravelDto\Manipulators\Listener; |
|
7
|
|
|
use Cerbero\LaravelDto\Console\Commands\MakeDtoCommand; |
|
8
|
|
|
use Cerbero\LaravelDto\Console\Manifest; |
|
9
|
|
|
use Cerbero\LaravelDto\Console\DtoQualifierContract; |
|
10
|
|
|
use Cerbero\LaravelDto\Dto; |
|
11
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
12
|
|
|
use Illuminate\Support\ServiceProvider; |
|
13
|
|
|
|
|
14
|
|
|
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The package service provider. |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class LaravelDtoServiceProvider extends ServiceProvider |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The package configuration path. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected const CONFIG = __DIR__ . '/../../config/dto.php'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Bootstrap the application services. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
39 |
|
public function boot() |
|
35
|
|
|
{ |
|
36
|
39 |
|
if ($this->app->runningInConsole()) { |
|
37
|
39 |
|
$this->commands(MakeDtoCommand::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
39 |
|
$this->publishes([static::CONFIG => $this->app->configPath('dto.php')], 'cerbero-dto'); |
|
41
|
|
|
|
|
42
|
39 |
|
ArrayConverter::instance()->setConversions($this->config('conversions')); |
|
43
|
|
|
|
|
44
|
39 |
|
Listener::instance()->listen($this->config('listeners')); |
|
45
|
39 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Retrieve the given configuration value |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $key |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
39 |
|
protected function config(string $key) |
|
54
|
|
|
{ |
|
55
|
39 |
|
return $this->app['config']["dto.{$key}"]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Register any application services. |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
39 |
|
public function register() |
|
64
|
|
|
{ |
|
65
|
39 |
|
$this->mergeConfigFrom(static::CONFIG, 'dto'); |
|
66
|
|
|
|
|
67
|
39 |
|
$this->app->bind(DtoQualifierContract::class, $this->config('qualifier')); |
|
68
|
|
|
|
|
69
|
|
|
$this->app->singleton(Manifest::class, function (Application $app) { |
|
70
|
24 |
|
return new Manifest($app->storagePath() . '/cerbero_laravel_dto.php'); |
|
71
|
39 |
|
}); |
|
72
|
|
|
|
|
73
|
|
|
$this->app->resolving(Dto::class, function (Dto $dto, Application $app) { |
|
74
|
|
|
return $dto->mutate(function (Dto $dto) use ($app) { |
|
75
|
3 |
|
$dto->merge($app->make('request')->all(), IGNORE_UNKNOWN_PROPERTIES); |
|
76
|
3 |
|
}); |
|
77
|
39 |
|
}); |
|
78
|
39 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|