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