Skip to content

Commit

Permalink
Merge pull request #862 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
Implementation of a Laravel-esque Request, Input (and Response) support
  • Loading branch information
daveismynamecom committed May 21, 2016
2 parents 55294c0 + fd6ee98 commit 8c33b24
Show file tree
Hide file tree
Showing 24 changed files with 1,901 additions and 12 deletions.
5 changes: 3 additions & 2 deletions app/Config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
Config::set('classAliases', array(
'Config' => '\Core\Config',
'Errors' => '\Core\Error',
'Request' => '\Core\Request',
'Response' => '\Core\Response',
'Redirect' => '\Core\Redirect',
'Mail' => '\Helpers\Mailer',
Expand All @@ -167,11 +166,13 @@
'TableBuilder' => '\Helpers\TableBuilder',
'Tags' => '\Helpers\Tags',
'Url' => '\Helpers\Url',
// The Facades
// The Support Facades
'Auth' => '\Auth\Auth',
'DB' => '\Support\Facades\Database',
'Event' => '\Support\Facades\Event',
'Input' => '\Support\Facades\Input',
'Language' => '\Support\Facades\Language',
'Request' => '\Support\Facades\Request',
'Validator' => '\Support\Facades\Validator',
// The Legacy Mailer
'Helpers\PhpMailer\Mail' => '\Helpers\Mailer',
Expand Down
26 changes: 26 additions & 0 deletions app/Controllers/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

use Event;
use Validator;
use Input;
use Request;

use App\Models\ORM\User;


/*
*
* Demo controller
Expand Down Expand Up @@ -52,6 +55,29 @@ public function test($param1 = '', $param2 = '', $param3 = '', $param4 = '')
echo '<pre>' .var_export($params, true) .'</pre>';
}

public function request($param1 = '', $param2 = '', $param3 = '', $param4 = '')
{
echo '<h3>HTTP Request</h3>';

echo '<pre>' .var_export(Request::root(), true).'</pre>';

echo '<pre>' .var_export(Request::url(), true).'</pre>';

echo '<pre>' .var_export(Request::path(), true).'</pre>';

echo '<pre>' .var_export(Request::segments(), true).'</pre>';

echo '<pre>' .var_export(Request::segment(1), true).'</pre>';

echo '<pre>' .var_export(Request::isGet(), true).'</pre>';

echo '<pre>' .var_export(Request::isPost(), true).'</pre>';

echo '<pre>' .var_export(Input::all(), true).'</pre>';

echo '<pre>' .var_export(Request::createFromGlobals(), true).'</pre>';
}

public function events()
{
echo '<h3>Events dispatching</h3>';
Expand Down
2 changes: 2 additions & 0 deletions app/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
Router::any('demo/events', 'App\Controllers\Demo@events');
Router::any('demo/validate', 'App\Controllers\Demo@validate');

Router::any('demo/request(/(:any)(/(:any)(/(:all))))', 'App\Controllers\Demo@request');

Router::any('admin/(:any)(/(:any)(/(:any)(/(:all))))', array(
'filters' => 'test',
'uses' => 'App\Controllers\Demo@test'
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"symfony/console": "3.0.*",
"doctrine/inflector": "1.0.*",
"phpmailer/phpmailer": "5.2.*",
"phpfastcache/phpfastcache": "3.0.*"
"phpfastcache/phpfastcache": "3.0.*",
"symfony/http-foundation": "^3.0",
"patchwork/utf8": "~1.2"
},
"autoload": {
"psr-4": {
Expand Down
167 changes: 167 additions & 0 deletions system/Cookie/CookieJar.php
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;
}

}
6 changes: 5 additions & 1 deletion system/Database/ORM/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
use Database\Query\Builder as QueryBuilder;
use Database\ORM\Builder;

use Support\Contracts\ArrayableInterface;
use Support\Contracts\JsonableInterface;

use \PDO;
use ArrayAccess;


class Model implements \ArrayAccess
class Model implements ArrayableInterface, JsonableInterface, ArrayAccess
{
/**
* The Database Connection name.
Expand Down
84 changes: 84 additions & 0 deletions system/Http/JsonResponse.php
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;
}

}
Loading

0 comments on commit 8c33b24

Please sign in to comment.