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\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
|
42 |
|
public function boot() |
35
|
|
|
{ |
36
|
42 |
|
if ($this->app->runningInConsole()) { |
37
|
42 |
|
$this->commands(MakeDtoCommand::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
42 |
|
$this->publishes([static::CONFIG => $this->app->configPath('dto.php')], 'dto'); |
41
|
|
|
|
42
|
42 |
|
ArrayConverter::instance()->setConversions($this->config('conversions')); |
43
|
|
|
|
44
|
42 |
|
Listener::instance()->listen($this->config('listeners')); |
45
|
|
|
|
46
|
42 |
|
if (class_exists($cloner = '\Symfony\Component\VarDumper\Cloner\VarCloner')) { |
47
|
42 |
|
$cloner::$defaultCasters[Dto::class] = function (Dto $dto) { |
|
|
|
|
48
|
|
|
return $dto->toArray(); |
49
|
|
|
}; |
50
|
|
|
} |
51
|
42 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve the given configuration value |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
42 |
|
protected function config(string $key) |
60
|
|
|
{ |
61
|
42 |
|
return $this->app['config']["dto.{$key}"]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Register any application services. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
42 |
|
public function register() |
70
|
|
|
{ |
71
|
42 |
|
$this->mergeConfigFrom(static::CONFIG, 'dto'); |
72
|
|
|
|
73
|
42 |
|
$this->app->bind(DtoQualifierContract::class, $this->config('qualifier')); |
74
|
|
|
|
75
|
42 |
|
$this->app->singleton(Manifest::class, function (Application $app) { |
76
|
24 |
|
return new Manifest($app->storagePath() . '/cerbero_laravel_dto.php'); |
77
|
42 |
|
}); |
78
|
|
|
|
79
|
42 |
|
$this->app->resolving(Dto::class, function (Dto $dto, Application $app) { |
80
|
3 |
|
return $dto->mutate(function (Dto $dto) use ($app) { |
81
|
3 |
|
$dto->merge($app->make('request')->all(), IGNORE_UNKNOWN_PROPERTIES); |
82
|
3 |
|
}); |
83
|
42 |
|
}); |
84
|
42 |
|
} |
85
|
|
|
} |
86
|
|
|
|