|
5 | 5 | namespace DragonCode\LaravelHttpMacros;
|
6 | 6 |
|
7 | 7 | use DragonCode\LaravelHttpMacros\Macros\Macro;
|
8 |
| -use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro; |
9 |
| -use DragonCode\LaravelHttpMacros\Macros\ToDataMacro; |
10 | 8 | use Illuminate\Http\Client\Response;
|
11 | 9 | use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
12 | 10 |
|
13 | 11 | class ServiceProvider extends BaseServiceProvider
|
14 | 12 | {
|
15 |
| - /** @var array<string|Macro> */ |
16 |
| - protected array $macros = [ |
17 |
| - ToDataMacro::class, |
18 |
| - ToDataCollectionMacro::class, |
19 |
| - ]; |
| 13 | + public function register(): void |
| 14 | + { |
| 15 | + $this->registerConfig(); |
| 16 | + } |
20 | 17 |
|
21 | 18 | public function boot(): void
|
22 | 19 | {
|
23 |
| - foreach ($this->macros as $macros) { |
24 |
| - $this->bootMacros($macros); |
| 20 | + $this->publishConfig(); |
| 21 | + $this->bootMacros(); |
| 22 | + } |
| 23 | + |
| 24 | + protected function bootMacros(): void |
| 25 | + { |
| 26 | + foreach ($this->macros() as $macros) { |
| 27 | + Response::macro($macros::name(), $macros::callback()); |
25 | 28 | }
|
26 | 29 | }
|
27 | 30 |
|
28 |
| - protected function bootMacros(Macro|string $macros): void |
| 31 | + /** |
| 32 | + * @throws \Psr\Container\ContainerExceptionInterface |
| 33 | + * @throws \Psr\Container\NotFoundExceptionInterface |
| 34 | + * |
| 35 | + * @return array<string|Macro> |
| 36 | + */ |
| 37 | + protected function macros(): array |
| 38 | + { |
| 39 | + return $this->app['config']->get('http.macros.response', []); |
| 40 | + } |
| 41 | + |
| 42 | + protected function registerConfig(): void |
| 43 | + { |
| 44 | + $this->mergeConfigFrom( |
| 45 | + path: __DIR__ . '/../config/http.php', |
| 46 | + key : 'http' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + protected function publishConfig(): void |
29 | 51 | {
|
30 |
| - Response::macro($macros::name(), $macros::callback()); |
| 52 | + $this->publishes([ |
| 53 | + __DIR__ . '/../config/http.php' => config_path('http.php'), |
| 54 | + ]); |
31 | 55 | }
|
32 | 56 | }
|
0 commit comments