public function register(Application $app) { parent::register($app); if (!isset($app['session.cookie.options'])) { $app['session.cookie.options'] = []; } $this->app = $app; $app['session.storage.handler'] = $app->share(function ($app) { $options = CookieSessionServiceProvider::mergeDefaultOptions(isset($app['session.cookie.options']) ? $app['session.cookie.options'] : []); return new CookieSessionHandler($options['name'], $options['lifetime'], $options['path'], $options['domain'], $options['secure'], $options['httponly']); }); $app['session.storage.native'] = $app->share(function ($app) { return new PhpBridgeSessionStorage($app['session.storage.handler']); }); $app['session'] = $app->share(function ($app) { if (!isset($app['session.storage'])) { if ($app['session.test']) { $app['session.storage'] = $app['session.storage.test']; } else { $app['session.storage'] = $app['session.storage.native']; } } $options = CookieSessionServiceProvider::mergeDefaultOptions(isset($app['session.cookie.options']) ? $app['session.cookie.options'] : []); $session = new CookieSession($app['session.storage']); $session->setName($options['name']); return $session; }); }
/** * get session data from cookie stored on the client side * @param Application|string $app - if Application we retrieve the cookie name from the 'session.cookie.options' configuration else we assume this is the name of the cookie * @throws RuntimeException - when passed application has not registered CookieSessionServiceProvider * @return string */ public function getCookieSessionName($app) { if ($app instanceof Application) { if (isset($app['session.cookie.options'])) { $options = CookieSessionServiceProvider::mergeDefaultOptions(isset($app['session.cookie.options']) ? $app['session.cookie.options'] : []); $app = $options['name']; } else { throw new \RuntimeException('You need to register Renegare\\SilexCSH\\CookieSessionServiceProvider in order to determine the correct cookie session name.'); } } if (!is_string($app)) { throw new \RuntimeException('$app must be either be an instance of Silex\\Application or a string.'); } return $app; }