Url: add optional additional info to Url handler
This commit is contained in:
parent
73dc860972
commit
d304d4741f
3 changed files with 66 additions and 22 deletions
|
@ -613,6 +613,7 @@ class Tpl {
|
|||
Url :: add_url_handler(
|
||||
$pattern,
|
||||
array('EesyPHP\\Tpl', 'handle_static_file'),
|
||||
null, // additionnal info
|
||||
false, // authenticated
|
||||
false, // override
|
||||
false, // API mode
|
||||
|
|
22
src/Url.php
22
src/Url.php
|
@ -14,12 +14,14 @@ class Url {
|
|||
* array (
|
||||
* '|get/(?P<name>[a-zA-Z0-9]+)$|' => array (
|
||||
* 'handler' => 'get',
|
||||
* 'additional_info' => array(),
|
||||
* 'authenticated' => true,
|
||||
* 'api_mode' => false,
|
||||
* 'methods' => array('GET'),
|
||||
* ),
|
||||
* '|get/all$|' => => array (
|
||||
* 'handler' => 'get_all',
|
||||
* 'additional_info' => array(),
|
||||
* 'authenticated' => true,
|
||||
* 'api_mode' => false,
|
||||
* 'methods' => array('GET', 'POST'),
|
||||
|
@ -72,6 +74,7 @@ class Url {
|
|||
*
|
||||
* @param string|array $pattern The URL pattern or an array of patterns (required)
|
||||
* @param callable $handler The URL pattern handler (must be callable, required)
|
||||
* @param array|null $additional_info Array of information to pass to the URL handler
|
||||
* @param boolean $authenticated Permit to define if this URL is accessible only for
|
||||
* authenticated users (optional, default: true if the
|
||||
* special force_authentication function is defined,
|
||||
|
@ -81,8 +84,9 @@ class Url {
|
|||
* @param boolean $api_mode Enable API mode (optional, default: false)
|
||||
* @param array|string|null $methods HTTP method (optional, default: array('GET', 'POST'))
|
||||
**/
|
||||
public static function add_url_handler($pattern, $handler=null, $authenticated=null, $override=true,
|
||||
$api_mode=false, $methods=null) {
|
||||
public static function add_url_handler($pattern, $handler=null, $additional_info=null,
|
||||
$authenticated=null, $override=true, $api_mode=false,
|
||||
$methods=null) {
|
||||
$authenticated = (
|
||||
is_null($authenticated)?
|
||||
function_exists('force_authentication'):
|
||||
|
@ -95,15 +99,21 @@ class Url {
|
|||
if (is_array($pattern)) {
|
||||
if (is_null($handler))
|
||||
foreach($pattern as $p => $h)
|
||||
self :: add_url_handler($p, $h, $authenticated, $override, $api_mode, $methods);
|
||||
self :: add_url_handler(
|
||||
$p, $h, $additional_info, $authenticated, $override, $api_mode, $methods);
|
||||
else
|
||||
foreach($pattern as $p)
|
||||
self :: add_url_handler($p, $handler, $authenticated, $override, $api_mode, $methods);
|
||||
self :: add_url_handler(
|
||||
$p, $handler, $additional_info, $authenticated, $override, $api_mode, $methods);
|
||||
}
|
||||
else {
|
||||
if (!isset(self :: $patterns[$pattern])) {
|
||||
self :: $patterns[$pattern] = array(
|
||||
'handler' => $handler,
|
||||
'additional_info' => (
|
||||
is_array($additional_info)?
|
||||
$additional_info:array()
|
||||
),
|
||||
'authenticated' => $authenticated,
|
||||
'api_mode' => $api_mode,
|
||||
'methods' => $methods,
|
||||
|
@ -116,6 +126,10 @@ class Url {
|
|||
);
|
||||
self :: $patterns[$pattern] = array(
|
||||
'handler' => $handler,
|
||||
'additional_info' => (
|
||||
is_array($additional_info)?
|
||||
$additional_info:array()
|
||||
),
|
||||
'authenticated' => $authenticated,
|
||||
'api_mode' => $api_mode,
|
||||
'methods' => $methods,
|
||||
|
|
|
@ -13,31 +13,56 @@ namespace EesyPHP;
|
|||
*/
|
||||
class UrlRequest {
|
||||
|
||||
// The URL requested handler
|
||||
private $current_url = null;
|
||||
/**
|
||||
* The URL requested handler
|
||||
* @var string
|
||||
*/
|
||||
private $current_url;
|
||||
|
||||
// The URL requested handler
|
||||
private $handler = null;
|
||||
/**
|
||||
* The URL requested handler
|
||||
* @var callable
|
||||
*/
|
||||
private $handler;
|
||||
|
||||
// Request need authentication ?
|
||||
private $authenticated = true;
|
||||
/**
|
||||
* Request need authentication ?
|
||||
* @var bool|null
|
||||
*/
|
||||
private $authenticated = null;
|
||||
|
||||
// API mode enabled ?
|
||||
private $api_mode = false;
|
||||
/**
|
||||
* API mode enabled ?
|
||||
* @var bool
|
||||
*/
|
||||
private $api_mode;
|
||||
|
||||
// Parameters detected on requested URL
|
||||
/**
|
||||
* Parameters detected on requested URL
|
||||
* @var array<string,mixed>
|
||||
*/
|
||||
private $url_params = array();
|
||||
|
||||
public function __construct($current_url, $handler_infos, $url_params=array()) {
|
||||
/**
|
||||
* Additional info pass when the URL handler was registered
|
||||
* @var array<string,mixed>
|
||||
*/
|
||||
private $additional_info = array();
|
||||
|
||||
public function __construct($current_url, $handler_info, $url_params=array()) {
|
||||
$this -> current_url = $current_url;
|
||||
$this -> handler = $handler_infos['handler'];
|
||||
$this -> handler = $handler_info['handler'];
|
||||
$this -> authenticated = (
|
||||
isset($handler_infos['authenticated'])?
|
||||
boolval($handler_infos['authenticated']):true);
|
||||
isset($handler_info['authenticated'])?
|
||||
boolval($handler_info['authenticated']):true);
|
||||
$this -> api_mode = (
|
||||
isset($handler_infos['api_mode'])?
|
||||
boolval($handler_infos['api_mode']):false);
|
||||
isset($handler_info['api_mode'])?
|
||||
boolval($handler_info['api_mode']):false);
|
||||
$this -> url_params = $url_params;
|
||||
$this -> additional_info = (
|
||||
isset($handler_info['additional_info']) && is_array($handler_info['additional_info'])?
|
||||
$handler_info['additional_info']:array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,9 +85,10 @@ class UrlRequest {
|
|||
return $this -> get_referer();
|
||||
if ($key == 'http_method')
|
||||
return $_SERVER['REQUEST_METHOD'];
|
||||
if (array_key_exists($key, $this->url_params)) {
|
||||
if (array_key_exists($key, $this->url_params))
|
||||
return urldecode($this->url_params[$key]);
|
||||
}
|
||||
if (array_key_exists($key, $this->additional_info))
|
||||
return urldecode($this->additional_info[$key]);
|
||||
// Unknown key, log warning
|
||||
Log :: warning(
|
||||
"__get($key): invalid property requested\n%s",
|
||||
|
@ -103,7 +129,10 @@ class UrlRequest {
|
|||
)
|
||||
)
|
||||
return True;
|
||||
return array_key_exists($key, $this->url_params);
|
||||
return (
|
||||
array_key_exists($key, $this->url_params)
|
||||
|| array_key_exists($key, $this->additional_info)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue