-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #862 from LuckyCyborg/master
Implementation of a Laravel-esque Request, Input (and Response) support
- Loading branch information
Showing
24 changed files
with
1,901 additions
and
12 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
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
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
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
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,167 @@ | ||
<?php | ||
|
||
namespace Cookie; | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
|
||
class CookieJar | ||
{ | ||
/** | ||
* The default path (if specified). | ||
* | ||
* @var string | ||
*/ | ||
protected $path = '/'; | ||
|
||
/** | ||
* The default domain (if specified). | ||
* | ||
* @var string | ||
*/ | ||
protected $domain = null; | ||
|
||
/** | ||
* All of the cookies queued for sending. | ||
* | ||
* @var array | ||
*/ | ||
protected $queued = array(); | ||
|
||
/** | ||
* 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); | ||
|
||
$time = ($minutes == 0) ? 0 : time() + ($minutes * 60); | ||
|
||
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 | ||
* @param string $path | ||
* @param string $domain | ||
* @return \Symfony\Component\HttpFoundation\Cookie | ||
*/ | ||
public function forget($name, $path = null, $domain = null) | ||
{ | ||
return $this->make($name, null, -2628000, $path, $domain); | ||
} | ||
|
||
/** | ||
* Determine if a Cookie has been queued. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function hasQueued($key) | ||
{ | ||
return ! is_null($this->queued($key)); | ||
} | ||
|
||
/** | ||
* Get a queued Cookie instance. | ||
* | ||
* @param string $key | ||
* @param mixed $default | ||
* @return \Symfony\Component\HttpFoundation\Cookie | ||
*/ | ||
public function queued($key, $default = null) | ||
{ | ||
return array_get($this->queued, $key, $default); | ||
} | ||
|
||
/** | ||
* Queue a Cookie to send with the next response. | ||
* | ||
* @param dynamic | ||
* @return void | ||
*/ | ||
public function queue() | ||
{ | ||
if (head(func_get_args()) instanceof Cookie) { | ||
$cookie = head(func_get_args()); | ||
} else { | ||
$cookie = call_user_func_array(array($this, 'make'), func_get_args()); | ||
} | ||
|
||
$this->queued[$cookie->getName()] = $cookie; | ||
} | ||
|
||
/** | ||
* Remove a cookie from the queue. | ||
* | ||
* @param $cookieName | ||
*/ | ||
public function unqueue($name) | ||
{ | ||
unset($this->queued[$name]); | ||
} | ||
|
||
/** | ||
* 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 self | ||
*/ | ||
public function setDefaultPathAndDomain($path, $domain) | ||
{ | ||
list($this->path, $this->domain) = array($path, $domain); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the cookies which have been queued for the next request | ||
* | ||
* @return array | ||
*/ | ||
public function getQueuedCookies() | ||
{ | ||
return $this->queued; | ||
} | ||
|
||
} |
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
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,84 @@ | ||
<?php | ||
|
||
namespace Http; | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Support\Contracts\JsonableInterface; | ||
|
||
class JsonResponse extends \Symfony\Component\HttpFoundation\JsonResponse { | ||
|
||
/** | ||
* The json encoding options. | ||
* | ||
* @var int | ||
*/ | ||
protected $jsonOptions; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mixed $data | ||
* @param int $status | ||
* @param array $headers | ||
* @param int $options | ||
*/ | ||
public function __construct($data = null, $status = 200, $headers = array(), $options = 0) | ||
{ | ||
$this->jsonOptions = $options; | ||
|
||
parent::__construct($data, $status, $headers); | ||
} | ||
|
||
/** | ||
* Get the json_decoded data from the response | ||
* | ||
* @param bool $assoc | ||
* @param int $depth | ||
* @return mixed | ||
*/ | ||
public function getData($assoc = false, $depth = 512) | ||
{ | ||
return json_decode($this->data, $assoc, $depth); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setData($data = array()) | ||
{ | ||
$this->data = ($data instanceof JsonableInterface) | ||
? $data->toJson($this->jsonOptions) | ||
: json_encode($data, $this->jsonOptions); | ||
|
||
return $this->update(); | ||
} | ||
|
||
/** | ||
* Set a header on the Response. | ||
* | ||
* @param string $key | ||
* @param string $value | ||
* @param bool $replace | ||
* @return \Http\Response | ||
*/ | ||
public function header($key, $value, $replace = true) | ||
{ | ||
$this->headers->set($key, $value, $replace); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a cookie to the response. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\Cookie $cookie | ||
* @return \Http\Response | ||
*/ | ||
public function withCookie(Cookie $cookie) | ||
{ | ||
$this->headers->setCookie($cookie); | ||
|
||
return $this; | ||
} | ||
|
||
} |
Oops, something went wrong.