|
14 | 14 | use Flarum\Foundation\Application; |
15 | 15 | use Flarum\Foundation\Config; |
16 | 16 | use Flarum\Settings\SettingsRepositoryInterface; |
| 17 | +use Flarum\User\SessionManager; |
17 | 18 | use Illuminate\Contracts\Queue\Queue; |
18 | 19 | use Illuminate\Database\ConnectionInterface; |
| 20 | +use Illuminate\Support\Arr; |
19 | 21 | use Illuminate\Support\Str; |
| 22 | +use InvalidArgumentException; |
20 | 23 | use PDO; |
| 24 | +use SessionHandlerInterface; |
21 | 25 | use Symfony\Component\Console\Helper\Table; |
22 | 26 | use Symfony\Component\Console\Helper\TableStyle; |
23 | 27 |
|
@@ -48,18 +52,32 @@ class InfoCommand extends AbstractCommand |
48 | 52 | */ |
49 | 53 | private $queue; |
50 | 54 |
|
| 55 | + /** |
| 56 | + * @var SessionManager |
| 57 | + */ |
| 58 | + private $session; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var SessionHandlerInterface |
| 62 | + */ |
| 63 | + private $sessionHandler; |
| 64 | + |
51 | 65 | public function __construct( |
52 | 66 | ExtensionManager $extensions, |
53 | 67 | Config $config, |
54 | 68 | SettingsRepositoryInterface $settings, |
55 | 69 | ConnectionInterface $db, |
56 | | - Queue $queue |
| 70 | + Queue $queue, |
| 71 | + SessionManager $session, |
| 72 | + SessionHandlerInterface $sessionHandler |
57 | 73 | ) { |
58 | 74 | $this->extensions = $extensions; |
59 | 75 | $this->config = $config; |
60 | 76 | $this->settings = $settings; |
61 | 77 | $this->db = $db; |
62 | 78 | $this->queue = $queue; |
| 79 | + $this->session = $session; |
| 80 | + $this->sessionHandler = $sessionHandler; |
63 | 81 |
|
64 | 82 | parent::__construct(); |
65 | 83 | } |
@@ -93,6 +111,7 @@ protected function fire() |
93 | 111 | $this->output->writeln('<info>Base URL:</info> '.$this->config->url()); |
94 | 112 | $this->output->writeln('<info>Installation path:</info> '.getcwd()); |
95 | 113 | $this->output->writeln('<info>Queue driver:</info> '.$this->identifyQueueDriver()); |
| 114 | + $this->output->writeln('<info>Session driver:</info> '.$this->identifySessionDriver()); |
96 | 115 | $this->output->writeln('<info>Mail driver:</info> '.$this->settings->get('mail_driver', 'unknown')); |
97 | 116 | $this->output->writeln('<info>Debug mode:</info> '.($this->config->inDebugMode() ? '<error>ON</error>' : 'off')); |
98 | 117 |
|
@@ -169,4 +188,53 @@ private function identifyDatabaseVersion(): string |
169 | 188 | { |
170 | 189 | return $this->db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); |
171 | 190 | } |
| 191 | + |
| 192 | + /** |
| 193 | + * Reports on the session driver in use based on three scenarios: |
| 194 | + * 1. If the configured session driver is valid and in use, it will be returned. |
| 195 | + * 2. If the configured session driver is invalid, fallback to the default one and mention it. |
| 196 | + * 3. If the actual used driver (i.e `session.handler`) is different from the current one (configured or default), mention it. |
| 197 | + */ |
| 198 | + private function identifySessionDriver(): string |
| 199 | + { |
| 200 | + /* |
| 201 | + * Get the configured driver and fallback to the default one. |
| 202 | + */ |
| 203 | + $defaultDriver = $this->session->getDefaultDriver(); |
| 204 | + $configuredDriver = Arr::get($this->config, 'session.driver', $defaultDriver); |
| 205 | + $driver = $configuredDriver; |
| 206 | + |
| 207 | + try { |
| 208 | + // Try to get the configured driver instance. |
| 209 | + // Driver instances are created on demand. |
| 210 | + $this->session->driver($configuredDriver); |
| 211 | + } catch (InvalidArgumentException $e) { |
| 212 | + // An exception is thrown if the configured driver is not a valid driver. |
| 213 | + // So we fallback to the default driver. |
| 214 | + $driver = $defaultDriver; |
| 215 | + } |
| 216 | + |
| 217 | + /* |
| 218 | + * Get actual driver name from its class name. |
| 219 | + * And compare that to the current configured driver. |
| 220 | + */ |
| 221 | + // Get class name |
| 222 | + $handlerName = get_class($this->sessionHandler); |
| 223 | + // Drop the namespace |
| 224 | + $handlerName = Str::afterLast($handlerName, '\\'); |
| 225 | + // Lowercase the class name |
| 226 | + $handlerName = strtolower($handlerName); |
| 227 | + // Drop everything like sessionhandler FileSessionHandler, DatabaseSessionHandler ..etc |
| 228 | + $handlerName = str_replace('sessionhandler', '', $handlerName); |
| 229 | + |
| 230 | + if ($driver !== $handlerName) { |
| 231 | + return "$handlerName <comment>(Code override. Configured to <options=bold,underscore>$configuredDriver</>)</comment>"; |
| 232 | + } |
| 233 | + |
| 234 | + if ($driver !== $configuredDriver) { |
| 235 | + return "$driver <comment>(Fallback default driver. Configured to invalid driver <options=bold,underscore>$configuredDriver</>)</comment>"; |
| 236 | + } |
| 237 | + |
| 238 | + return $driver; |
| 239 | + } |
172 | 240 | } |
0 commit comments