miniflux-legacy/vendor/PicoFarad/Router.php

160 lines
3.6 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFarad\Router;
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
function action($name, callable $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
function post_action($name, callable $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
function get_action($name, callable $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
function notfound(callable $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
function get($url, callable $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
function post($url, callable $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
function put($url, callable $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
function delete($url, callable $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
function find_route($method, $route, callable $callback)
2013-02-18 03:48:21 +01:00
{
if ($_SERVER['REQUEST_METHOD'] === $method) {
if ($_SERVER['QUERY_STRING']) {
$url = substr($_SERVER['REQUEST_URI'], 0, -(strlen($_SERVER['QUERY_STRING']) + 1));
}
else {
$url = $_SERVER['REQUEST_URI'];
}
$params = array();
if (url_match($route, $url, $params)) {
before($handler);
\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
}