diff --git a/composer.json b/composer.json index 318f9c9..1f6f887 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "cycle/schema-renderer": "^1.2", "doctrine/inflector": "^1.4 || ^2.0", "spiral/attributes": "^2.10 || ^3.0", - "spiral/framework": "^3.0", + "spiral/framework": "^3.3", "spiral/reactor": "^3.0", "spiral/scaffolder": "^3.0", "spiral/prototype": "^3.0", diff --git a/src/Bootloader/AuthTokensBootloader.php b/src/Bootloader/AuthTokensBootloader.php index d02f6a6..b9bf6c8 100644 --- a/src/Bootloader/AuthTokensBootloader.php +++ b/src/Bootloader/AuthTokensBootloader.php @@ -4,28 +4,47 @@ namespace Spiral\Cycle\Bootloader; +use Spiral\Auth\Config\AuthConfig; +use Spiral\Boot\EnvironmentInterface; +use Spiral\Config\ConfiguratorInterface; +use Spiral\Config\Patch\Set; use Spiral\Cycle\Auth\Token; use Spiral\Cycle\Auth\TokenStorage as CycleStorage; -use Spiral\Auth\TokenStorageInterface; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Bootloader\Auth\HttpAuthBootloader; use Spiral\Tokenizer\Bootloader\TokenizerBootloader; final class AuthTokensBootloader extends Bootloader { + private const TOKEN_STORAGE_NAME = 'cycle'; + protected const DEPENDENCIES = [ - HttpAuthBootloader::class, CycleOrmBootloader::class, AnnotatedBootloader::class, ]; - protected const SINGLETONS = [ - TokenStorageInterface::class => CycleStorage::class, - ]; + public function __construct( + private readonly ConfiguratorInterface $config, + ) { + } + + public function init( + TokenizerBootloader $tokenizer, + HttpAuthBootloader $bootloader, + EnvironmentInterface $env + ): void { + $bootloader->addTokenStorage(self::TOKEN_STORAGE_NAME, CycleStorage::class); + + // This is a temporary fix and backward compatibility for case when AUTH_TOKEN_STORAGE is not set. + // TODO: Will be removed after release of spiral/framework 4.0.0 + if ($env->get('AUTH_TOKEN_STORAGE') === null) { + $this->config->modify( + AuthConfig::CONFIG, + new Set('defaultStorage', self::TOKEN_STORAGE_NAME), + ); + } - public function init(TokenizerBootloader $tokenizer): void - { $tokenClass = new \ReflectionClass(Token::class); - $tokenizer->addDirectory(dirname($tokenClass->getFileName())); + $tokenizer->addDirectory(\dirname($tokenClass->getFileName())); } } diff --git a/tests/src/Auth/TokenStorageTest.php b/tests/src/Auth/TokenStorageTest.php index 9ad656e..e1e12c3 100644 --- a/tests/src/Auth/TokenStorageTest.php +++ b/tests/src/Auth/TokenStorageTest.php @@ -6,6 +6,7 @@ use Spiral\Auth\TokenInterface; use Spiral\Auth\TokenStorageInterface; +use Spiral\Auth\TokenStorageProviderInterface; use Spiral\Cycle\Auth\TokenStorage; use Spiral\Tests\BaseTest; @@ -17,7 +18,7 @@ protected function setUp(): void { parent::setUp(); - $this->storage = $this->getContainer()->get(TokenStorageInterface::class); + $this->storage = $this->getContainer()->get(TokenStorageProviderInterface::class)->getStorage('cycle'); } public function testTokenShouldBeCreatedWithoutExpiration() diff --git a/tests/src/Bootloader/AuthTokensBootloaderTest.php b/tests/src/Bootloader/AuthTokensBootloaderTest.php index e1992d1..57d969f 100644 --- a/tests/src/Bootloader/AuthTokensBootloaderTest.php +++ b/tests/src/Bootloader/AuthTokensBootloaderTest.php @@ -4,16 +4,17 @@ namespace Spiral\Tests\Bootloader; -use Spiral\Auth\TokenStorageInterface; +use Spiral\Auth\Config\AuthConfig; use Spiral\Cycle\Auth\Token; -use Spiral\Cycle\Auth\TokenStorage as CycleStorage; +use Spiral\Cycle\Auth\TokenStorage; use Spiral\Tests\BaseTest; final class AuthTokensBootloaderTest extends BaseTest { public function testGetsTokenStorage(): void { - $this->assertContainerBoundAsSingleton(TokenStorageInterface::class, CycleStorage::class); + $storages = $this->getConfig(AuthConfig::CONFIG)['storages']; + $this->assertSame(TokenStorage::class, $storages['cycle']); } public function testTokenEntityShouldBeRegisterInTokenizer(): void