mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-07 10:41:40 +09:00
Add Request class and make Router::parseUrl() return an instance of it
This commit is contained in:
parent
986fc23043
commit
a196706d7f
5 changed files with 242 additions and 57 deletions
187
common/framework/Request.php
Normal file
187
common/framework/Request.php
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The request class.
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
/**
|
||||
* The request method.
|
||||
*/
|
||||
public $method = '';
|
||||
|
||||
/**
|
||||
* The request URL, not including RX_BASEURL.
|
||||
*/
|
||||
public $url = '';
|
||||
|
||||
/**
|
||||
* The requested host name and current domain information.
|
||||
*/
|
||||
public $hostname = '';
|
||||
public $domain;
|
||||
|
||||
/**
|
||||
* The protocol used.
|
||||
*/
|
||||
public $protocol = 'http';
|
||||
|
||||
/**
|
||||
* The callback function for JSONP requests, also known as "JS callback" in XE.
|
||||
*/
|
||||
public $callback_function = '';
|
||||
|
||||
/**
|
||||
* Routing information, request arguments, and options.
|
||||
*/
|
||||
protected $_route_status = 200;
|
||||
protected $_route_options;
|
||||
public $module = '';
|
||||
public $mid = '';
|
||||
public $act = '';
|
||||
public $args = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param string $hostname
|
||||
* @param string $protocol
|
||||
*/
|
||||
public function __construct(string $method = '', string $url = '', string $hostname = '', string $protocol = '')
|
||||
{
|
||||
// Set instance properties.
|
||||
$this->method = $method ?: (\PHP_SAPI === 'cli' ? 'CLI' : \Context::getRequestMethod());
|
||||
$this->url = $url ?: \RX_REQUEST_URL;
|
||||
$this->hostname = $hostname ?: $_SERVER['HTTP_HOST'];
|
||||
$this->protocol = \RX_SSL ? 'https' : 'http';
|
||||
$this->callback_function = \Context::get('js_callback_func') ?: '';
|
||||
|
||||
// Initialize the arguments object.
|
||||
$this->args = [];
|
||||
|
||||
// Initialize route options.
|
||||
$this->_route_options = new \stdClass;
|
||||
$this->_route_options->cache_control = true;
|
||||
$this->_route_options->check_csrf = true;
|
||||
$this->_route_options->is_forwarded = false;
|
||||
$this->_route_options->is_indexable = true;
|
||||
$this->_route_options->enable_session = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a request argument, optionally coerced into a type.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $type
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, string $type = '')
|
||||
{
|
||||
$value = $this->args[$name] ?? null;
|
||||
switch ($type)
|
||||
{
|
||||
case 'int': $value = (int)$value; break;
|
||||
case 'float': $value = (float)$value; break;
|
||||
case 'bool': $value = tobool($value); break;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all request arguments.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getAll(): object
|
||||
{
|
||||
return (object)($this->args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the complete URL of this request.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullUrl(): string
|
||||
{
|
||||
return sprintf('%s://%s%s%s', $this->protocol, $this->hostname, \RX_BASEURL, $this->url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the HTTP method of this request.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod(): string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JS callback function.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCallbackFunction(): string
|
||||
{
|
||||
return $this->callback_function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get route status.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRouteStatus(): int
|
||||
{
|
||||
return $this->_route_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set route status.
|
||||
*
|
||||
* @param int $status
|
||||
* @return void
|
||||
*/
|
||||
public function setRouteStatus(int $status): void
|
||||
{
|
||||
$this->_route_status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get route options.
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRouteOption(string $name)
|
||||
{
|
||||
return $this->_route_options->{$name} ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all route options.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function getRouteOptions(): object
|
||||
{
|
||||
return $this->_route_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set route option.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function setRouteOption(string $name, $value): void
|
||||
{
|
||||
$this->_route_options->{$name} = $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -90,21 +90,6 @@ class Router
|
|||
protected static $_internal_forwarded_cache = array();
|
||||
protected static $_route_cache = array();
|
||||
|
||||
/**
|
||||
* Instance properties.
|
||||
*/
|
||||
public $status = 200;
|
||||
public $url = '';
|
||||
public $module = '';
|
||||
public $mid = '';
|
||||
public $act = '';
|
||||
public $args = [];
|
||||
public $is_forwarded = false;
|
||||
public $check_csrf = true;
|
||||
public $meta_noindex = false;
|
||||
public $session = true;
|
||||
public $cache_control = true;
|
||||
|
||||
/**
|
||||
* Return the currently configured rewrite level.
|
||||
*
|
||||
|
|
@ -130,12 +115,12 @@ class Router
|
|||
* @param string $method
|
||||
* @param string $url
|
||||
* @param int $rewrite_level
|
||||
* @return self
|
||||
* @return Request
|
||||
*/
|
||||
public static function parseURL(string $method, string $url, int $rewrite_level): self
|
||||
public static function parseURL(string $method, string $url, int $rewrite_level): Request
|
||||
{
|
||||
// Prepare the return object.
|
||||
$result = new self;
|
||||
$result = new Request;
|
||||
|
||||
// Separate additional arguments from the URL.
|
||||
$args = array();
|
||||
|
|
@ -155,7 +140,7 @@ class Router
|
|||
}
|
||||
if (function_exists('mb_check_encoding') && !mb_check_encoding($url, 'UTF-8'))
|
||||
{
|
||||
$result->status = 404;
|
||||
$result->setRouteStatus(404);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
|
@ -223,7 +208,7 @@ class Router
|
|||
$result->module = $action[0];
|
||||
$result->mid = $prefix;
|
||||
$result->act = $action[1];
|
||||
$result->is_forwarded = true;
|
||||
$result->setRouteOption('is_forwarded', true);
|
||||
$result->args = $allargs;
|
||||
self::_fillActionProperties($result);
|
||||
return $result;
|
||||
|
|
@ -238,7 +223,7 @@ class Router
|
|||
$result->module = $module_name;
|
||||
$result->mid = $prefix_type === 'mid' ? $prefix : '';
|
||||
$result->act = $internal_url;
|
||||
$result->is_forwarded = true;
|
||||
$result->setRouteOption('is_forwarded', true);
|
||||
$result->args = $allargs;
|
||||
self::_fillActionProperties($result);
|
||||
return $result;
|
||||
|
|
@ -251,7 +236,7 @@ class Router
|
|||
$result->module = $module_name;
|
||||
$result->mid = $prefix_type === 'mid' ? $prefix : '';
|
||||
$result->act = $action_info->error_handlers[404];
|
||||
$result->is_forwarded = false;
|
||||
$result->setRouteOption('is_forwarded', false);
|
||||
$result->args = $allargs;
|
||||
self::_fillActionProperties($result);
|
||||
return $result;
|
||||
|
|
@ -271,7 +256,7 @@ class Router
|
|||
$allargs = array_merge($args, $matches, ['act' => $action[1]]);
|
||||
$result->module = $action[0];
|
||||
$result->act = $action[1];
|
||||
$result->is_forwarded = true;
|
||||
$result->setRouteOption('is_forwarded', true);
|
||||
$result->args = $allargs;
|
||||
self::_fillActionProperties($result);
|
||||
return $result;
|
||||
|
|
@ -289,7 +274,7 @@ class Router
|
|||
$result->module = $allargs['module'] ?? '';
|
||||
$result->mid = ($allargs['mid'] ?? '') ?: '';
|
||||
$result->act = ($allargs['act'] ?? '') ?: '';
|
||||
$result->is_forwarded = false;
|
||||
$result->setRouteOption('is_forwarded', false);
|
||||
$result->args = $allargs;
|
||||
self::_fillActionProperties($result);
|
||||
return $result;
|
||||
|
|
@ -308,7 +293,7 @@ class Router
|
|||
}
|
||||
else
|
||||
{
|
||||
$result->status = 404;
|
||||
$result->setRouteStatus(404);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -496,20 +481,20 @@ class Router
|
|||
/**
|
||||
* Fill additional properties of an action.
|
||||
*
|
||||
* @param object $route
|
||||
* @param Request $request
|
||||
* @param ?object $action
|
||||
* @return void
|
||||
*/
|
||||
protected static function _fillActionProperties(object $route, ?object $action = null): void
|
||||
protected static function _fillActionProperties(Request $request, ?object $action = null): void
|
||||
{
|
||||
if (!$action)
|
||||
{
|
||||
if ($route->module && $route->act)
|
||||
if ($request->module && $request->act)
|
||||
{
|
||||
$action_info = \ModuleModel::getModuleActionXml($route->module);
|
||||
if (isset($action_info->action->{$route->act}))
|
||||
$action_info = \ModuleModel::getModuleActionXml($request->module);
|
||||
if (isset($action_info->action->{$request->act}))
|
||||
{
|
||||
$action = $action_info->action->{$route->act};
|
||||
$action = $action_info->action->{$request->act};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -518,10 +503,10 @@ class Router
|
|||
return;
|
||||
}
|
||||
|
||||
$route->check_csrf = ($action->check_csrf === 'false') ? false : true;
|
||||
$route->meta_noindex = ($action->meta_noindex === 'true') ? true : false;
|
||||
$route->session = ($action->session === 'false') ? false : true;
|
||||
$route->cache_control = ($action->cache_control === 'false') ? false : true;
|
||||
$request->setRouteOption('check_csrf', ($action->check_csrf === 'false') ? false : true);
|
||||
$request->setRouteOption('is_indexable', ($action->meta_noindex === 'true') ? false : true);
|
||||
$request->setRouteOption('enable_session', ($action->session === 'false') ? false : true);
|
||||
$request->setRouteOption('cache_control', ($action->cache_control === 'false') ? false : true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue