Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbuscher committed Dec 9, 2015
1 parent 7d803d3 commit 8707e73
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 153 deletions.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Noah Buscher
Copyright (c) 2015 Noah Buscher

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
280 changes: 130 additions & 150 deletions Macaw.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,167 +10,147 @@
* @method static Macaw options(string $route, Callable $callback)
* @method static Macaw head(string $route, Callable $callback)
*/
class Macaw
{
class Macaw {
public static $halts = false;
public static $routes = array();
public static $methods = array();
public static $callbacks = array();
public static $patterns = array(
':any' => '[^/]+',
':num' => '[0-9]+',
':all' => '.*'
);
public static $error_callback;

/**
* Defines a route w/ callback and method
*/
public static function __callstatic($method, $params) {
$uri = dirname($_SERVER['PHP_SELF']).$params[0];
$callback = $params[1];

array_push(self::$routes, $uri);
array_push(self::$methods, strtoupper($method));
array_push(self::$callbacks, $callback);
}

/**
* Defines callback if route is not found
*/
public static function error($callback) {
self::$error_callback = $callback;
}

public static function haltOnMatch($flag = true) {
self::$halts = $flag;
}

/**
* Runs the callback for the given request
*/
public static function dispatch(){
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];

$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);

$found_route = false;

self::$routes = str_replace('//', '/', self::$routes);

// Check if route is defined without regex
if (in_array($uri, self::$routes)) {
$route_pos = array_keys(self::$routes, $uri);
foreach ($route_pos as $route) {
// Using an ANY option to match both GET and POST requests
if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
$found_route = true;

// If route is not an object
if (!is_object(self::$callbacks[$route])) {

// Grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$route]);

// Collect the last index of the array
$last = end($parts);

// Grab the controller name and method call
$segments = explode('@',$last);

// Instanitate controller
$controller = new $segments[0]();

// Call method
$controller->$segments[1]();

if (self::$halts) return;
} else {
// Call closure
call_user_func(self::$callbacks[$route]);

if (self::$halts) return;
}
}
}
} else {
// Check if defined with regex
$pos = 0;
foreach (self::$routes as $route) {
if (strpos($route, ':') !== false) {
$route = str_replace($searches, $replaces, $route);
}

public static $halts = false;
if (preg_match('#^' . $route . '$#', $uri, $matched)) {
if (self::$methods[$pos] == $method) {
$found_route = true;

public static $routes = array();
// Remove $matched[0] as [1] is the first parameter.
array_shift($matched);

public static $methods = array();
if (!is_object(self::$callbacks[$pos])) {

public static $callbacks = array();
// Grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$pos]);

public static $patterns = array(
':any' => '[^/]+',
':num' => '[0-9]+',
':all' => '.*'
);
// Collect the last index of the array
$last = end($parts);

public static $error_callback;
// Grab the controller name and method call
$segments = explode('@',$last);

/**
* Defines a route w/ callback and method
*/
public static function __callstatic($method, $params)
{

$uri = dirname($_SERVER['PHP_SELF']).$params[0];
$callback = $params[1];
// Instanitate controller
$controller = new $segments[0]();

array_push(self::$routes, $uri);
array_push(self::$methods, strtoupper($method));
array_push(self::$callbacks, $callback);
}
// Fix multi parameters
if(!method_exists($controller, $segments[1])) {
echo "controller and action not found";
} else {
call_user_func_array(array($controller, $segments[1]), $matched);
}

/**
* Defines callback if route is not found
*/
public static function error($callback)
{
self::$error_callback = $callback;
}

public static function haltOnMatch($flag = true)
{
self::$halts = $flag;
}
if (self::$halts) return;
} else {
call_user_func_array(self::$callbacks[$pos], $matched);

/**
* Runs the callback for the given request
*/
public static function dispatch()
{
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];

$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);

$found_route = false;

self::$routes = str_replace('//', '/', self::$routes);

// check if route is defined without regex
if (in_array($uri, self::$routes)) {
$route_pos = array_keys(self::$routes, $uri);
foreach ($route_pos as $route) {

//using an ANY option to match both GET and POST requests
if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
$found_route = true;

//if route is not an object
if(!is_object(self::$callbacks[$route])){

//grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$route]);

//collect the last index of the array
$last = end($parts);

//grab the controller name and method call
$segments = explode('@',$last);

//instanitate controller
$controller = new $segments[0]();

//call method
$controller->$segments[1]();

if (self::$halts) return;

} else {
//call closure
call_user_func(self::$callbacks[$route]);

if (self::$halts) return;
}
}
if (self::$halts) return;
}
} else {
// check if defined with regex
$pos = 0;
foreach (self::$routes as $route) {

if (strpos($route, ':') !== false) {
$route = str_replace($searches, $replaces, $route);
}

if (preg_match('#^' . $route . '$#', $uri, $matched)) {
if (self::$methods[$pos] == $method) {
$found_route = true;

array_shift($matched); //remove $matched[0] as [1] is the first parameter.


if(!is_object(self::$callbacks[$pos])){

//grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$pos]);

//collect the last index of the array
$last = end($parts);

//grab the controller name and method call
$segments = explode('@',$last);

//instanitate controller
$controller = new $segments[0]();

//fix multi parameters
if(!method_exists($controller, $segments[1])){
echo "controller and action not found";
}else{
call_user_func_array(array($controller, $segments[1]), $matched);
}

//call method and pass any extra parameters to the method
// $controller->$segments[1](implode(",", $matched));

if (self::$halts) return;
} else {
call_user_func_array(self::$callbacks[$pos], $matched);

if (self::$halts) return;
}

}
}
$pos++;
}
}


// run the error callback if the route was not found
if ($found_route == false) {
if (!self::$error_callback) {
self::$error_callback = function() {
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
echo '404';
};
}
call_user_func(self::$error_callback);
}
}
$pos++;
}
}

// Run the error callback if the route was not found
if ($found_route == false) {
if (!self::$error_callback) {
self::$error_callback = function() {
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
echo '404';
};
}
call_user_func(self::$error_callback);
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Macaw
=====

Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and sexy. This class allows you to jus throw it into your project and start using it immediately.
Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately.

### Install

Expand Down Expand Up @@ -67,4 +67,4 @@ If you don't specify an error callback, Macaw will just echo `404`.

<hr>

In order to let the server know the URI does not point to a real file, you need to use the included [.htaccess](http://httpd.apache.org/docs/2.2/howto/htaccess.html) file.
In order to let the server know the URI does not point to a real file, you may need to use one of the example [configuration files](https://github.com/noahbuscher/Macaw/blob/master/config).
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8707e73

Please sign in to comment.