2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
2015-08-29 03:34:34 +02:00
|
|
|
namespace Router;
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Load controllers: bootstrap('controllers', 'controller1', 'controller2')
|
|
|
|
function bootstrap()
|
|
|
|
{
|
|
|
|
$files = \func_get_args();
|
|
|
|
$base_path = array_shift($files);
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
require $base_path.'/'.$file.'.php';
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Execute a callback before each action
|
2013-02-18 03:48:21 +01:00
|
|
|
function before($value = null)
|
|
|
|
{
|
|
|
|
static $before_callback = null;
|
|
|
|
|
|
|
|
if (is_callable($value)) {
|
|
|
|
$before_callback = $value;
|
|
|
|
}
|
|
|
|
else if (is_callable($before_callback)) {
|
|
|
|
$before_callback($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Execute a callback before a specific action
|
|
|
|
function before_action($name, $value = null)
|
|
|
|
{
|
|
|
|
static $callbacks = array();
|
2013-02-18 03:48:21 +01:00
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
if (is_callable($value)) {
|
|
|
|
$callbacks[$name] = $value;
|
|
|
|
}
|
|
|
|
else if (isset($callbacks[$name]) && is_callable($callbacks[$name])) {
|
|
|
|
$callbacks[$name]($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute an action
|
2014-02-09 21:13:36 +01:00
|
|
|
function action($name, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
$handler = isset($_GET['action']) ? $_GET['action'] : 'default';
|
|
|
|
|
|
|
|
if ($handler === $name) {
|
2014-02-08 20:13:14 +01:00
|
|
|
before($name);
|
|
|
|
before_action($name);
|
2013-02-18 03:48:21 +01:00
|
|
|
$callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Execute an action only for POST requests
|
2014-02-09 21:13:36 +01:00
|
|
|
function post_action($name, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
action($name, $callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Execute an action only for GET requests
|
2014-02-09 21:13:36 +01:00
|
|
|
function get_action($name, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
|
action($name, $callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Run when no action have been executed before
|
2014-02-09 21:13:36 +01:00
|
|
|
function notfound(\Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2014-02-08 20:13:14 +01:00
|
|
|
before('notfound');
|
|
|
|
before_action('notfound');
|
2013-02-18 03:48:21 +01:00
|
|
|
$callback();
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Match a request like this one: GET /myhandler
|
2014-02-09 21:13:36 +01:00
|
|
|
function get($url, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
find_route('GET', $url, $callback);
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Match a request like this one: POST /myhandler
|
2014-02-09 21:13:36 +01:00
|
|
|
function post($url, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
find_route('POST', $url, $callback);
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Match a request like this one: PUT /myhandler
|
2014-02-09 21:13:36 +01:00
|
|
|
function put($url, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
find_route('PUT', $url, $callback);
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Match a request like this one: DELETE /myhandler
|
2014-02-09 21:13:36 +01:00
|
|
|
function delete($url, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
find_route('DELETE', $url, $callback);
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Define which callback to execute according to the URL and the HTTP verb
|
2014-02-09 21:13:36 +01:00
|
|
|
function find_route($method, $route, \Closure $callback)
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === $method) {
|
|
|
|
|
2014-03-17 02:56:43 +01:00
|
|
|
if (! empty($_SERVER['QUERY_STRING'])) {
|
2013-02-18 03:48:21 +01:00
|
|
|
$url = substr($_SERVER['REQUEST_URI'], 0, -(strlen($_SERVER['QUERY_STRING']) + 1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$url = $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
if (url_match($route, $url, $params)) {
|
|
|
|
|
2014-03-17 02:56:43 +01:00
|
|
|
before($route);
|
2013-02-18 03:48:21 +01:00
|
|
|
\call_user_func_array($callback, $params);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
// Parse url and find matches
|
2013-02-18 03:48:21 +01:00
|
|
|
function url_match($route_uri, $request_uri, array &$params)
|
|
|
|
{
|
|
|
|
if ($request_uri === $route_uri) return true;
|
|
|
|
if ($route_uri === '/' || $request_uri === '/') return false;
|
|
|
|
|
|
|
|
$route_uri = trim($route_uri, '/');
|
|
|
|
$request_uri = trim($request_uri, '/');
|
|
|
|
|
|
|
|
$route_items = explode('/', $route_uri);
|
|
|
|
$request_items = explode('/', $request_uri);
|
|
|
|
$nb_route_items = count($route_items);
|
|
|
|
|
|
|
|
if ($nb_route_items === count($request_items)) {
|
|
|
|
|
|
|
|
for ($i = 0; $i < $nb_route_items; ++$i) {
|
|
|
|
|
|
|
|
if ($route_items[$i][0] === ':') {
|
|
|
|
|
|
|
|
$params[substr($route_items[$i], 1)] = $request_items[$i];
|
|
|
|
}
|
|
|
|
else if ($route_items[$i] !== $request_items[$i]) {
|
|
|
|
|
|
|
|
$params = array();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-08 20:13:14 +01:00
|
|
|
}
|