silverstripe-framework/Control/Cookie.php

85 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Control;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;
/**
* A set of static methods for manipulating cookies.
*/
class Cookie {
use Configurable;
/**
* @config
2015-11-13 23:44:49 +01:00
*
* @var bool
*/
private static $report_errors = true;
/**
2015-11-13 23:44:49 +01:00
* Fetch the current instance of the cookie backend.
NEW Cookie_Backend for managing cookie state I've decoupled `Cookie` from the actual act of setting and getting cookies. Currently there are a few limitations to how Cookie works that this change mitigates: 0. `Cookie` currently changes the super global `$_COOKIE` when setting to make the state of an application a bit more managable, but this is bad because we shouldn't be modifying super globals 0. One can't actually change the `$cookie_class` once the `Cookie::$inst` has been instantiated 0. One can't test cookies as there is no class that holds the state of the cookies (it's just held in the super global which is reset as part of `Director::test()` 0. One can't tell the origin of a cookie (eg: did the application set it and it needs to be sent, or did we receive it from the browser?) 0. `time()` was used, so testing was made difficult 0. There was no way to get all the cookies at once (without accessing the super global) Todos are on the phpdoc and I'd like to write some tests for the backend as well as update the docs (if there are any) around cookies. DOCS Adding `Cookie` docs Explains basic usage of `Cookie` as well as how the `Cookie_Backend` controls the setting and getting of cookies and manages state of sent vs received cookies Fixing `Cookie` usage `Cookie` is being used inconsistently with the API throughout framework. Either by not using `force_expiry` to expire cookies or setting them to null and then expiring them (which is redundant). NEW `Director::test()` takes `Cookie_Backend` rather than `array` for `$cookies` param
2014-05-04 15:34:58 +02:00
*
2015-11-13 23:44:49 +01:00
* @return Cookie_Backend
*/
public static function get_inst() {
return Injector::inst()->get('SilverStripe\\Control\\Cookie_Backend');
}
/**
2015-11-13 23:44:49 +01:00
* Set a cookie variable.
*
* Expiry time is set in days, and defaults to 90.
*
2015-11-13 23:44:49 +01:00
* @param string $name
* @param mixed $value
* @param int $expiry
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
*
* See http://php.net/set_session
*/
2014-08-15 08:53:05 +02:00
public static function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false,
$httpOnly = true
) {
NEW Cookie_Backend for managing cookie state I've decoupled `Cookie` from the actual act of setting and getting cookies. Currently there are a few limitations to how Cookie works that this change mitigates: 0. `Cookie` currently changes the super global `$_COOKIE` when setting to make the state of an application a bit more managable, but this is bad because we shouldn't be modifying super globals 0. One can't actually change the `$cookie_class` once the `Cookie::$inst` has been instantiated 0. One can't test cookies as there is no class that holds the state of the cookies (it's just held in the super global which is reset as part of `Director::test()` 0. One can't tell the origin of a cookie (eg: did the application set it and it needs to be sent, or did we receive it from the browser?) 0. `time()` was used, so testing was made difficult 0. There was no way to get all the cookies at once (without accessing the super global) Todos are on the phpdoc and I'd like to write some tests for the backend as well as update the docs (if there are any) around cookies. DOCS Adding `Cookie` docs Explains basic usage of `Cookie` as well as how the `Cookie_Backend` controls the setting and getting of cookies and manages state of sent vs received cookies Fixing `Cookie` usage `Cookie` is being used inconsistently with the API throughout framework. Either by not using `force_expiry` to expire cookies or setting them to null and then expiring them (which is redundant). NEW `Director::test()` takes `Cookie_Backend` rather than `array` for `$cookies` param
2014-05-04 15:34:58 +02:00
return self::get_inst()->set($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
}
/**
2015-11-13 23:44:49 +01:00
* Get the cookie value by name. Returns null if not set.
*
2015-11-13 23:44:49 +01:00
* @param string $name
* @param bool $includeUnsent
*
2015-11-13 23:44:49 +01:00
* @return null|string
*/
public static function get($name, $includeUnsent = true) {
return self::get_inst()->get($name, $includeUnsent);
}
/**
2015-11-13 23:44:49 +01:00
* Get all the cookies.
*
* @param bool $includeUnsent
*
2015-11-13 23:44:49 +01:00
* @return array
*/
public static function get_all($includeUnsent = true) {
return self::get_inst()->getAll($includeUnsent);
}
/**
2015-11-13 23:44:49 +01:00
* @param string $name
* @param null|string $path
* @param null|string $domain
* @param bool $secure
* @param bool $httpOnly
*/
public static function force_expiry($name, $path = null, $domain = null, $secure = false, $httpOnly = true) {
return self::get_inst()->forceExpiry($name, $path, $domain, $secure, $httpOnly);
}
}