-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto register ide-helper hook using Package Discovery (#215)
* Auto register ide-helper hook * Update EloquentHasManyDeepServiceProvider.php This package should not be installed on a production environment, therefore we don't need to check it. * Update EloquentHasManyDeepServiceProvider to implement DeferrableProvider Ensures the hook is only registered after the ModelsCommand is called. * Refactoring --------- Co-authored-by: Jonas Staudenmeir <[email protected]>
- Loading branch information
1 parent
dfcc1a9
commit 9d9aa83
Showing
5 changed files
with
210 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| IDE Helper | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Automatically register the model hook to receive correct type hints | ||
| | ||
*/ | ||
'ide_helper_enabled' => env('ELOQUENT_HAS_MANY_DEEP_IDE_HELPER_ENABLED', true), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentHasManyDeep\Providers; | ||
|
||
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; | ||
use Illuminate\Contracts\Support\DeferrableProvider; | ||
use Illuminate\Support\ServiceProvider; | ||
use Staudenmeir\EloquentHasManyDeep\IdeHelper\DeepRelationsHook; | ||
|
||
class EloquentHasManyDeepServiceProvider extends ServiceProvider implements DeferrableProvider | ||
{ | ||
public function boot(): void | ||
{ | ||
$this->publishConfig(); | ||
} | ||
|
||
public function register(): void | ||
{ | ||
$this->registerConfig(); | ||
|
||
$this->registerIdeHelperHook(); | ||
} | ||
|
||
public function provides(): array | ||
{ | ||
return [ | ||
ModelsCommand::class, | ||
]; | ||
} | ||
|
||
protected function registerIdeHelperHook(): void | ||
{ | ||
/** @var \Illuminate\Contracts\Config\Repository $config */ | ||
$config = $this->app->get('config'); | ||
|
||
if (!$config->get('eloquent-has-many-deep.ide_helper_enabled')) { | ||
return; | ||
} | ||
|
||
$config->set('ide-helper.model_hooks', array_merge([ | ||
DeepRelationsHook::class, | ||
], $config->get('ide-helper.model_hooks', []))); | ||
} | ||
|
||
protected function publishConfig(): void | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../../config/eloquent-has-many-deep.php' => config_path('eloquent-has-many-deep.php'), | ||
], 'eloquent-has-many-deep'); | ||
} | ||
|
||
protected function registerConfig(): void | ||
{ | ||
$this->mergeConfigFrom( | ||
__DIR__ . '/../../config/eloquent-has-many-deep.php', | ||
'eloquent-has-many-deep', | ||
); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/Providers/EloquentHasManyDeepServiceProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers; | ||
|
||
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider; | ||
use Orchestra\Testbench\TestCase; | ||
use Staudenmeir\EloquentHasManyDeep\IdeHelper\DeepRelationsHook; | ||
use Staudenmeir\EloquentHasManyDeep\Providers\EloquentHasManyDeepServiceProvider; | ||
|
||
class EloquentHasManyDeepServiceProviderTest extends TestCase | ||
{ | ||
protected function getPackageProviders($app): array | ||
{ | ||
return [ | ||
IdeHelperServiceProvider::class, | ||
EloquentHasManyDeepServiceProvider::class, | ||
]; | ||
} | ||
|
||
public function testAutoRegistrationOfModelHook(): void | ||
{ | ||
$this->app->loadDeferredProvider(IdeHelperServiceProvider::class); | ||
$this->app->loadDeferredProvider(EloquentHasManyDeepServiceProvider::class); | ||
|
||
/** @var \Illuminate\Contracts\Config\Repository $config */ | ||
$config = $this->app->get('config'); | ||
|
||
$this->assertContains( | ||
DeepRelationsHook::class, | ||
$config->get('ide-helper.model_hooks'), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* @define-env usesIdeHelperDisabledInConfig | ||
*/ | ||
public function testDisabledRegistrationOfModelHookFromConfig(): void | ||
{ | ||
$this->app->loadDeferredProvider(IdeHelperServiceProvider::class); | ||
$this->app->loadDeferredProvider(EloquentHasManyDeepServiceProvider::class); | ||
|
||
/** @var \Illuminate\Contracts\Config\Repository $config */ | ||
$config = $this->app->get('config'); | ||
|
||
$this->assertNotContains( | ||
DeepRelationsHook::class, | ||
$config->get('ide-helper.model_hooks'), | ||
); | ||
} | ||
|
||
protected function usesIdeHelperDisabledInConfig($app): void | ||
{ | ||
$app['config']->set('eloquent-has-many-deep.ide_helper_enabled', false); | ||
} | ||
} |