Skip to content

Commit

Permalink
Tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Feb 3, 2015
1 parent 2e6b762 commit 078b7da
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 175 deletions.
3 changes: 3 additions & 0 deletions lib/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class CallableDispatcher implements DispatcherInterface
{
private $callable;

/**
* @param callable $callable
*/
public function __construct($callable)
{
$this->callable = $callable;
Expand Down
4 changes: 2 additions & 2 deletions lib/Dispatcher/BeforeDispatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class BeforeDispatchEvent extends \ICanBoogie\Event
/**
* The event is constructed with the type `dispatch:before`.
*
* @param Dispatcher $target.
* @param Request $request The request.
* @param Dispatcher $target
* @param Request $request
* @param Response|null $response Reference to the response.
*/
public function __construct(Dispatcher $target, Request $request, &$response)
Expand Down
4 changes: 2 additions & 2 deletions lib/Dispatcher/DispatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class DispatchEvent extends \ICanBoogie\Event
/**
* The event is constructed with the type `dispatch`.
*
* @param Dispatcher $target.
* @param Request $request The request.
* @param Dispatcher $target
* @param Request $request
* @param mixed $response Reference to the response.
*/
public function __construct(Dispatcher $target, Request $request, &$response)
Expand Down
2 changes: 1 addition & 1 deletion lib/ForceRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @property-read string $location The location of the redirect.
*/
class ForceRedirect extends HTTPError
class ForceRedirect extends \Exception implements Exception
{
use AccessorTrait;

Expand Down
22 changes: 0 additions & 22 deletions lib/HTTPError.php

This file was deleted.

23 changes: 17 additions & 6 deletions lib/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ class Headers implements \ArrayAccess, \IteratorAggregate

];

/**
* Normalizes field name.
*
* @param string $name
*
* @return string
*/
static private function normalize_field_name($name)
{
return mb_convert_case(strtr(substr($name, 5), '_', '-'), MB_CASE_TITLE);
}

/**
* Header fields.
*
* @var array[string]mixed
* @var array
*/
protected $fields = [];

Expand All @@ -51,7 +63,7 @@ class Headers implements \ArrayAccess, \IteratorAggregate
*
* @param array $fields The initial headers.
*/
public function __construct(array $fields=[])
public function __construct(array $fields = [])
{
if (isset($fields['REQUEST_URI']))
{
Expand All @@ -62,8 +74,8 @@ public function __construct(array $fields=[])
continue;
}

$field = strtr(substr($field, 5), '_', '-');
$field = mb_convert_case($field, MB_CASE_TITLE);
$field = self::normalize_field_name($field);

$this[$field] = $value;
}
}
Expand All @@ -73,8 +85,7 @@ public function __construct(array $fields=[])
{
if (strpos($field, 'HTTP_') === 0)
{
$field = strtr(substr($field, 5), '_', '-');
$field = mb_convert_case($field, MB_CASE_TITLE);
$field = self::normalize_field_name($field);
}

$this[$field] = $value;
Expand Down
4 changes: 2 additions & 2 deletions lib/Headers/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CacheControl
/**
* Returns the default values of the instance.
*
* @return array[string]mixed
* @return array
*/
static protected function get_default_values()
{
Expand Down Expand Up @@ -303,7 +303,7 @@ protected function set_cacheable($value)
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.6
*
* @var string
* @var array
*/
public $extensions = [];

Expand Down
11 changes: 8 additions & 3 deletions lib/MethodNotSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
/**
* Exception thrown when the HTTP method is not supported.
*
* @property-read string $method The method that is not supported.
* @property-read string $method The unsupported HTTP method.
*/
class MethodNotSupported extends HTTPError
class MethodNotSupported extends \Exception implements Exception
{
use AccessorTrait;

Expand All @@ -29,7 +29,12 @@ protected function get_method()
return $this->method;
}

public function __construct($method, $code=500, \Exception $previous=null)
/**
* @param string $method The unsupported HTTP method.
* @param int $code
* @param \Exception $previous
*/
public function __construct($method, $code = 500, \Exception $previous = null)
{
$this->method = $method;

Expand Down
4 changes: 2 additions & 2 deletions lib/NotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
/**
* Exception thrown when a resource is not found.
*/
class NotFound extends HTTPError
class NotFound extends \Exception implements Exception
{
public function __construct($message='The requested URL was not found on this server.', $code=404, \Exception $previous=null)
public function __construct($message = 'The requested URL was not found on this server.', $code = 404, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
149 changes: 78 additions & 71 deletions lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,62 @@ class Request implements \ArrayAccess, \IteratorAggregate

];

static private $properties_mappers;

/**
* Returns request properties mappers.
*
* @return \Closure[]
*/
static protected function get_properties_mappers()
{
if (self::$properties_mappers)
{
return self::$properties_mappers;
}

return self::$properties_mappers = static::create_properties_mappers();
}

/**
* Returns request properties mappers.
*
* @return \Closure[]
*/
static protected function create_properties_mappers()
{
return [

'path_params' => function($value) { return $value; },
'query_params' => function($value) { return $value; },
'request_params' => function($value) { return $value; },
'cookie' => function($value) { return $value; },
'files' => function($value) { return $value; },
'headers' => function($value) { return ($value instanceof Headers) ? $value : new Headers($value); },

'cache_control' => function($value, array &$env) { $env['HTTP_CACHE_CONTROL'] = $value; },
'content_length' => function($value, array &$env) { $env['CONTENT_LENGTH'] = $value; },
'ip' => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = $value; },
'is_local' => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = '::1'; },
'is_delete' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_DELETE; },
'is_connect' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_CONNECT; },
'is_get' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_GET; },
'is_head' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_HEAD; },
'is_options' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_OPTIONS; },
'is_patch' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PATCH; },
'is_post' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_POST; },
'is_put' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PUT; },
'is_trace' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_TRACE; },
'is_xhr' => function($value, array &$env) { if ($value) $env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; else unset($env['HTTP_X_REQUESTED_WITH']); },
'method' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = $value; },
'path' => function($value, array &$env) { $env['REQUEST_URI'] = $value; }, // TODO-20130521: handle query string
'referer' => function($value, array &$env) { $env['HTTP_REFERER'] = $value; },
'uri' => function($value, array &$env) { $env['REQUEST_URI'] = $value; $qs = strpos($value, '?'); $env['QUERY_STRING'] = $qs === false ? '' : substr($value, $qs + 1); },
'user_agent' => function($value, array &$env) { $env['HTTP_USER_AGENT'] = $value; }

];
}

