Skip to content

Commit

Permalink
Merge pull request #3 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
The list of connected macros is included in the configuration file
  • Loading branch information
andrey-helldar authored Aug 23, 2024
2 parents 015d68b + 267fc50 commit f257a8b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Installation

To get the latest version of `HTTP Macros`, simply require the project using [Composer](https://getcomposer.org):

```Bash
composer require dragon-code/laravel-http-macros
```
Expand Down
15 changes: 15 additions & 0 deletions config/http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;

return [
'macros' => [
'response' => [
ToDataMacro::class,
ToDataCollectionMacro::class,
],
],
];
46 changes: 35 additions & 11 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,52 @@
namespace DragonCode\LaravelHttpMacros;

use DragonCode\LaravelHttpMacros\Macros\Macro;
use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
use Illuminate\Http\Client\Response;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
/** @var array<string|Macro> */
protected array $macros = [
ToDataMacro::class,
ToDataCollectionMacro::class,
];
public function register(): void
{
$this->registerConfig();
}

public function boot(): void
{
foreach ($this->macros as $macros) {
$this->bootMacros($macros);
$this->publishConfig();
$this->bootMacros();
}

protected function bootMacros(): void
{
foreach ($this->macros() as $macros) {
Response::macro($macros::name(), $macros::callback());
}
}

protected function bootMacros(Macro|string $macros): void
/**
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*
* @return array<string|Macro>
*/
protected function macros(): array
{
return $this->app['config']->get('http.macros.response', []);
}

protected function registerConfig(): void
{
$this->mergeConfigFrom(
path: __DIR__ . '/../config/http.php',
key : 'http'
);
}

protected function publishConfig(): void
{
Response::macro($macros::name(), $macros::callback());
$this->publishes([
__DIR__ . '/../config/http.php' => config_path('http.php'),
]);
}
}

0 comments on commit f257a8b

Please sign in to comment.