the whole shebang

This commit is contained in:
2014-11-25 16:42:40 +01:00
parent 7f74c0613e
commit ab1334c0cf
3686 changed files with 496409 additions and 1 deletions

View File

@@ -0,0 +1,199 @@
<?php namespace Illuminate\Cookie;
use Closure;
use Illuminate\Encryption\Encrypter;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CookieJar {
/*
* The current request instance.
*
* @var Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The encrypter instance.
*
* @var \Illuminate\Encryption\Encrypter
*/
protected $encrypter;
/**
* The default path (if specified).
*
* @var string
*/
protected $path = '/';
/**
* The default domain (if specified).
*
* @var string
*/
protected $domain = null;
/**
* Create a new cookie manager instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Illuminate\Encryption\Encrypter $encrypter
* @return void
*/
public function __construct(Request $request, Encrypter $encrypter)
{
$this->request = $request;
$this->encrypter = $encrypter;
}
/**
* Determine if a cookie exists and is not null.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ! is_null($this->get($key));
}
/**
* Get the value of the given cookie.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
$value = $this->request->cookies->get($key);
if ( ! is_null($value))
{
return $this->decrypt($value);
}
return $default instanceof Closure ? $default() : $default;
}
/**
* Decrypt the given cookie value.
*
* @param string $value
* @return mixed|null
*/
protected function decrypt($value)
{
try
{
return $this->encrypter->decrypt($value);
}
catch (\Exception $e)
{
return null;
}
}
/**
* Create a new cookie instance.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
list($path, $domain) = $this->getPathAndDomain($path, $domain);
// Once we calculate the time we can encrypt the message. All cookies will be
// encrypted using the Illuminate encryption component and will have a MAC
// assigned to them by the encrypter to make sure they remain authentic.
$time = ($minutes == 0) ? 0 : time() + ($minutes * 60);
$value = $this->encrypter->encrypt($value);
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly);
}
/**
* Create 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 \Symfony\Component\HttpFoundation\Cookie
*/
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly);
}
/**
* Expire the given cookie.
*
* @param string $name
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forget($name)
{
return $this->make($name, null, -2628000);
}
/**
* 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 void
*/
public function setDefaultPathAndDomain($path, $domain)
{
list($this->path, $this->domain) = array($path, $domain);
return $this;
}
/**
* Get the request instance.
*
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Get the encrypter instance.
*
* @return \Illuminate\Encryption\Encrypter
*/
public function getEncrypter()
{
return $this->encrypter;
}
}

View File

@@ -0,0 +1,24 @@
<?php namespace Illuminate\Cookie;
use Illuminate\Support\ServiceProvider;
class CookieServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['cookie'] = $this->app->share(function($app)
{
$cookies = new CookieJar($app['request'], $app['encrypter']);
$config = $app['config']['session'];
return $cookies->setDefaultPathAndDomain($config['path'], $config['domain']);
});
}
}

View File

@@ -0,0 +1,32 @@
{
"name": "illuminate/cookie",
"license": "MIT",
"authors": [
{
"name": "Taylor Otwell",
"email": "taylorotwell@gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/encryption": "4.0.x",
"illuminate/support": "4.0.x",
"symfony/http-foundation": "2.3.*"
},
"require-dev": {
"mockery/mockery": "0.7.2",
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Illuminate\\Cookie": ""
}
},
"target-dir": "Illuminate/Cookie",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"minimum-stability": "dev"
}