aboutsummaryrefslogtreecommitdiffstats
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
parenta2fba5e02f3f41364c4cd8ae2eb5fadd70d0c482 (diff)
downloadRouter-0a0ca46899138ce3d79bfd9976801bbd5066108a.zip
Router-0a0ca46899138ce3d79bfd9976801bbd5066108a.tar.gz
Router-0a0ca46899138ce3d79bfd9976801bbd5066108a.tar.bz2
Move to PSR-1 and PSR-2.
-rw-r--r--Cli.php167
-rw-r--r--Documentation/Fr/Index.xyl49
-rw-r--r--Exception/Exception.php10
-rw-r--r--Exception/NotFound.php10
-rw-r--r--Generic.php110
-rw-r--r--Http/Dav.php12
-rw-r--r--Http/Http.php543
-rw-r--r--README.md22
-rw-r--r--Router.php55
9 files changed, 518 insertions, 460 deletions
diff --git a/Cli.php b/Cli.php
index 09abb42..278add4 100644
--- a/Cli.php
+++ b/Cli.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:
@@ -43,24 +43,22 @@ use Hoa\Core;
*
* CLI 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 Cli extends Generic implements Core\Parameter\Parameterizable {
-
+class Cli extends Generic implements Core\Parameter\Parameterizable
+{
/**
* Parameters.
*
- * @var \Hoa\Core\Parameter object
+ * @var \Hoa\Core\Parameter
*/
protected $_parameters = null;
/**
* CLI methods that the router understand.
*
- * @var \Hoa\Router\Http array
+ * @var array
*/
protected static $_methods = [
'get'
@@ -71,11 +69,10 @@ class Cli extends 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,
[],
@@ -86,24 +83,24 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
);
$this->_parameters->setParameters($parameters);
- 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
@@ -116,18 +113,16 @@ class Cli extends 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.
@@ -137,28 +132,44 @@ class Cli extends 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
+ * @throws \Hoa\Router\Exception
*/
- protected function _addRule ( $visibility, $id, Array $methods, $pattern,
- $call, $able, Array $variables ) {
-
- if(true === $this->ruleExists($id))
+ protected function _addRule(
+ $visibility,
+ $id,
+ Array $methods,
+ $pattern,
+ $call, $able,
+ Array $variables
+ ) {
+ if (true === $this->ruleExists($id)) {
throw new Exception(
- 'Cannot add rule %s because it already exists.', 0, $id);
+ 'Cannot add rule %s because it already exists.',
+ 0,
+ $id
+ );
+ }
- array_walk($methods, function ( &$method ) {
+ array_walk($methods, function (&$method) {
$method = strtolower($method);
});
$diff = array_diff($methods, self::$_methods);
- if(!empty($diff))
+ if (!empty($diff)) {
throw new Exception(
(1 == count($diff)
? 'Method %s is'
: 'Methods %s are') .
' invalid for the rule %s (valid methods are: %s).',
- 1, [implode(', ', $diff), $id, implode(', ', self::$_methods)]);
+ 1,
+ [
+ implode(', ', $diff),
+ $id,
+ implode(', ', self::$_methods)
+ ]
+ );
+ }
$this->_rules[$id] = [
Router::RULE_VISIBILITY => $visibility,
@@ -177,26 +188,27 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
* Find the appropriated rule.
* Special variables: _call, _able and _tail.
*
- * @access public
* @param string $uri URI. If null, it will be deduce.
* @return \Hoa\Router\Cli
- * @throw \Hoa\Router\Exception\NotFound
+ * @throws \Hoa\Router\Exception\NotFound
*/
- public function route ( $uri = null ) {
-
- if(null === $uri)
+ public function route($uri = null)
+ {
+ if (null === $uri) {
$uri = static::getURI();
+ }
$method = $this->getMethod();
$rules = array_filter(
$this->getRules(),
- function ( $rule ) use ( &$method ) {
-
- if(Router::VISIBILITY_PUBLIC != $rule[Router::RULE_VISIBILITY])
+ function ($rule) use (&$method) {
+ 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;
+ }
return true;
}
@@ -204,20 +216,23 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
$gotcha = false;
- foreach($rules as $rule) {
-
+ foreach ($rules as $rule) {
$pattern = $rule[Router::RULE_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 Exception\NotFound(
- 'Cannot found an appropriated rule to route %s.', 5, $uri);
+ 'Cannot found an appropriated rule to route %s.',
+ 2,
+ $uri
+ );
+ }
array_shift($muri);
$rule[Router::RULE_VARIABLES]['_method'] = 'get';
@@ -229,16 +244,18 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
$rule[Router::RULE_PATTERN]
);
- foreach($muri as $key => $value) {
-
- if(!is_string($key))
+ foreach ($muri 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;
+ }
$rule[Router::RULE_VARIABLES][$key] = $value;
}
@@ -251,24 +268,23 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
/**
* Unroute a rule (i.e. route()^-1).
*
- * @access public
* @param string $id ID.
* @param array $variables Variables.
* @return string
*/
- public function unroute ( $id, Array $variables = [] ) {
-
+ public function unroute($id, Array $variables = [])
+ {
$rule = $this->getRule($id);
$pattern = $rule[Router::RULE_PATTERN];
$variables = array_merge($rule[Router::RULE_VARIABLES], $variables);
$out = preg_replace_callback(
'#\(\?\<([^>]+)>[^\)]*\)[\?\*\+]{0,2}#',
- function ( Array $matches ) use ( &$variables ) {
-
+ function (Array $matches) use (&$variables) {
$m = strtolower($matches[1]);
- if(!isset($variables[$m]))
+ if (!isset($variables[$m])) {
return '';
+ }
return $variables[$m];
},
@@ -291,65 +307,62 @@ class Cli extends Generic implements Core\Parameter\Parameterizable {
/**
* Get HTTP method.
*
- * @access public
* @return string
*/
- public function getMethod ( ) {
-
+ public function getMethod()
+ {
return 'get';
}
/**
* Whether the router is called asynchronously or not.
*
- * @access public
* @return bool
*/
- public function isAsynchronous ( ) {
-
+ public function isAsynchronous()
+ {
return false;
}
/**
* Get URI.
*
- * @access public
* @return string
*/
- public static function getURI ( ) {
-
- if(!isset($_SERVER['argv']))
+ public static function getURI()
+ {
+ if (!isset($_SERVER['argv'])) {
return null;
+ }
$out = null;
$_argv = $_SERVER['argv'];
array_shift($_argv);
- foreach($_argv as $arg) {
-
- if('-' === $arg[0] && false !== strpos($arg, '=')) {
-
- if(false !== strpos($arg, '"'))
+ foreach ($_argv as $arg) {
+ if ('-' === $arg[0] && false !== strpos($arg, '=')) {
+ if (false !== strpos($arg, '"')) {
$arg = str_replace(
'=',
'="',
str_replace('"', '\\"', $arg)
) . '"';
- elseif(false !== strpos($arg, '\''))
+ } elseif (false !== strpos($arg, '\'')) {
$arg = str_replace(
'=',
'=\'',
str_replace('\'', '\\\'', $arg)
) . '\'';
- elseif(false !== strpos($arg, ' '))
+ } elseif (false !== strpos($arg, ' ')) {
$arg = str_replace('=', '="', $arg) . '"';
- }
- elseif(false !== strpos($arg, ' '))
+ }
+ } elseif (false !== strpos($arg, ' ')) {
$arg = '"' . str_replace('"', '\\"', $arg) . '"';
- elseif(false !== strpos($arg, '"'))
+ } elseif (false !== strpos($arg, '"')) {
$arg = '"' . str_replace('"', '\\"', $arg) . '"';
- elseif(false !== strpos($arg, '\''))
+ } elseif (false !== strpos($arg, '\'')) {
$arg = '"' . $arg . '"';
+ }
$out .= ' ' . $arg;
}
diff --git a/Documentation/Fr/Index.xyl b/Documentation/Fr/Index.xyl
index cff13d9..de2a5f6 100644
--- a/Documentation/Fr/Index.xyl
+++ b/Documentation/Fr/Index.xyl
@@ -89,8 +89,8 @@
<p>Prenons un exemple avec le routeur HTTP représenté par la classe
<code>Hoa\Router\Http</code> :</p>
<pre><code class="language-php">$router = new Hoa\Router\Http();
-$router->addRule('h', array('get'), '/hello')
- ->addRule('l', array('get', 'post'), '/login');</code></pre>
+$router->addRule('h', ['get'], '/hello')
+ ->addRule('l', ['get', 'post'], '/login');</code></pre>
<p>Nous avons déclarés deux règles : <code>h</code> et <code>l</code>. La
règle <code>h</code> n'est accessible qu'à travers la méthode HTTP
<code>GET</code> et seule la requête (l'URI) <code>/hello</code> correspond.
@@ -109,7 +109,7 @@ $router->addRule('h', array('get'), '/hello')
<p>Et enfin, pour représenter une règle privée, le nom devra commencer par le
symbole « <code>_</code> ». Ainsi, ces deux déclarations sont strictement
équivalentes :</p>
- <pre><code class="language-php">$router->addPrivateRule('f', array('get', 'post'), '/foobar');
+ <pre><code class="language-php">$router->addPrivateRule('f', ['get', 'post'], '/foobar');
$router->_get_post('f', '/foobar');</code></pre>
<p>Notons que nous pouvons supprimer à tout moment une règle avec la méthode
<code>Hoa\Router\Router::removeRule</code> à laquelle nous passons
@@ -182,11 +182,8 @@ print_r($router->getTheRule());
<p>L'exception <code>Hoa\Router\Exception\NotFound</code> signifie que la
requête ne correspond à aucune règle. Par exemple :</p>
<pre><code class="language-php">try {
-
$router->route('/foobar');
-}
-catch ( Hoa\Router\Exception\NotFound $e ) {
-
+} catch (Hoa\Router\Exception\NotFound $e) {
echo $e->getMessage();
}
@@ -262,7 +259,7 @@ print_r($theRule[$router::RULE_VARIABLES]);
'/hello_(?&amp;lt;who>\w+)(?&amp;lt;format>\.[a-z]+)?',
null,
null,
- array('format' => '.txt')
+ ['format' => '.txt']
);
$router->route('/hello_gordon');
@@ -331,7 +328,7 @@ var_dump($theRule[$router::RULE_VARIABLES]['who']);
la règle <code>h</code> avec comme valeur <code>alyx</code> pour
<code>who</code> et rien pour le format (la valeur par défaut sera utilisée).
Alors, nous écrirons :</p>
- <pre><code class="language-php">var_dump($router->unroute('h', array('who' => 'alyx')));
+ <pre><code class="language-php">var_dump($router->unroute('h', ['who' => 'alyx']));
/**
* Will output:
@@ -342,7 +339,7 @@ var_dump($theRule[$router::RULE_VARIABLES]['who']);
syntaxe finale peut changer à tout moment sans casser l'application. Par
exemple, changeons la règle <code>h</code> pour :</p>
<pre><code class="language-php">$router->get('h', '/users/(?&amp;lt;who>\w+)/hello(?&amp;lt;format>\.[a-z]+)?');
-var_dump($router->unroute('h', array('who' => 'alyx')));
+var_dump($router->unroute('h', ['who' => 'alyx']));
/**
* Will output:
@@ -538,10 +535,10 @@ print_r($router->getTheRule());
<pre><code class="language-php">var_dump(
$router->unroute(
'p',
- array(
+ [
'user' => 'gordon',
'project' => 'Space-biker'
- )
+ ]
)
);
@@ -557,10 +554,10 @@ print_r($router->getTheRule());
<pre><code class="language-php">var_dump(
$router->unroute(
'p',
- array(
+ [
'project' => 'Space-biker',
- '_subdomain' => 'my-subdomain')
- )
+ '_subdomain' => 'my-subdomain'
+ ]
)
);
@@ -587,22 +584,22 @@ print_r($router->getTheRule());
<pre><code class="language-php">$router->get('s', '(?&amp;lt;three>[^\.]+)\.(?&amp;lt;two>[^\.]+)\.(?&amp;lt;one>.+)@');
var_dump(
// Normal.
- $router->unroute('s', array('three' => 'foo', 'two' => 'bar', 'one' => 'baz')),
+ $router->unroute('s', ['three' => 'foo', 'two' => 'bar', 'one' => 'baz']),
// Force.
- $router->unroute('s', array('_subdomain' => 'my-subdomain')),
+ $router->unroute('s', ['_subdomain' => 'my-subdomain']),
// Current subdomain.
- $router->unroute('s', array('_subdomain' => '__self__')),
+ $router->unroute('s', ['_subdomain' => '__self__']),
// No subdomain.
- $router->unroute('s', array('_subdomain' => '__root__')),
+ $router->unroute('s', ['_subdomain' => '__root__']),
// Shift only sub3.
- $router->unroute('s', array('_subdomain' => '__shift__')),
+ $router->unroute('s', ['_subdomain' => '__shift__']),
// Shift two sub-domains.
- $router->unroute('s', array('_subdomain' => '__shift__ * 2'))
+ $router->unroute('s', ['_subdomain' => '__shift__ * 2'])
);
/**
@@ -635,11 +632,11 @@ var_dump(
<pre><code class="language-php">var_dump(
$router->unroute(
'p',
- array(
+ [
'user' => 'gordon',
'project' => 'Space-biker',
'_fragment' => 'Introduction'
- )
+ ]
)
);
@@ -683,7 +680,7 @@ var_dump(
<pre><code class="language-php">var_dump(
$router->unroute(
'p',
- array('user' => 'gordon', 'project' => 'Space-biker'),
+ ['user' => 'gordon', 'project' => 'Space-biker'],
true
)
);
@@ -763,10 +760,10 @@ Array
'(?&amp;lt;group>\w+)?(:(?&amp;lt;subcommand>\w+))?(?&amp;lt;_tail>.*?)',
null,
null,
- array(
+ [
'group' => 'default',
'subcommand' => 'default'
- )
+ ]
);</code></pre>
<p>Ainsi, nous pourrions avoir <code><em>group</em></code>,
<code><em>group:subcommand</em></code> ou <code><em>:subcommand</em></code>.
diff --git a/Exception/Exception.php b/Exception/Exception.php
index 4f33147..48e5da3 100644
--- a/Exception/Exception.php
+++ b/Exception/Exception.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:
@@ -43,12 +43,12 @@ use Hoa\Core;
*
* Extending the \Hoa\Core\Exception class.
*
- * @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 Exception extends Core\Exception { }
+class Exception extends Core\Exception
+{
+}
/**
* Flex entity.
diff --git a/Exception/NotFound.php b/Exception/NotFound.php
index e19a0f9..bf140d7 100644
--- a/Exception/NotFound.php
+++ b/Exception/NotFound.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,9 +41,9 @@ namespace Hoa\Router\Exception;
*
* Extending the \Hoa\Router\Exception class.
*
- * @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 NotFound extends Exception { }
+class NotFound extends Exception
+{
+}
diff --git a/Generic.php b/Generic.php
index 29e6b3c..c6c5cf3 100644
--- a/Generic.php
+++ b/Generic.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,31 +41,29 @@ namespace Hoa\Router;
*
* Generic 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
*/
-
-abstract class Generic implements Router {
-
+abstract class Generic implements Router
+{
/**
* All rules buckets.
*
- * @var \Hoa\Router\Generic array
+ * @var array
*/
protected $_rules = [];
/**
* The routed rule.
*
- * @var \Hoa\Router\Generic array
+ * @var array
*/
protected $_rule = null;
/**
* Methods that the router understand.
*
- * @var \Hoa\Router\Generic array
+ * @var array
*/
protected static $_methods = [];
@@ -74,7 +72,6 @@ abstract class Generic implements Router {
/**
* Fallback for add*Rule() methods.
*
- * @access public
* @param int $visibility Visibility (please, see
* Router::VISIBILITY_* constants).
* @param string $id ID.
@@ -84,16 +81,21 @@ abstract class Generic implements Router {
* @param mixed $able Able (second part).
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router\Generic
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- abstract protected function _addRule ( $visibility, $id, Array $methods,
- $pattern, $call, $able,
- Array $variables );
+ abstract protected function _addRule(
+ $visibility,
+ $id,
+ Array $methods,
+ $pattern,
+ $call,
+ $able,
+ Array $variables
+ );
/**
* Add a public rule.
*
- * @access public
* @param string $id ID.
* @param array $methods HTTP methods allowed by the rule.
* @param string $pattern Pattern (on-subdomain@on-request).
@@ -101,11 +103,16 @@ abstract class Generic implements Router {
* @param mixed $able Able (second part).
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router\Generic
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- public function addRule ( $id, Array $methods, $pattern, $call = null,
- $able = null, Array $variables = [] ) {
-
+ public function addRule(
+ $id,
+ Array $methods,
+ $pattern,
+ $call = null,
+ $able = null,
+ Array $variables = []
+ ) {
return $this->_addRule(
Router::VISIBILITY_PUBLIC,
$id,
@@ -120,7 +127,6 @@ abstract class Generic implements Router {
/**
* Add a private rule.
*
- * @access public
* @param string $id ID.
* @param array $methods HTTP methods allowed by the rule.
* @param string $pattern Pattern (on-subdomain@on-request).
@@ -128,11 +134,15 @@ abstract class Generic implements Router {
* @param mixed $able Able (second part).
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router\Generic
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- public function addPrivateRule ( $id, Array $methods, $pattern, $call = null,
- $able = null, Array $variables = [] ) {
-
+ public function addPrivateRule(
+ $id,
+ Array $methods,
+ $pattern,$call = null,
+ $able = null,
+ Array $variables = []
+ ) {
return $this->_addRule(
Router::VISIBILITY_PRIVATE,
$id,
@@ -156,26 +166,25 @@ abstract class Generic implements Router {
* any(…) : addRule(…, array(<all methods>), …);
* head_delete(…): addRule(…, array('head', 'delete'), …).
*
- * @access public
* @param string $name Please, see API documentation.
* @param array $arguments Arguments for add*Rule() methods.
* @return \Hoa\Router\Generic
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- public function __call ( $name, $arguments ) {
-
- if('_' == $name[0]) {
-
+ public function __call($name, $arguments)
+ {
+ if ('_' === $name[0]) {
$name = substr($name, 1);
$method = 'addPrivateRule';
- }
- else
+ } else {
$method = 'addRule';
+ }
- if('any' == $name)
+ if ('any' === $name) {
array_unshift($arguments, static::$_methods);
- else
+ } else {
array_unshift($arguments, explode('_', $name));
+ }
$handle = $arguments[0];
$arguments[0] = $arguments[1];
@@ -187,12 +196,11 @@ abstract class Generic implements Router {
/**
* Remove a rule.
*
- * @access public
* @param string $id ID.
* @return void
*/
- public function removeRule ( $id ) {
-
+ public function removeRule($id)
+ {
unset($this->_rules[$id]);
return;
@@ -201,39 +209,36 @@ abstract class Generic implements Router {
/**
* Check whether a rule exists.
*
- * @access public
* @param string $id ID.
* @return bool
*/
- public function ruleExists ( $id ) {
-
+ public function ruleExists($id)
+ {
return isset($this->_rules[$id]);
}
/**
* Get all rules.
*
- * @access public
* @return array
*/
- public function getRules ( ) {
-
+ public function getRules()
+ {
return $this->_rules;
}
/**
* Get a specific rule.
*
- * @access public
* @param string $id ID.
* @return array
- * @throw \Hoa\Router\Exception
+ * @throws \Hoa\Router\Exception
*/
- public function getRule ( $id ) {
-
- if(false === $this->ruleExists($id))
- throw new Exception(
- 'Rule %s does not exist.', 2, $id);
+ public function getRule($id)
+ {
+ if (false === $this->ruleExists($id)) {
+ throw new Exception('Rule %s does not exist.', 0, $id);
+ }
return $this->_rules[$id];
}
@@ -241,11 +246,10 @@ abstract class Generic implements Router {
/**
* Get the selected rule after routing.
*
- * @access public
* @return array
*/
- public function &getTheRule ( ) {
-
+ public function &getTheRule()
+ {
return $this->_rule;
}
}
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;
}
}
diff --git a/README.md b/README.md
index b6683c2..87854c0 100644
--- a/README.md
+++ b/README.md
@@ -47,20 +47,16 @@ There are different ways to declare routes but the more usual is as follows:
```php
$router = new Hoa\Router\Http();
$router
- ->get('u', '/hello', function ( ) {
-
+ ->get('u', '/hello', function () {
echo 'world!', "\n";
})
- ->post('v', '/hello', function ( Array $_request ) {
-
+ ->post('v', '/hello', function (Array $_request) {
echo $_request['a'] + $_request['b'], "\n";
})
- ->get('w', '/bye', function ( ) {
-
+ ->get('w', '/bye', function () {
echo 'ohh :-(', "\n";
})
- ->get('x', '/hello_(?<nick>\w+)', function ( $nick ) {
-
+ ->get('x', '/hello_(?<nick>\w+)', function ($nick) {
echo 'Welcome ', ucfirst($nick), '!', "\n";
});
```
@@ -111,11 +107,11 @@ $router = new Hoa\Router\Cli();
$router->get(
'g',
'(?<group>\w+):(?<subcommand>\w+)(?<options>.*?)'
- function ( $group, $subcommand, $options ) {
-
- echo 'Group : ', $group, "\n",
- 'Subcommand: ', $subcommand, "\n",
- 'Options : ', trim($options), "\n";
+ function ($group, $subcommand, $options) {
+ echo
+ 'Group : ', $group, "\n",
+ 'Subcommand: ', $subcommand, "\n",
+ 'Options : ', trim($options), "\n";
}
);
```
diff --git a/Router.php b/Router.php
index d92cedd..e2b80ac 100644
--- a/Router.php
+++ b/Router.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:
@@ -43,13 +43,11 @@ use Hoa\Core;
*
* Router interface.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-interface Router {
-
+interface Router
+{
/**
* Rule visibility: public.
*
@@ -116,7 +114,6 @@ interface Router {
/**
* Add a public rule.
*
- * @access public
* @param string $id ID.
* @param array $methods Methods.
* @param string $pattern Pattern.
@@ -125,13 +122,18 @@ interface Router {
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router
*/
- public function addRule ( $id, Array $methods, $pattern, $call = null,
- $able = null, Array $variables = [] );
+ public function addRule(
+ $id,
+ Array $methods,
+ $pattern,
+ $call = null,
+ $able = null,
+ Array $variables = []
+ );
/**
* Add a private rule.
*
- * @access public
* @param string $id ID.
* @param array $methods Methods.
* @param string $pattern Pattern.
@@ -140,69 +142,68 @@ interface Router {
* @param array $variables Variables (default or additional values).
* @return \Hoa\Router
*/
- public function addPrivateRule ( $id, Array $methods, $pattern, $call = null,
- $able = null, Array $variables = [] );
+ public function addPrivateRule(
+ $id,
+ Array $methods,
+ $pattern,
+ $call = null,
+ $able = null,
+ Array $variables = []
+ );
/**
* Remove a rule.
*
- * @access public
* @param string $id ID.
* @return void
*/
- public function removeRule ( $id );
+ public function removeRule($id);
/**
* Check whether a rule exists.
*
- * @access public
* @param string $id ID.
* @return bool
*/
- public function ruleExists ( $id );
+ public function ruleExists($id);
/**
* Get the selected rule after routing.
*
- * @access public
* @return mixed
*/
- public function &getTheRule ( );
+ public function &getTheRule();
/**
* Find the appropriated rule.
*
- * @access public
* @return \Hoa\Router
- * @throw \Hoa\Router\Exception\NotFound
+ * @throws \Hoa\Router\Exception\NotFound
*/
- public function route ( );
+ public function route();
/**
* Unroute a rule (i.e. route()^-1).
*
- * @access public
* @param string $id ID.
* @param array $variables Variables.
* @return string
*/
- public function unroute ( $id, Array $variables = [] );
+ public function unroute($id, Array $variables = []);
/**
* Get method or mode where the router is called.
*
- * @access public
* @return string
*/
- public function getMethod ( );
+ public function getMethod();
/**
* Whether the router is called asynchronously or not.
*
- * @access public
* @return bool
*/
- public function isAsynchronous ( );
+ public function isAsynchronous();
}
/**