-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Config): Add ability to provider app service provider config
- By using `HasConfig` interface on service provider you can merge own provider config and user defined config. - Also name of the config is same as generated service file name. - See ValidConfig and LoadProviderConfigTest
- Loading branch information
Showing
21 changed files
with
334 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Config; | ||
|
||
use Illuminate\Config\Repository; | ||
use LaraStrict\Providers\AbstractServiceProvider; | ||
use LaraStrict\Providers\Actions\GetAppServiceProviderForClassAction; | ||
|
||
abstract class AbstractProviderConfig extends AbstractConfig | ||
{ | ||
public function __construct( | ||
Repository $config, | ||
private readonly GetAppServiceProviderForClassAction $getAppServiceProviderForClassAction | ||
) { | ||
parent::__construct($config); | ||
} | ||
|
||
/** | ||
* @return class-string<AbstractServiceProvider> | ||
*/ | ||
abstract protected function getServiceProvider(): string; | ||
|
||
protected function getConfigFileName(): string | ||
{ | ||
$appService = $this->getAppServiceProviderForClassAction->execute($this->getServiceProvider()); | ||
|
||
return $appService->serviceFileName; | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/Providers/Actions/GetAppServiceProviderForClassAction.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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Providers\Actions; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\ServiceProvider; | ||
use LaraStrict\Cache\Contracts\CacheMeServiceContract; | ||
use LaraStrict\Cache\Enums\CacheMeStrategy; | ||
use LaraStrict\Providers\AbstractServiceProvider; | ||
use LaraStrict\Providers\Entities\AppServiceProviderEntity; | ||
use LogicException; | ||
|
||
class GetAppServiceProviderForClassAction | ||
{ | ||
public function __construct( | ||
private readonly CacheMeServiceContract $cacheMeService, | ||
) { | ||
} | ||
|
||
/** | ||
* @param class-string<AbstractServiceProvider> $providerClass | ||
*/ | ||
public function execute(string $providerClass): AppServiceProviderEntity | ||
{ | ||
return $this->cacheMeService->get( | ||
key: 'app-service-provider-' . $providerClass, | ||
getValue: static function (Application $application) use ($providerClass) { | ||
// TODO add ability to cache getProvider (laravel patch) | ||
$serviceProvider = $application->getProvider($providerClass); | ||
if ($serviceProvider instanceof ServiceProvider === false) { | ||
throw new LogicException(sprintf('Provider for <%s> not loaded ', $providerClass)); | ||
} | ||
|
||
if ($serviceProvider instanceof AbstractServiceProvider === false) { | ||
throw new LogicException(sprintf( | ||
'Provider <%s> must use <%s>', | ||
$providerClass, | ||
AbstractServiceProvider::class, | ||
)); | ||
} | ||
|
||
return $serviceProvider->getAppServiceProvider(); | ||
}, | ||
strategy: CacheMeStrategy::Memory | ||
); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Providers\Contracts; | ||
|
||
interface HasConfig | ||
{ | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Providers\Pipes; | ||
|
||
use Closure; | ||
use LaraStrict\Contracts\AppServiceProviderPipeContract; | ||
use LaraStrict\Providers\Contracts\HasConfig; | ||
use LaraStrict\Providers\Entities\AppServiceProviderEntity; | ||
|
||
class LoadProviderConfig implements AppServiceProviderPipeContract | ||
{ | ||
public function handle(AppServiceProviderEntity $appServiceProvider, Closure $next): void | ||
{ | ||
if ($appServiceProvider->serviceProvider instanceof HasConfig) { | ||
$appServiceProvider->serviceProvider->laraLoadProviderConfigFrom( | ||
path: $appServiceProvider->serviceRootDir, | ||
namespace: $appServiceProvider->serviceFileName | ||
); | ||
} | ||
|
||
$next($appServiceProvider); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Config; | ||
|
||
use Tests\LaraStrict\Feature\Config\NotUsingPipe\Config\NotUsingPipeConfig; | ||
use Tests\LaraStrict\Feature\Config\NotUsingPipe\NotUsingPipeServiceProvider; | ||
use Tests\LaraStrict\Feature\Config\Valid\Config\ValidConfig; | ||
use Tests\LaraStrict\Feature\Config\Valid\ValidConfigServiceProvider; | ||
use Tests\LaraStrict\Feature\TestCase; | ||
|
||
class AbstractProviderConfigTest extends TestCase | ||
{ | ||
public function testValid(): void | ||
{ | ||
$this->app() | ||
->register(provider: ValidConfigServiceProvider::class); | ||
$config = $this->app() | ||
->make(ValidConfig::class); | ||
|
||
$this->assertEquals('value!', $config->getTest()); | ||
} | ||
|
||
public function testNotUsingPipe(): void | ||
{ | ||
$this->app() | ||
->register(provider: NotUsingPipeServiceProvider::class); | ||
$config = $this->app() | ||
->make(NotUsingPipeConfig::class); | ||
|
||
$this->assertEquals('missing file, this is a default', $config->getTest()); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
tests/Feature/Config/NotUsingPipe/Config/NotUsingPipeConfig.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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Config\NotUsingPipe\Config; | ||
|
||
use LaraStrict\Config\AbstractProviderConfig; | ||
use Tests\LaraStrict\Feature\Config\NotUsingPipe\NotUsingPipeServiceProvider; | ||
|
||
class NotUsingPipeConfig extends AbstractProviderConfig | ||
{ | ||
public function getTest(): string | ||
{ | ||
return $this->get(keyOrPath: ['test', 'sub-key'], default: 'missing file, this is a default'); | ||
} | ||
|
||
protected function getServiceProvider(): string | ||
{ | ||
return NotUsingPipeServiceProvider::class; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Feature/Config/NotUsingPipe/NotUsingPipeServiceProvider.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Config\NotUsingPipe; | ||
|
||
use LaraStrict\Providers\AbstractServiceProvider; | ||
|
||
class NotUsingPipeServiceProvider extends AbstractServiceProvider | ||
{ | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Config\Valid\Config; | ||
|
||
use LaraStrict\Config\AbstractProviderConfig; | ||
use Tests\LaraStrict\Feature\Config\Valid\ValidConfigServiceProvider; | ||
|
||
class ValidConfig extends AbstractProviderConfig | ||
{ | ||
final public const KeyTest = 'test'; | ||
|
||
public function getTest(): string | ||
{ | ||
return $this->get(self::KeyTest); | ||
} | ||
|
||
protected function getServiceProvider(): string | ||
{ | ||
return ValidConfigServiceProvider::class; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\LaraStrict\Feature\Config\Valid\Config\ValidConfig; | ||
|
||
return [ | ||
ValidConfig::KeyTest => 'value!', | ||
]; |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Config\Valid; | ||
|
||
use LaraStrict\Providers\AbstractServiceProvider; | ||
use LaraStrict\Providers\Contracts\HasConfig; | ||
|
||
class ValidConfigServiceProvider extends AbstractServiceProvider implements HasConfig | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Feature/Providers/Pipes/LoadProviderConfig/Invalid/InvalidConfigServiceProvider.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\LoadProviderConfig\Invalid; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use LaraStrict\Providers\Contracts\HasConfig; | ||
|
||
class InvalidConfigServiceProvider extends ServiceProvider implements HasConfig | ||
{ | ||
} |
Oops, something went wrong.