-
-
Notifications
You must be signed in to change notification settings - Fork 879
feat: customizable session driver #3610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b77421e
feat: add session driver extension API
SychO9 12eb9b9
chore: no need to inject into SessionManager
SychO9 ccb2799
chore: improve current session driver detection
SychO9 8030d11
chore: silently fallback to default if configured driver is unavailable
SychO9 8bd47b6
chore: log a critical error if the configured session driver is unava…
SychO9 7b7e65e
fix(review): simplify session manager logic
SychO9 9bb2fb9
chore(review): clearer error message
SychO9 6e53566
chore(review): improve `InfoCommand` code readability
SychO9 f284946
Apply fixes from StyleCI
StyleCIBot a9c62e4
chore(review): improve `InfoCommand` code readability
SychO9 5488d66
Merge branch 'main' into sm/session-driver
SychO9 0942086
fix: phpstan detected errors
SychO9 7f2a0c0
Apply fixes from StyleCI
StyleCIBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Flarum. | ||
| * | ||
| * For detailed copyright and license information, please view the | ||
| * LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Flarum\Extend; | ||
|
|
||
| use Flarum\Extension\Extension; | ||
| use Illuminate\Contracts\Container\Container; | ||
|
|
||
| class Session implements ExtenderInterface | ||
| { | ||
| private $drivers = []; | ||
|
|
||
| /** | ||
| * Register a new session driver. | ||
| * | ||
| * A driver can currently be selected by setting `session.driver` in `config.php`. | ||
| * | ||
| * @param string $name: The name of the driver. | ||
| * @param string $driverClass: The ::class attribute of the driver. | ||
| * Driver must implement `\Flarum\User\SessionDriverInterface`. | ||
| * @return self | ||
| */ | ||
| public function driver(string $name, string $driverClass): self | ||
| { | ||
| $this->drivers[$name] = $driverClass; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function extend(Container $container, Extension $extension = null) | ||
| { | ||
| $container->extend('flarum.session.drivers', function ($drivers) { | ||
| return array_merge($drivers, $this->drivers); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Flarum. | ||
| * | ||
| * For detailed copyright and license information, please view the | ||
| * LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Flarum\User; | ||
|
|
||
| use Flarum\Foundation\Config; | ||
| use Flarum\Settings\SettingsRepositoryInterface; | ||
| use SessionHandlerInterface; | ||
|
|
||
| interface SessionDriverInterface | ||
| { | ||
| /** | ||
| * Build a session handler to handle sessions. | ||
| * Settings and configuration can either be pulled from the Flarum settings repository | ||
| * or the config.php file. | ||
| * | ||
| * @param SettingsRepositoryInterface $settings: An instance of the Flarum settings repository. | ||
| * @param Config $config: An instance of the wrapper class around `config.php`. | ||
| */ | ||
| public function build(SettingsRepositoryInterface $settings, Config $config): SessionHandlerInterface; | ||
| } |
This file contains hidden or 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,37 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Flarum. | ||
| * | ||
| * For detailed copyright and license information, please view the | ||
| * LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Flarum\User; | ||
|
|
||
| use Flarum\Foundation\Config; | ||
| use Illuminate\Session\SessionManager as IlluminateSessionManager; | ||
| use Illuminate\Session\Store; | ||
| use InvalidArgumentException; | ||
| use SessionHandlerInterface; | ||
|
|
||
| class SessionManager extends IlluminateSessionManager | ||
| { | ||
| public function handler(string $driver = null): SessionHandlerInterface | ||
|
SychO9 marked this conversation as resolved.
Outdated
|
||
| { | ||
| $config = $this->container->make(Config::class); | ||
|
|
||
| /** @var Store $driver */ | ||
| try { | ||
| $driverInstance = parent::driver($driver ?? $config['session.driver']); | ||
|
SychO9 marked this conversation as resolved.
Outdated
|
||
| } catch (InvalidArgumentException $e) { | ||
| if (! $driver) { | ||
| $driverInstance = parent::driver($this->getDefaultDriver()); | ||
| } else { | ||
| throw $e; | ||
| } | ||
| } | ||
|
|
||
| return $driverInstance->getHandler(); | ||
| } | ||
| } | ||
This file contains hidden or 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
116 changes: 116 additions & 0 deletions
116
framework/core/tests/integration/extenders/SessionTest.php
This file contains hidden or 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,116 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Flarum. | ||
| * | ||
| * For detailed copyright and license information, please view the | ||
| * LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Flarum\Tests\integration\extenders; | ||
|
|
||
| use Flarum\Extend; | ||
| use Flarum\Foundation\Config; | ||
| use Flarum\Settings\SettingsRepositoryInterface; | ||
| use Flarum\Testing\integration\RetrievesAuthorizedUsers; | ||
| use Flarum\Testing\integration\TestCase; | ||
| use Flarum\User\SessionDriverInterface; | ||
| use Illuminate\Session\FileSessionHandler; | ||
| use Illuminate\Session\NullSessionHandler; | ||
| use InvalidArgumentException; | ||
| use SessionHandlerInterface; | ||
|
|
||
| class SessionTest extends TestCase | ||
| { | ||
| use RetrievesAuthorizedUsers; | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function default_driver_exists_by_default() | ||
| { | ||
| $this->expectNotToPerformAssertions(); | ||
| $this->app()->getContainer()->make('session.handler'); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function custom_driver_doesnt_exist_by_default() | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->app()->getContainer()->make('session')->handler('flarum-acme'); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function custom_driver_exists_if_added() | ||
| { | ||
| $this->extend((new Extend\Session())->driver('flarum-acme', AcmeSessionDriver::class)); | ||
|
|
||
| $handler = $this->app()->getContainer()->make('session')->handler('flarum-acme'); | ||
|
|
||
| $this->assertEquals(NullSessionHandler::class, get_class($handler)); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function custom_driver_overrides_laravel_defined_drivers_if_added() | ||
| { | ||
| $this->extend((new Extend\Session())->driver('redis', AcmeSessionDriver::class)); | ||
|
|
||
| $handler = $this->app()->getContainer()->make('session')->handler('redis'); | ||
|
|
||
| $this->assertEquals(NullSessionHandler::class, get_class($handler)); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function uses_default_driver_if_driver_from_config_file_not_configured() | ||
| { | ||
| $this->config('session.driver', null); | ||
|
|
||
| $handler = $this->app()->getContainer()->make('session.handler'); | ||
|
|
||
| $this->assertEquals(FileSessionHandler::class, get_class($handler)); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function uses_default_driver_if_configured_driver_from_config_file_unavailable() | ||
| { | ||
| $this->config('session.driver', 'nevergonnagiveyouup'); | ||
|
|
||
| $handler = $this->app()->getContainer()->make('session.handler'); | ||
|
|
||
| $this->assertEquals(FileSessionHandler::class, get_class($handler)); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function uses_custom_driver_from_config_file_if_configured_and_available() | ||
| { | ||
| $this->extend( | ||
| (new Extend\Session)->driver('flarum-acme', AcmeSessionDriver::class) | ||
| ); | ||
|
|
||
| $this->config('session.driver', 'flarum-acme'); | ||
|
|
||
| $handler = $this->app()->getContainer()->make('session.handler'); | ||
|
|
||
| $this->assertEquals(NullSessionHandler::class, get_class($handler)); | ||
| } | ||
| } | ||
|
|
||
| class AcmeSessionDriver implements SessionDriverInterface | ||
| { | ||
| public function build(SettingsRepositoryInterface $settings, Config $config): SessionHandlerInterface | ||
| { | ||
| return new NullSessionHandler(); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.