1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?php
namespace ICanBoogie\Session;
use ICanBoogie\SessionOptions;
class NormalizeOptions implements SessionOptions
{
static private $default_options = [
self::OPTION_ID => self::DEFAULT_ID,
self::OPTION_NAME => self::DEFAULT_NAME,
self::OPTION_CACHE_LIMITER => self::DEFAULT_CACHE_LIMITER,
self::OPTION_CACHE_EXPIRE => self::DEFAULT_CACHE_EXPIRE,
self::OPTION_MODULE_NAME => self::DEFAULT_MODULE_NAME,
self::OPTION_SAVE_PATH => self::DEFAULT_SAVE_PATH,
];
static private $default_cookie_params = [
CookieParams::OPTION_LIFETIME => CookieParams::DEFAULT_LIFETIME,
CookieParams::OPTION_PATH => CookieParams::DEFAULT_PATH,
CookieParams::OPTION_DOMAIN => CookieParams::DEFAULT_DOMAIN,
CookieParams::OPTION_SECURE => CookieParams::DEFAULT_SECURE,
CookieParams::OPTION_HTTP_ONLY => CookieParams::DEFAULT_HTTP_ONLY,
];
public function __invoke(array $options)
{
$default_options = static::resolve_default_options();
$options = array_intersect_key(array_replace_recursive($default_options, $options), $default_options);
if (empty($options[self::OPTION_ID])) {
unset($options[self::OPTION_ID]);
}
return $options;
}
protected function resolve_default_options()
{
return [
self::OPTION_NAME => ini_get('session.name'),
self::OPTION_CACHE_LIMITER => ini_get('session.cache_limiter'),
self::OPTION_CACHE_EXPIRE => ini_get('session.cache_expire'),
self::OPTION_COOKIE_PARAMS => $this->resolve_default_cookie_params(),
] + self::$default_options;
}
protected function resolve_default_cookie_params()
{
return session_get_cookie_params() + self::$default_cookie_params;
}
}