Source

Ray / Ray / Router.php

Full commit
<?php

namespace Ray;

/**
 * Ray - micro framework for PHP
 * @author Mateusz Łopaciński <mateusz@robopuff.com> {@link http://robopuff.com}
 * @since 1.0.0
 * @version 1.0.0
 */
class Router{

    /**
     * @var array
     */
    private $routes = array();

    /**
     * Add new route
     * @param $method
     * @param $pattern
     * @param $callable
     * @param null $view
     * @return array
     */
    public function map($method, $pattern, $callable, $view=null){

        if(is_array($pattern)){
            $return = array();

            foreach($pattern as $p){
                $return[] = $this->map($method, $p, $callable, $view);
            }

            return $return;
        }

        $route = $this->routes[$method][$pattern] =  new Router\Route($pattern, $callable, $view);

        return $route;
    }

    /**
     * Check if route exist
     * @param $method
     * @param $pattern
     * @return bool
     */
    public function hasRoute($method, $pattern){
        return isset($this->routes[$method]) && isset($this->routes[$method][$pattern]);
    }

    /**
     * Check routes against request uri and method
     * @param $request
     * @return bool
     */
    public function process($request){

        if(isset($this->routes[$request->method])){
            $active = false;

            foreach($this->routes[$request->method] as $route){
                if($route->check($request->uri) === true){
                    $active = $route;
                    break;
                }
            }

            if($active){
                return $active->process();
            }
        }

        return false;
    }

}