-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit efb6625
Showing
5 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
### Installation | ||
|
||
`composer require hazzard/cookie` | ||
|
||
### Usage | ||
|
||
``` | ||
use Hazzard\Cookie\Cookie; | ||
// Set a cookie for 60 minutes. | ||
Cookie::set('name', 'value', 60); | ||
// Set a cookie "forever". | ||
Cookie::forever('name', 'value'); | ||
// Retrieve a cookie. | ||
$value = Cookie::get('name'); | ||
// Forget a cookie. | ||
Cookie::forget('name'); | ||
``` | ||
|
||
|
||
##### Default path and domain | ||
|
||
``` | ||
Cookie::setDefaultPathAndDomain('/path/to', 'domain.com'); | ||
``` | ||
|
||
|
||
##### Encrypted cookies | ||
|
||
First `composer require hazzard/encryption`, then: | ||
|
||
``` | ||
use Hazzard\Cookie\Cookie; | ||
use Hazzard\Encryption\Encrypter; | ||
$encrypter = new Encrypter('a random 32 character string', 'AES-256-CBC'); | ||
Cookie::setEncrypter($encrypter); | ||
``` | ||
|
||
You can use any other encrypter as long as it has the `encrypt` and `decrypt` methods. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "hazzard/cookie", | ||
"description": "A simple cookie package.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Cretu Eusebiu", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Hazzard\\Cookie\\": "src/" | ||
} | ||
}, | ||
"suggest": { | ||
"hazzard/encryption": "Required to for encrypted cookies." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Hazzard\Cookie; | ||
|
||
class Cookie | ||
{ | ||
/** | ||
* The cookie jar instance. | ||
* | ||
* @var \Hazzard\Cookie\CookieJar | ||
*/ | ||
protected static $cookieJar; | ||
|
||
/** | ||
* Get the cookie jar instance. | ||
* | ||
* @return \Hazzard\Cookie\CookieJar | ||
*/ | ||
public static function getCookieJar() | ||
{ | ||
if (!isset(static::$cookieJar)) { | ||
static::$cookieJar = new CookieJar; | ||
} | ||
} | ||
|
||
/** | ||
* Call static cookie jar methods dynamically. | ||
* | ||
* @param string $method | ||
* @param array $arguments | ||
* @return mixed | ||
*/ | ||
public static function __callStatic($method, $arguments) | ||
{ | ||
$mailer = static::getCookieJar(); | ||
|
||
return call_user_func_array([$mailer, $method], $arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
namespace Hazzard\Cookie; | ||
|
||
class CookieJar implements CookieJarInterface | ||
{ | ||
/** | ||
* The default path (if specified). | ||
* | ||
* @var string | ||
*/ | ||
protected $path = '/'; | ||
|
||
/** | ||
* The default domain (if specified). | ||
* | ||
* @var string | ||
*/ | ||
protected $domain = null; | ||
|
||
/** | ||
* Encrypter instance. | ||
* | ||
* @var \Hazzard\Encryption\Encrypter | ||
*/ | ||
protected $encrypter; | ||
|
||
/** | ||
* Set a cookie. | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @param int $minutes | ||
* @param string $path | ||
* @param string $domain | ||
* @param bool $secure | ||
* @param bool $httpOnly | ||
* @return bool | ||
*/ | ||
public function set($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true) | ||
{ | ||
list($path, $domain) = $this->getPathAndDomain($path, $domain); | ||
|
||
$time = ($minutes == 0) ? 0 : time() + ($minutes * 60); | ||
|
||
if (isset($this->encrypter)) { | ||
$value = $this->encrypter->encrypt($value); | ||
} | ||
|
||
return setcookie($name, $value, $time, $path, $domain, $secure, $httpOnly); | ||
} | ||
|
||
/** | ||
* Retrieve a cookie. | ||
* | ||
* @param string $name | ||
* @param mixed $default | ||
* @return mixed | ||
*/ | ||
public function get($key, $default = null) | ||
{ | ||
if (array_key_exists($key, $_COOKIE)) { | ||
$value = $_COOKIE[$key]; | ||
|
||
if (isset($this->encrypter)) { | ||
$value = $this->encrypter->decrypt($value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
/** | ||
* Set a cookie that lasts "forever" (five years). | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @param string $path | ||
* @param string $domain | ||
* @param bool $secure | ||
* @param bool $httpOnly | ||
* @return bool | ||
*/ | ||
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true) | ||
{ | ||
return $this->set($name, $value, 2628000, $path, $domain, $secure, $httpOnly); | ||
} | ||
|
||
/** | ||
* Expire a cookie. | ||
* | ||
* @param string $name | ||
* @param string $path | ||
* @param string $domain | ||
* @return bool | ||
*/ | ||
public function forget($name, $path = null, $domain = null) | ||
{ | ||
return $this->set($name, null, -2628000, $path, $domain); | ||
} | ||
|
||
/** | ||
* Get the path and domain, or the default values. | ||
* | ||
* @param string $path | ||
* @param string $domain | ||
* @return array | ||
*/ | ||
protected function getPathAndDomain($path, $domain) | ||
{ | ||
return array($path ?: $this->path, $domain ?: $this->domain); | ||
} | ||
|
||
/** | ||
* Set the default path and domain for the jar. | ||
* | ||
* @param string $path | ||
* @param string $domain | ||
* @return $this | ||
*/ | ||
public function setDefaultPathAndDomain($path, $domain) | ||
{ | ||
list($this->path, $this->domain) = array($path, $domain); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set encrypter. | ||
* | ||
* @param \Hazzard\Encryption\Encrypter $encrypter | ||
* @return $this | ||
*/ | ||
public function setEncrypter($encrypter) | ||
{ | ||
$this->encrypter = $encrypter; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Hazzard\Cookie; | ||
|
||
interface CookieJarInterface | ||
{ | ||
/** | ||
* Set a cookie. | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @param int $minutes | ||
* @param string $path | ||
* @param string $domain | ||
* @param bool $secure | ||
* @param bool $httpOnly | ||
* @return bool | ||
*/ | ||
public function set($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true); | ||
|
||
/** | ||
* Retrieve a cookie. | ||
* | ||
* @param string $name | ||
* @param mixed $default | ||
* @return mixed | ||
*/ | ||
public function get($key, $default = null); | ||
|
||
/** | ||
* Set a cookie that lasts "forever" (five years). | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @param string $path | ||
* @param string $domain | ||
* @param bool $secure | ||
* @param bool $httpOnly | ||
* @return bool | ||
*/ | ||
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true); | ||
|
||
/** | ||
* Expire a cookie. | ||
* | ||
* @param string $name | ||
* @param string $path | ||
* @param string $domain | ||
* @return bool | ||
*/ | ||
public function forget($name, $path = null, $domain = null); | ||
} |