/**
* Current request.
*
Expand Down Expand Up @@ -298,7 +354,7 @@ static protected function from_properties(array $properties, array $env)
{
$properties = $properties ?: [];

$mappers = self::get_properties_mappers();
$mappers = static::get_properties_mappers();

foreach ($properties as $property => &$value)
{
Expand All @@ -323,52 +379,6 @@ static protected function from_properties(array $properties, array $env)
return new static($properties, $env);
}

/**
* Returns properties to env mappers.
*
* @return array
*/
static public function get_properties_mappers()
{
static $mappers;

if (!$mappers)
{
$mappers = [

'path_params' => function($value) { return $value; },
'query_params' => function($value) { return $value; },
'request_params' => function($value) { return $value; },
'cookie' => function($value) { return $value; },
'files' => function($value) { return $value; },
'headers' => function($value) { return ($value instanceof Headers) ? $value : new Headers($value); },

'cache_control' => function($value, array &$env) { $env['HTTP_CACHE_CONTROL'] = $value; },
'content_length' => function($value, array &$env) { $env['CONTENT_LENGTH'] = $value; },
'ip' => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = $value; },
'is_local' => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = '::1'; },
'is_delete' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_DELETE; },
'is_connect' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_CONNECT; },
'is_get' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_GET; },
'is_head' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_HEAD; },
'is_options' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_OPTIONS; },
'is_patch' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PATCH; },
'is_post' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_POST; },
'is_put' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PUT; },
'is_trace' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_TRACE; },
'is_xhr' => function($value, array &$env) { if ($value) $env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; else unset($env['HTTP_X_REQUESTED_WITH']); },
'method' => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = $value; },
'path' => function($value, array &$env) { $env['REQUEST_URI'] = $value; }, // TODO-20130521: handle query string
'referer' => function($value, array &$env) { $env['HTTP_REFERER'] = $value; },
'uri' => function($value, array &$env) { $env['REQUEST_URI'] = $value; $qs = strpos($value, '?'); $env['QUERY_STRING'] = $qs === false ? '' : substr($value, $qs + 1); },
'user_agent' => function($value, array &$env) { $env['HTTP_USER_AGENT'] = $value; }

];
}

return $mappers;
}

/**
* Initialize the properties {@link $env}, {@link $headers} and {@link $context}.
*
Expand All @@ -382,27 +392,21 @@ static public function get_properties_mappers()
*/
protected function __construct(array $properties, array $env=[])
{
$this->context = new Request\Context($this);
$this->env = $env;

foreach ($properties as $property => $value)
{
$this->$property = $value;
}

$method = $this->method;

if (!in_array($method, self::$methods))
{
throw new MethodNotSupported($method);
}
$this->assert_method($this->method);

if (!$this->headers)
{
$this->headers = new Headers($env);
}

$this->context = new Request\Context($this);

if ($this->params === null)
{
$this->params = $this->path_params + $this->request_params + $this->query_params;
Expand Down Expand Up @@ -481,6 +485,21 @@ protected function dispatch()
return dispatch($this); // @codeCoverageIgnore
}

/**
* Asserts that a method is supported.
*
* @param string $method
*
* @throws MethodNotSupported
*/
private function assert_method($method)
{
if (!in_array($method, self::$methods))
{
throw new MethodNotSupported($method);
}
}

/**
* Return a new instance with the specified changed properties.
*
Expand All @@ -493,7 +512,7 @@ protected function dispatch()
public function change(array $properties)
{
$changed = clone $this;
$mappers = $this->properties_mappers;
$mappers = static::get_properties_mappers();
$env = &$changed->env;

foreach ($properties as $property => $value)
Expand Down Expand Up @@ -844,26 +863,14 @@ protected function get_is_xhr()
*/
protected function get_is_local()
{
static $patterns = [ '::1', '/^127\.0\.0\.\d{1,3}$/', '/^0:0:0:0:0:0:0:1(%.*)?$/' ];

$ip = $this->ip;

foreach ($patterns as $pattern)
if ($ip == '::1' || preg_match('/^127\.0\.0\.\d{1,3}$/', $ip))
{
if ($pattern{0} == '/')
{
if (preg_match($pattern, $ip))
{
return true;
}
}
else if ($pattern == $ip)
{
return true;
}
return true;
}

return false;
return preg_match('/^0:0:0:0:0:0:0:1(%.*)?$/', $ip);
}

/**
Expand Down
Loading

0 comments on commit 078b7da

Please sign in to comment.