Skip to content

Commit 267fc50

Browse files
The list of connected macros is included in the configuration file
1 parent 0841c5b commit 267fc50

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
## Installation
1111

12+
To get the latest version of `HTTP Macros`, simply require the project using [Composer](https://getcomposer.org):
13+
1214
```Bash
1315
composer require dragon-code/laravel-http-macros
1416
```

config/http.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
6+
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
7+
8+
return [
9+
'macros' => [
10+
'response' => [
11+
ToDataMacro::class,
12+
ToDataCollectionMacro::class,
13+
],
14+
],
15+
];

src/ServiceProvider.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,52 @@
55
namespace DragonCode\LaravelHttpMacros;
66

77
use DragonCode\LaravelHttpMacros\Macros\Macro;
8-
use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
9-
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
108
use Illuminate\Http\Client\Response;
119
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1210

1311
class ServiceProvider extends BaseServiceProvider
1412
{
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+
}
2017

2118
public function boot(): void
2219
{
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());
2528
}
2629
}
2730

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
2951
{
30-
Response::macro($macros::name(), $macros::callback());
52+
$this->publishes([
53+
__DIR__ . '/../config/http.php' => config_path('http.php'),
54+
]);
3155
}
3256
}

0 commit comments

Comments
 (0)