aboutsummaryrefslogtreecommitdiffstats
path: root/Http
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-20 09:27:10 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-20 09:27:10 +0200
commit0a0ca46899138ce3d79bfd9976801bbd5066108a (patch)
tree3e95e462a9c42880dc6b8c75aea5010942af0dc6 /Http
parenta2fba5e02f3f41364c4cd8ae2eb5fadd70d0c482 (diff)
downloadRouter-0a0ca46899138ce3d79bfd9976801bbd5066108a.zip
Router-0a0ca46899138ce3d79bfd9976801bbd5066108a.tar.gz
Router-0a0ca46899138ce3d79bfd9976801bbd5066108a.tar.bz2
Move to PSR-1 and PSR-2.
Diffstat (limited to 'Http')
-rw-r--r--Http/Dav.php12
-rw-r--r--Http/Http.php543
2 files changed, 301 insertions, 254 deletions
diff --git a/Http/Dav.php b/Http/Dav.php
index 978ec27..24427a8 100644
--- a/Http/Dav.php
+++ b/Http/Dav.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -41,17 +41,15 @@ namespace Hoa\Router\Http;
*
* DAV router (extension of HTTP).
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Dav extends Http {
-
+class Dav extends Http
+{
/**
* DAV methods that the router understand.
*
- * @var \Hoa\Router\Http\Dav array
+ * @var array
*/
protected static $_methods = [
'acl',
diff --git a/Http/Http.php b/Http/Http.php
index c46ef86..6ee3fba 100644
--- a/Http/Http.php
+++ b/Http/Http.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -44,13 +44,11 @@ use Hoa\Router;
*
* HTTP router.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Http extends Router\Generic implements Core\Parameter\Parameterizable {
-
+class Http extends Router\Generic implements Core\Parameter\Parameterizable
+{
/**
* Secure connection.
*
@@ -68,28 +66,28 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Parameters.
*
- * @var \Hoa\Core\Parameter object
+ * @var \Hoa\Core\Parameter
*/
protected $_parameters = null;
/**
* Path prefix.
*
- * @var \Hoa\Router\Http string
+ * @var string
*/
protected $_pathPrefix = null;
/**
* HTTP port.
*
- * @var \Hoa\Router\Http int
+ * @var int
*/
protected $_httpPort = 80;
/**
* HTTPS port.
*
- * @var \Hoa\Router\Http int
+ * @var int
*/
protected $_httpsPort = 443;
@@ -97,7 +95,7 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* HTTP methods that the router understand.
* Must map http://www.iana.org/assignments/http-methods/http-methods.xhtml.
*
- * @var \Hoa\Router\Http array
+ * @var array
*/
protected static $_methods = [
'connect',
@@ -118,7 +116,7 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* A subdomain is said dynamic if at least one rule pattern considers
* subdomain. It changes the default rules filter behavior.
*
- * @var \Hoa\Router\Http int
+ * @var int
*/
protected $_subdomainStack = _static;
@@ -126,7 +124,7 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* Subdomain suffix.
* A string to append to subdomain on each rule.
*
- * @var \Hoa\Router\Http string
+ * @var string
*/
protected $_subdomainSuffix = null;
@@ -135,11 +133,10 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Constructor.
*
- * @access public
* @return void
*/
- public function __construct ( Array $parameters = [] ) {
-
+ public function __construct(Array $parameters = [])
+ {
$this->_parameters = new Core\Parameter(
$this,
[],
@@ -151,31 +148,32 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
);
$this->_parameters->setParameters($parameters);
- if(null === $prefix = $this->_parameters->getParameter('prefix'))
+ if (null === $prefix = $this->_parameters->getParameter('prefix')) {
$this->setPrefix(
('\\' === $_ = dirname($this->getBootstrap())) ? '/' : $_
);
- else
+ } else {
$this->setPrefix($prefix);
+ }
- foreach($this->_parameters->getParameter('rules.public') as $id => $rule) {
-
+ foreach ($this->_parameters->getParameter('rules.public') as $id => $rule) {
@list($methods, $pattern, $call, $able, $variables)
= $rule;
- if(null === $variables)
+ if (null === $variables) {
$variables = [];
+ }
$this->addRule($id, $methods, $pattern, $call, $able, $variables);
}
- foreach($this->_parameters->getParameter('rules.private') as $id => $rule) {
-
+ foreach ($this->_parameters->getParameter('rules.private') as $id => $rule) {
@list($methods, $pattern, $call, $able, $variables)
= $rule;
- if(null === $variables)
+ if (null === $variables) {
$variables = [];
+ }
$this->addPrivateRule(
$id, $methods, $pattern, $call, $able, $variables
@@ -190,18 +188,16 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get parameters.
*
- * @access public
* @return \Hoa\Core\Parameter
*/
- public function getParameters ( ) {
-
+ public function getParameters()
+ {
return $this->_parameters;
}
/**
* Fallback for add*Rule() methods.
*
- * @access public
* @param int $visibility Visibility (please, see
* Router::VISIBILITY_* constants).
* @param string $id ID.
@@ -211,32 +207,49 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* @param mixed $able Able (second part).
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router\Http
- * @throw \Hoa\Router\Exception
- */
- protected function _addRule ( $visibility, $id, Array $methods, $pattern,
- $call, $able, Array $variables ) {
-
- if(true === $this->ruleExists($id))
+ * @throws \Hoa\Router\Exception
+ */
+ protected function _addRule(
+ $visibility,
+ $id,
+ Array $methods,
+ $pattern,
+ $call,
+ $able,
+ Array $variables
+ ) {
+ if (true === $this->ruleExists($id)) {
throw new Router\Exception(
- 'Cannot add rule %s because it already exists.', 0, $id);
-
- array_walk($methods, function ( &$method ) {
+ 'Cannot add rule %s because it already exists.',
+ 0,
+ $id
+ );
+ }
+ array_walk($methods, function (&$method) {
$method = strtolower($method);
});
$diff = array_diff($methods, static::$_methods);
- if(!empty($diff))
+ if (!empty($diff)) {
throw new Router\Exception(
(1 == count($diff)
? 'Method %s is'
: 'Methods %s are') .
' invalid for the rule %s (valid methods are: %s).',
- 1, [implode(', ', $diff), $id, implode(', ', static::$_methods)]);
+ 1,
+ [
+ implode(', ', $diff),
+ $id,
+ implode(', ', static::$_methods)
+ ]
+ );
+ }
- if( _static == $this->_subdomainStack
- && false != strpos($pattern, '@'))
+ if (_static === $this->_subdomainStack &&
+ false !== strpos($pattern, '@')) {
$this->_subdomainStack = _dynamic;
+ }
$this->_rules[$id] = [
Router::RULE_VISIBILITY => $visibility,
@@ -255,41 +268,41 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* Find the appropriated rule.
* Special variables: _domain, _subdomain, _call, _able and _request.
*
- * @access public
* @param string $uri URI. If null, it will be deduced. Can contain
* subdomain.
* @param string $prefix Path prefix. If null, it will be deduced.
* @return \Hoa\Router\Http
- * @throw \Hoa\Router\Exception\NotFound
+ * @throws \Hoa\Router\Exception\NotFound
*/
- public function route ( $uri = null, $prefix = null ) {
-
- if(null === $uri) {
-
+ public function route($uri = null, $prefix = null)
+ {
+ if (null === $uri) {
$uri = static::getURI();
$subdomain = $this->getSubdomain();
- }
- else {
-
- if(false !== $pos = strpos($uri, '@'))
+ } else {
+ if (false !== $pos = strpos($uri, '@')) {
list($subdomain, $uri) = explode('@', $uri, 2);
- else
- $subdomain = $this->getSubdomain();
+ } else {
+ $subdomain = $this->getSubdomain();
+ }
$uri = ltrim(urldecode($uri), '/');
}
- if(null === $prefix)
+ if (null === $prefix) {
$prefix = $this->getPrefix();
+ }
- if(!empty($prefix)) {
-
+ if (!empty($prefix)) {
$prefix = ltrim($prefix, '/');
- if(0 === preg_match('#^' . $prefix . '(.*)?$#', $uri, $matches))
+ if (0 === preg_match('#^' . $prefix . '(.*)?$#', $uri, $matches)) {
throw new Router\Exception\NotFound(
'Cannot match the path prefix %s in the URI %s.',
- 3, [$prefix, $uri]);
+ 2,
+ [$prefix, $uri]
+ );
+ }
$uri = ltrim($matches[1], '/');
}
@@ -303,24 +316,30 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
$subdomainStack = $this->getSubdomainStack();
$subdomainSuffix = $this->getSubdomainSuffix();
- if(null !== $subdomainSuffix)
+ if (null !== $subdomainSuffix) {
$subdomainSuffix = '\.' . $subdomainSuffix;
+ }
$rules = array_filter(
$this->getRules(),
- function ( $rule ) use ( &$method, &$subdomain, &$subdomainStack,
- &$subdomainSuffix ) {
-
- if(Router::VISIBILITY_PUBLIC != $rule[Router::RULE_VISIBILITY])
+ function ($rule) use (
+ &$method,
+ &$subdomain,
+ &$subdomainStack,
+ &$subdomainSuffix
+ ) {
+ if (Router::VISIBILITY_PUBLIC != $rule[Router::RULE_VISIBILITY]) {
return false;
+ }
- if(false === in_array($method, $rule[Router::RULE_METHODS]))
+ if (false === in_array($method, $rule[Router::RULE_METHODS])) {
return false;
+ }
- if(false !== $pos = strpos($rule[Router::RULE_PATTERN], '@'))
- if(empty($subdomain))
+ if (false !== $pos = strpos($rule[Router::RULE_PATTERN], '@')) {
+ if (empty($subdomain)) {
return false;
- else
+ } else {
return 0 !== preg_match(
'#^' .
substr($rule[Router::RULE_PATTERN], 0, $pos) .
@@ -328,40 +347,51 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
'$#i',
$subdomain
);
+ }
+ }
- return _dynamic == $subdomainStack
- ? empty($subdomain)
- : true;
+ return
+ _dynamic == $subdomainStack
+ ? empty($subdomain)
+ : true;
}
);
- if(empty($rules))
+ if (empty($rules)) {
throw new Router\Exception\NotFound(
- 'No rule to apply to route %s.', 4, $uri);
+ 'No rule to apply to route %s.',
+ 3,
+ $uri
+ );
+ }
$gotcha = false;
- foreach($rules as $rule) {
-
+ foreach ($rules as $rule) {
$pattern = $rule[Router::RULE_PATTERN];
- if(false !== $pos = strpos($pattern, '@'))
+ if (false !== $pos = strpos($pattern, '@')) {
$pattern = substr($pattern, $pos + 1);
+ }
$pattern = ltrim($pattern, '/');
- if(0 !== preg_match('#^' . $pattern . '$#i', $uri, $muri)) {
-
+ if (0 !== preg_match('#^' . $pattern . '$#i', $uri, $muri)) {
$gotcha = true;
+
break;
}
}
- if(false === $gotcha)
+ if (false === $gotcha) {
throw new Router\Exception\NotFound(
- 'Cannot found an appropriated rule to route %s.', 5, $uri);
+ 'Cannot found an appropriated rule to route %s.',
+ 4,
+ $uri
+ );
+ }
- if(false !== $pos)
+ if (false !== $pos) {
preg_match(
'#^' .
substr($rule[Router::RULE_PATTERN], 0, $pos) .
@@ -370,11 +400,12 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
$subdomain,
$msubdomain
);
- else
+ } else {
$msubdomain = [];
+ }
array_shift($muri);
- $sub = array_shift($msubdomain) ?: null;
+ $sub = array_shift($msubdomain) ?: null;
$rule[Router::RULE_VARIABLES]['_uri'] = $uri;
$rule[Router::RULE_VARIABLES]['_method'] = $method;
$rule[Router::RULE_VARIABLES]['_domain'] = static::getDomain();
@@ -388,19 +419,22 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
$rule[Router::RULE_PATTERN]
);
- foreach(array_merge($muri, $msubdomain) as $key => $value) {
-
- if(!is_string($key))
+ foreach (array_merge($muri, $msubdomain) as $key => $value) {
+ if (!is_string($key)) {
continue;
+ }
- if(true === $caseless)
+ if (true === $caseless) {
$key = mb_strtolower($key);
+ }
- if(isset($rule[Router::RULE_VARIABLES][$key]) && empty($value))
+ if (isset($rule[Router::RULE_VARIABLES][$key]) && empty($value)) {
continue;
+ }
- if(true === $caseless)
+ if (true === $caseless) {
$value = mb_strtolower($value);
+ }
$rule[Router::RULE_VARIABLES][$key] = $value;
}
@@ -420,31 +454,34 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
* * __shift__ to shift a subdomain part, i.e. going to the upper
* domain; if you want to shift x times, just type __shift__ * x.
*
- * @access public
* @param string $id ID.
* @param array $variables Variables.
* @param bool $secure Whether the connection is secured. If
* null, will use the self::isSecure() value.
* @param string $prefix Path prefix. If null, it will be deduced.
* @return string
- * @throw \Hoa\Router\Exception
- */
- public function unroute ( $id, Array $variables = [],
- $secured = null, $prefix = null ) {
-
- if(null === $prefix)
+ * @throws \Hoa\Router\Exception
+ */
+ public function unroute(
+ $id,
+ Array $variables = [],
+ $secured = null,
+ $prefix = null
+ ) {
+ if (null === $prefix) {
$prefix = $this->getPrefix();
+ }
$suffix = $this->getSubdomainSuffix();
$rule = $this->getRule($id);
$pattern = $rule[Router::RULE_PATTERN];
- foreach($variables as $KeY => $value)
- if($KeY != $key = strtolower($KeY)) {
-
+ foreach ($variables as $KeY => $value) {
+ if ($KeY != $key = strtolower($KeY)) {
unset($variables[$KeY]);
$variables[$key] = $value;
}
+ }
$variables = array_merge($rule[Router::RULE_VARIABLES], $variables);
$anchor = !empty($variables['_fragment'])
@@ -453,60 +490,62 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
unset($variables['_fragment']);
$self = $this;
- $prependPrefix = function ( $unroute ) use ( &$prefix ) {
-
- if(0 !== preg_match('#^https?://#', $unroute))
+ $prependPrefix = function ($unroute) use (&$prefix) {
+ if (0 !== preg_match('#^https?://#', $unroute)) {
return $unroute;
+ }
return $prefix . $unroute;
};
- $getPort = function ( $secure ) use ( $self ) {
-
+ $getPort = function ($secure) use ($self) {
$defaultPort = $self->getDefaultPort($secure);
- if(static::UNSECURE === $secure)
+ if (static::UNSECURE === $secure) {
return 80 !== $defaultPort ? ':' . $defaultPort : '';
+ }
return 443 !== $defaultPort ? ':' . $defaultPort : '';
};
- if( true === array_key_exists('_subdomain', $variables)
- && null !== $variables['_subdomain']) {
-
- if(empty($variables['_subdomain']))
+ if (true === array_key_exists('_subdomain', $variables) &&
+ null !== $variables['_subdomain']) {
+ if (empty($variables['_subdomain'])) {
throw new Router\Exception(
'Subdomain is empty, cannot unroute the rule %s properly.',
- 6, $id);
+ 5,
+ $id
+ );
+ }
$secure = null === $secured ? static::isSecure() : $secured;
- if(false !== $pos = strpos($pattern, '@'))
+ if (false !== $pos = strpos($pattern, '@')) {
$pattern = substr($pattern, $pos + 1);
+ }
$subdomain = $variables['_subdomain'];
$handle = strtolower($subdomain);
- switch($handle) {
-
+ switch ($handle) {
case '__self__':
$subdomain = $this->getSubdomain();
- break;
+
+ break;
case '__root__':
$subdomain = '';
- break;
- default:
- if(0 !== preg_match('#__shift__(?:\s*\*\s*(\d+))?#', $handle, $m)) {
+ break;
+ default:
+ if (0 !== preg_match('#__shift__(?:\s*\*\s*(\d+))?#', $handle, $m)) {
$repetition = isset($m[1]) ? (int) $m[1] : 1;
$subdomain = $this->getSubdomain();
- for(; $repetition >= 1; --$repetition) {
-
- if(false === $pos = strpos($subdomain, '.')) {
-
+ for (; $repetition >= 1; --$repetition) {
+ if (false === $pos = strpos($subdomain, '.')) {
$subdomain = '';
+
break;
}
@@ -516,77 +555,90 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
break;
}
- if(null !== $suffix)
+ if (null !== $suffix) {
$subdomain .= '.' . $suffix;
- break;
+ }
+
+ break;
}
- if(!empty($subdomain))
+ if (!empty($subdomain)) {
$subdomain .= '.';
+ }
- return (true === $secure ? 'https://' : 'http://') .
- $subdomain .
- $this->getStrictDomain() .
- $getPort($secure) .
- $prependPrefix($this->_unroute($id, $pattern, $variables)) .
- $anchor;
+ return
+ (true === $secure ? 'https://' : 'http://') .
+ $subdomain .
+ $this->getStrictDomain() .
+ $getPort($secure) .
+ $prependPrefix($this->_unroute($id, $pattern, $variables)) .
+ $anchor;
}
- if(false !== $pos = strpos($pattern, '@')) {
-
+ if (false !== $pos = strpos($pattern, '@')) {
$subPattern = substr($pattern, 0, $pos);
$pattern = substr($pattern, $pos + 1);
- if(null !== $suffix)
+ if (null !== $suffix) {
$subPattern .= '.' . $suffix;
+ }
- if($suffix === $subPattern)
- return $prependPrefix($this->_unroute($id, $pattern, $variables)) .
- $anchor;
+ if ($suffix === $subPattern) {
+ return
+ $prependPrefix($this->_unroute($id, $pattern, $variables)) .
+ $anchor;
+ }
$secure = null === $secured ? static::isSecure() : $secured;
- return (true === $secure ? 'https://' : 'http://') .
- $this->_unroute($id, $subPattern, $variables, false) .
- '.' . $this->getStrictDomain() .
- $getPort($secure) .
- $prependPrefix($this->_unroute($id, $pattern, $variables)) .
- $anchor;
+ return
+ (true === $secure ? 'https://' : 'http://') .
+ $this->_unroute($id, $subPattern, $variables, false) .
+ '.' . $this->getStrictDomain() .
+ $getPort($secure) .
+ $prependPrefix($this->_unroute($id, $pattern, $variables)) .
+ $anchor;
}
- return $prependPrefix($this->_unroute($id, $pattern, $variables)) .
- $anchor;
+ return
+ $prependPrefix($this->_unroute($id, $pattern, $variables)) .
+ $anchor;
}
/**
* Real unroute method.
*
- * @access protected
* @param string $id ID.
* @param string $pattern Pattern.
* @param array $variables Variables.
* @param bool $allowEmpty Whether allow empty variables.
* @return string
- * @throw \Hoa\Router\Exception
- */
- protected function _unroute ( $id, $pattern, Array $variables,
- $allowEmpty = true ) {
-
+ * @throws \Hoa\Router\Exception
+ */
+ protected function _unroute(
+ $id,
+ $pattern,
+ Array $variables,
+ $allowEmpty = true
+ ) {
// (?<named>…)
$out = preg_replace_callback(
'#\(\?\<([^>]+)>[^\)]*\)[\?\*\+]{0,2}#',
- function ( Array $matches ) use ( &$id, &$variables, &$allowEmpty ) {
-
+ function (Array $matches) use (&$id, &$variables, &$allowEmpty) {
$m = strtolower($matches[1]);
- if(!isset($variables[$m]) || '' === $variables[$m])
- if(true === $allowEmpty)
+ if (!isset($variables[$m]) || '' === $variables[$m]) {
+ if (true === $allowEmpty) {
return '';
- else
+ } else {
throw new Router\Exception(
'Variable %s is empty and it is not allowed when ' .
'unrouting rule %s.',
- 7, [$m, $id]);
+ 6,
+ [$m, $id]
+ );
+ }
+ }
return $variables[$m];
},
@@ -622,13 +674,13 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get HTTP method.
*
- * @access public
* @return string
*/
- public function getMethod ( ) {
-
- if('cli' === php_sapi_name())
+ public function getMethod()
+ {
+ if ('cli' === php_sapi_name()) {
return 'get';
+ }
return strtolower($_SERVER['REQUEST_METHOD']);
}
@@ -636,13 +688,13 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Whether the router is called asynchronously or not.
*
- * @access public
* @return bool
*/
- public function isAsynchronous ( ) {
-
- if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
+ public function isAsynchronous()
+ {
+ if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
return false;
+ }
return 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
}
@@ -650,23 +702,24 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get URI.
*
- * @access public
* @return string
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- public static function getURI ( ) {
-
- if('cli' === php_sapi_name())
+ public static function getURI()
+ {
+ if ('cli' === php_sapi_name()) {
return ltrim(@$_SERVER['argv'][1] ?: '', '/');
+ }
- if(!isset($_SERVER['REQUEST_URI']))
- throw new Router\Exception(
- 'Cannot find URI so we cannot route.', 8);
+ if (!isset($_SERVER['REQUEST_URI'])) {
+ throw new Router\Exception('Cannot find URI so we cannot route.', 7);
+ }
$uri = ltrim(urldecode($_SERVER['REQUEST_URI']), '/');
- if(false !== $pos = strpos($uri, '?'))
+ if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
+ }
return $uri;
}
@@ -674,22 +727,23 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get query.
*
- * @access public
* @return array
*/
- public static function getQuery ( ) {
-
- if('cli' === php_sapi_name())
+ public static function getQuery()
+ {
+ if ('cli' === php_sapi_name()) {
return [];
+ }
- if(!isset($_SERVER['REQUEST_URI']))
- throw new Router\Exception(
- 'Cannot find URI so we cannot get query.', 9);
+ if (!isset($_SERVER['REQUEST_URI'])) {
+ throw new Router\Exception('Cannot find URI so we cannot get query.', 8);
+ }
$uri = $_SERVER['REQUEST_URI'];
- if(false === $pos = strpos($uri, '?'))
+ if (false === $pos = strpos($uri, '?')) {
return [];
+ }
parse_str(substr($uri, $pos + 1), $out);
@@ -699,25 +753,26 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get domain (with subdomain if exists).
*
- * @access public
* @return string
*/
- public static function getDomain ( ) {
-
+ public static function getDomain()
+ {
static $domain = null;
- if(null === $domain) {
-
- if('cli' === php_sapi_name())
+ if (null === $domain) {
+ if ('cli' === php_sapi_name()) {
return $domain = '';
+ }
$domain = $_SERVER['SERVER_NAME'];
- if(empty($domain))
+ if (empty($domain)) {
$domain = $_SERVER['SERVER_ADDR'];
+ }
- if(0 !== preg_match('#^(.+):' . static::getPort() . '$#', $domain, $m))
+ if (0 !== preg_match('#^(.+):' . static::getPort() . '$#', $domain, $m)) {
$domain = $m[1];
+ }
}
return $domain;
@@ -726,15 +781,15 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get strict domain (i.e. without subdomain).
*
- * @access public
* @return string
*/
- public function getStrictDomain ( ) {
-
+ public function getStrictDomain()
+ {
$sub = $this->getSubdomain();
- if(empty($sub))
+ if (empty($sub)) {
return static::getDomain();
+ }
return substr(static::getDomain(), strlen($sub) + 1);
}
@@ -742,26 +797,27 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get subdomain.
*
- * @access public
* @param bool $withSuffix With or without suffix.
* @return string
*/
- public function getSubdomain ( $withSuffix = true ) {
-
+ public function getSubdomain($withSuffix = true)
+ {
static $subdomain = null;
- if(null === $subdomain) {
-
+ if (null === $subdomain) {
$domain = static::getDomain();
- if(empty($domain))
+ if (empty($domain)) {
return null;
+ }
- if($domain == long2ip(ip2long($domain)))
+ if ($domain == long2ip(ip2long($domain))) {
return null;
+ }
- if(2 > substr_count($domain, '.', 1))
+ if (2 > substr_count($domain, '.', 1)) {
return null;
+ }
$subdomain = substr(
$domain,
@@ -774,13 +830,15 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
);
}
- if(true === $withSuffix)
+ if (true === $withSuffix) {
return $subdomain;
+ }
$suffix = $this->getSubdomainSuffix();
- if(null === $suffix)
+ if (null === $suffix) {
return $subdomain;
+ }
return substr($subdomain, 0, -strlen($suffix) - 1);
}
@@ -788,12 +846,11 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Set subdomain stack: static or dynamic.
*
- * @access public
* @param int $stack Stack: _static or _dynamic constants.
* @return int
*/
- public function setSubdomainStack ( $stack ) {
-
+ public function setSubdomainStack($stack)
+ {
$old = $this->_subdomainStack;
$this->_subdomainStack = $stack;
@@ -803,23 +860,21 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get subdomain stack.
*
- * @access public
* @return int
*/
- public function getSubdomainStack ( ) {
-
+ public function getSubdomainStack()
+ {
return $this->_subdomainStack;
}
/**
* Set subdomain suffix.
*
- * @access public
* @param string $suffix Suffix.
* @return string
*/
- public function setSubdomainSuffix ( $suffix ) {
-
+ public function setSubdomainSuffix($suffix)
+ {
$old = $this->_subdomainSuffix;
$this->_subdomainSuffix = preg_quote($suffix);
@@ -829,24 +884,23 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get subdomain suffix.
*
- * @access public
* @return string
*/
- public function getSubdomainSuffix ( ) {
-
+ public function getSubdomainSuffix()
+ {
return $this->_subdomainSuffix;
}
/**
* Get port.
*
- * @access public
* @return int
*/
- public static function getPort ( ) {
-
- if('cli' === php_sapi_name())
+ public static function getPort()
+ {
+ if ('cli' === php_sapi_name()) {
return 80;
+ }
return (int) $_SERVER['SERVER_PORT'];
}
@@ -854,15 +908,15 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get bootstrap (script name).
*
- * @access public
* @return string
*/
- public static function getBootstrap ( ) {
-
+ public static function getBootstrap()
+ {
$sapi = php_sapi_name();
- if('cli' === $sapi || 'cli-server' === $sapi)
+ if ('cli' === $sapi || 'cli-server' === $sapi) {
return '';
+ }
return $_SERVER['SCRIPT_NAME'];
}
@@ -870,12 +924,11 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Set path prefix.
*
- * @access public
* @param string $prefix Path prefix.
* @return string
*/
- public function setPrefix ( $prefix ) {
-
+ public function setPrefix($prefix)
+ {
$old = $this->_pathPrefix;
$this->_pathPrefix = preg_quote(rtrim($prefix, '/'));
@@ -885,31 +938,26 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get path prefix (aka “base”).
*
- * @access public
* @return string
*/
- public function getPrefix ( ) {
-
+ public function getPrefix()
+ {
return $this->_pathPrefix;
}
/**
* Set port.
*
- * @access public
* @param int $port Port.
* @param bool $secure Whether the connection is secured.
* @return int
*/
- public function setDefaultPort ( $port, $secure = self::UNSECURE ) {
-
- if(static::UNSECURE === $secure) {
-
+ public function setDefaultPort($port, $secure = self::UNSECURE)
+ {
+ if (static::UNSECURE === $secure) {
$old = $this->_httpPort;
$this->_httpPort = $port;
- }
- else {
-
+ } else {
$old = $this->_httpsPort;
$this->_httpsPort = $port;
}
@@ -920,14 +968,14 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Get HTTP port.
*
- * @access public
* @param bool $secure Whether the connection is secured.
* @return int
*/
- public function getDefaultPort ( $secure = self::UNSECURE ) {
-
- if(static::UNSECURE === $secure)
+ public function getDefaultPort($secure = self::UNSECURE)
+ {
+ if (static::UNSECURE === $secure) {
return $this->_httpPort;
+ }
return $this->_httpsPort;
}
@@ -935,17 +983,18 @@ class Http extends Router\Generic implements Core\Parameter\Parameterizable {
/**
* Whether the connection is secure.
*
- * @access public
* @return bool
*/
- public static function isSecure ( ) {
-
- if(!isset($_SERVER['HTTPS']))
+ public static function isSecure()
+ {
+ if (!isset($_SERVER['HTTPS'])) {
return static::UNSECURE;
+ }
- return (!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS'])
- ? static::SECURE
- : static::UNSECURE;
+ return
+ (!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS'])
+ ? static::SECURE
+ : static::UNSECURE;
}
}