Skip to content

Commit

Permalink
Implement DeferrableProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-de-wit committed Jan 24, 2025
1 parent 0a09298 commit 726b307
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
38 changes: 29 additions & 9 deletions src/Providers/LaravelIdeHelperHookPaperclipServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
<?php

declare(strict_types=1);

namespace DanielDeWit\LaravelIdeHelperHookPaperclip\Providers;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use DanielDeWit\LaravelIdeHelperHookPaperclip\Hooks\PaperclipHook;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Config\Repository;
use Illuminate\Console\Command;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class LaravelIdeHelperHookPaperclipServiceProvider extends ServiceProvider
class LaravelIdeHelperHookPaperclipServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* @return list<class-string<Command>>
*/
public function provides(): array
{
return [
ModelsCommand::class,
];
}

public function register(): void
{
if ($this->app->isProduction()) {
return;
}
$this->registerIdeHelperHook();
}

/** @var Config $config */
protected function registerIdeHelperHook(): void
{
/** @var Repository $config */
$config = $this->app->get('config');

$config->set('ide-helper.model_hooks', array_merge([
PaperclipHook::class,
], (array) $config->get('ide-helper.model_hooks', [])));
$config->set(
'ide-helper.model_hooks',
array_merge(
[PaperclipHook::class],
(array) $config->get('ide-helper.model_hooks', []),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace DanielDeWit\LaravelIdeHelperHookPaperclip\Tests\Integration;

use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use DanielDeWit\LaravelIdeHelperHookPaperclip\Hooks\PaperclipHook;
use DanielDeWit\LaravelIdeHelperHookPaperclip\Providers\LaravelIdeHelperHookPaperclipServiceProvider;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Application;
use Orchestra\Testbench\TestCase;

class LaravelIdeHelperHookPaperclipServiceProviderTest extends TestCase
Expand All @@ -16,15 +19,28 @@ class LaravelIdeHelperHookPaperclipServiceProviderTest extends TestCase
protected function getPackageProviders($app): array
{
return [
IdeHelperServiceProvider::class,
LaravelIdeHelperHookPaperclipServiceProvider::class,
];
}

/**
* @test
*/
public function it_adds_the_paperclip_hook_to_the_config(): void
public function it_auto_registration_off_model_hook(): void
{
static::assertContains(PaperclipHook::class, (array) config('ide-helper.model_hooks'));
/** @var Application $app */
$app = $this->app;

$app->loadDeferredProvider(IdeHelperServiceProvider::class);
$app->loadDeferredProvider(LaravelIdeHelperHookPaperclipServiceProvider::class);

/** @var Repository $config */
$config = $app->get('config');

$this->assertContains(
PaperclipHook::class,
(array) $config->get('ide-helper.model_hooks', []),
);
}
}

0 comments on commit 726b307

Please sign in to comment.