From 1b7cb3bec2e5f413e9c22d20d5a848c5c640cbe1 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 2 Nov 2017 00:51:31 +0100 Subject: [PATCH] The CookieFactory now also works if no configuration exists (#1258) * Returning the $default value if there's no config This is especially important for the CookieFactory which accesses the configuration before the application is installed * Injecting the configuration values into the CookieFactory --- src/Foundation/Application.php | 2 +- src/Http/CookieFactory.php | 52 +++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/Foundation/Application.php b/src/Foundation/Application.php index a432bdbb8..b3edee9fe 100644 --- a/src/Foundation/Application.php +++ b/src/Foundation/Application.php @@ -143,7 +143,7 @@ class Application extends Container implements ApplicationContract */ public function config($key, $default = null) { - return array_get($this->make('flarum.config'), $key, $default); + return $this->isInstalled() ? array_get($this->make('flarum.config'), $key, $default) : $default; } /** diff --git a/src/Http/CookieFactory.php b/src/Http/CookieFactory.php index aacaff7f5..0ddb3de4c 100644 --- a/src/Http/CookieFactory.php +++ b/src/Http/CookieFactory.php @@ -17,16 +17,46 @@ use Flarum\Foundation\Application; class CookieFactory { /** - * @var Application + * The prefix for the cookie names. + * + * @var string */ - protected $app; + protected $prefix; + + /** + * A path scope for the cookies. + * + * @var string + */ + protected $path; + + /** + * A domain scope for the cookies. + * + * @var string + */ + protected $domain; + + /** + * Whether the cookie(s) can be requested only over HTTPS. + * + * @var bool + */ + protected $secure; /** * @param Application $app */ public function __construct(Application $app) { - $this->app = $app; + // Parse the forum's base URL so that we can determine the optimal cookie settings + $url = parse_url(rtrim($app->url(), '/')); + + // Get the cookie settings from the config or use the default values + $this->prefix = $app->config('cookie.name', 'flarum'); + $this->path = $app->config('cookie.path', array_get($url, 'path') ?: '/'); + $this->domain = $app->config('cookie.domain'); + $this->secure = $app->config('cookie.secure', array_get($url, 'scheme') === 'https'); } /** @@ -42,9 +72,6 @@ class CookieFactory */ public function make($name, $value = null, $maxAge = null) { - // Parse the forum's base URL so that we can determine the optimal cookie settings - $url = parse_url(rtrim($this->app->url(), '/')); - $cookie = SetCookie::create($this->getName($name), $value); // Make sure we send both the MaxAge and Expires parameters (the former @@ -55,16 +82,13 @@ class CookieFactory ->withExpires(time() + $maxAge); } - if ($domain = $this->app->config('cookie.domain')) { - $cookie = $cookie->withDomain($domain); + if ($this->domain != null) { + $cookie = $cookie->withDomain($this->domain); } - $path = $this->app->config('cookie.path', array_get($url, 'path') ?: '/'); - $secure = $this->app->config('cookie.secure', array_get($url, 'scheme') === 'https'); - return $cookie - ->withPath($path) - ->withSecure($secure) + ->withPath($this->path) + ->withSecure($this->secure) ->withHttpOnly(true); } @@ -87,6 +111,6 @@ class CookieFactory */ public function getName($name) { - return $this->app->config('cookie.name', 'flarum').'_'.$name; + return $this->prefix.'_'.$name; } }