Skip to content

Commit

Permalink
feat: bind resource to the container and inject in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
trovster committed Feb 12, 2023
1 parent 1973156 commit ff5a284
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "surface/laravel-webfinger",
"description": "A Laravel package to create an ActivityPub webfinger",
"version": "1.0.0",
"version": "1.1.0",
"keywords": [
"trovster",
"laravel",
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Controllers/Wellknown/WebfingerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Surface\LaravelWebfinger\Http\Resources\Webfinger;
use Surface\LaravelWebfinger\Service\Webfinger as Service;
use Surface\LaravelWebfinger\Http\Resources\Webfinger as Resource;

final class WebfingerController extends Controller
{
public function __construct(protected Service $service)
public function __construct(protected Resource $resource)
{
}

public function __invoke(Request $request): JsonResponse
{
return Webfinger::make(...$this->service->toArray())->toResponse($request);
return $this->resource->toResponse($request);
}
}
27 changes: 23 additions & 4 deletions src/LaravelWebfingerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Surface\LaravelWebfinger;

use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Surface\LaravelWebfinger\Service\Webfinger;
use Surface\LaravelWebfinger\Http\Resources\Webfinger as Resource;
use Surface\LaravelWebfinger\Service\Webfinger as Service;

class LaravelWebfingerServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -34,8 +35,26 @@ protected function bootRoutes(): void
protected function bindService(): void
{
$this->app->bind(
Webfinger::class,
static fn () => new Webfinger(...Config::get('webfinger', []))
Service::class,
static function (Application $app): Service {
/** @var \Illuminate\Config\Repository $config */
$config = $app->get('config');

/** @var array<string, string> $items */
$items = $config->get('webfinger', []);

return new Service(...$items);
}
);

$this->app->bind(
Resource::class,
static function (Application $app): Resource {
/** @var \Surface\LaravelWebfinger\Service\Webfinger $service */
$service = $app->make(Service::class);

return new Resource(...$service);
}
);
}
}

0 comments on commit ff5a284

Please sign in to comment.