Skip to content

Commit ebfb423

Browse files
Added the ability to set custom macro names
1 parent 267fc50 commit ebfb423

17 files changed

+140
-22
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,55 @@ To get the latest version of `HTTP Macros`, simply require the project using [Co
1515
composer require dragon-code/laravel-http-macros
1616
```
1717

18+
## Configuration
19+
20+
If desired, you can publish the configuration file using the console command:
21+
22+
```bash
23+
php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider"
24+
```
25+
26+
If your application already has a `config/http.php` file, then you can simply add a new `macros` key from the
27+
[configuration](config/http.php) file to it.
28+
29+
Here you can specify a list of your classes for registering macros.
30+
Macro classes must inherit from the abstract class `DragonCode\LaravelHttpMacros\Macros\Macro`.
31+
32+
You can also redefine macro names using an associative array. For example:
33+
34+
```php
35+
// Config
36+
return [
37+
'macros' => [
38+
'response' => [
39+
ToDataMacro::class,
40+
],
41+
],
42+
];
43+
44+
// Macro
45+
Http::get()->toData(...);
46+
```
47+
48+
```php
49+
// Config
50+
return [
51+
'macros' => [
52+
'response' => [
53+
'qwerty' => ToDataMacro::class,
54+
],
55+
],
56+
];
57+
58+
// Macro
59+
Http::get()->qwerty(...);
60+
Http::get()->toData(...); // will be method not found exception
61+
```
62+
63+
> Note
64+
>
65+
> Please note that IDE hints will not work in this case.
66+
1867
## Usage
1968

2069
### As Class

config/http.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
'response' => [
1111
ToDataMacro::class,
1212
ToDataCollectionMacro::class,
13+
14+
// CustomMacro::class,
15+
// 'toFoo' => CustomMacro::class,
1316
],
1417
],
1518
];

src/ServiceProvider.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace DragonCode\LaravelHttpMacros;
66

7-
use DragonCode\LaravelHttpMacros\Macros\Macro;
87
use Illuminate\Http\Client\Response;
98
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
109

@@ -23,20 +22,17 @@ public function boot(): void
2322

2423
protected function bootMacros(): void
2524
{
26-
foreach ($this->macros() as $macros) {
27-
Response::macro($macros::name(), $macros::callback());
25+
foreach ($this->macros() as $name => $macro) {
26+
Response::macro(
27+
name : is_string($name) ? $name : $macro::name(),
28+
macro: $macro::callback()
29+
);
2830
}
2931
}
3032

31-
/**
32-
* @throws \Psr\Container\ContainerExceptionInterface
33-
* @throws \Psr\Container\NotFoundExceptionInterface
34-
*
35-
* @return array<string|Macro>
36-
*/
3733
protected function macros(): array
3834
{
39-
return $this->app['config']->get('http.macros.response', []);
35+
return config('http.macros.response', []);
4036
}
4137

4238
protected function registerConfig(): void

tests/Fixtures/ConstructorData.php renamed to tests/Fixtures/Data/ConstructorData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Fixtures;
5+
namespace Tests\Fixtures\Data;
66

77
class ConstructorData
88
{

tests/Fixtures/FromMethodData.php renamed to tests/Fixtures/Data/FromMethodData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Fixtures;
5+
namespace Tests\Fixtures\Data;
66

77
class FromMethodData
88
{

tests/Fixtures/SpatieConstructorData.php renamed to tests/Fixtures/Data/SpatieConstructorData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Fixtures;
5+
namespace Tests\Fixtures\Data;
66

77
use Spatie\LaravelData\Data;
88

tests/Fixtures/SpatiePropertiesData.php renamed to tests/Fixtures/Data/SpatiePropertiesData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Fixtures;
5+
namespace Tests\Fixtures\Data;
66

77
use Spatie\LaravelData\Data;
88

tests/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Tests;
44

5+
use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
6+
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
57
use DragonCode\LaravelHttpMacros\ServiceProvider;
8+
use Illuminate\Config\Repository;
69
use Orchestra\Testbench\TestCase as BaseTestCase;
710
use Spatie\LaravelData\LaravelDataServiceProvider;
811

@@ -15,4 +18,16 @@ protected function getPackageProviders($app): array
1518
ServiceProvider::class,
1619
];
1720
}
21+
22+
protected function defineEnvironment($app): void
23+
{
24+
tap($app['config'], function (Repository $config) {
25+
$config->set('http.macros.response', [
26+
ToDataMacro::class,
27+
ToDataCollectionMacro::class,
28+
29+
'toFoo' => ToDataMacro::class,
30+
]);
31+
});
32+
}
1833
}

tests/Unit/ToDataCollectionConstructorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Illuminate\Support\Collection;
6-
use Tests\Fixtures\ConstructorData;
6+
use Tests\Fixtures\Data\ConstructorData;
77

88
test('many', function () {
99
$response = fakeRequest()->get('many');

tests/Unit/ToDataCollectionFromMethodTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Illuminate\Support\Collection;
6-
use Tests\Fixtures\FromMethodData;
6+
use Tests\Fixtures\Data\FromMethodData;
77

88
test('many', function () {
99
$response = fakeRequest()->get('many');

0 commit comments

Comments
 (0)