Source

Ray / Ray / Router / Route.php

<?php

namespace Ray\Router;

use 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 Route{

    /**
     * @var string
     */
    private $pattern;

    /**
     * @var Callable
     */
    private $callable;

    /**
     * @var string
     */
    private $view;

    /**
     * @var array
     */
    private $params;

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

    /**
     * @param string $pattern
     * @param Callable $callable
     * @param string $view
     */
    public function __construct($pattern, $callable, $view=null){
        $this->pattern = $pattern;
        $this->callable = $callable;
        $this->view = $view;
    }

    /**
     * Check if Route is matching Request uri and method
     * @param $call
     * @return bool
     */
    public function check($call){

        preg_match_all('@:([\w]+)@', $this->pattern, $params, PREG_PATTERN_ORDER);
        $params = current($params);

        $regex = preg_replace_callback('@:[\w]+@', array($this, 'getRegex'), $this->pattern);

        if ( substr($this->pattern, -1) === '/' ) {
            $regex = $regex . '?';
        }

        if(preg_match('@^' . $regex . '$@', $call, $values)){
            array_shift($values);
            foreach($params as $k=>$v){
                $val = substr($v, 1);
                if(isset($values[$val])){
                    $this->params[$val] = urldecode($values[$val]);
                }
            }

            return true;
        }

        return false;
    }

    /**
     * Set custom conditions
     * @param array $conditions
     */
    public function conditions(array $conditions){
        $this->conditions = $conditions;
    }

    /**
     * Process route
     * @return array
     */
    public function process(){

        if(is_array($this->params)){
            $return = call_user_func_array($this->callable, $this->params);
        }else{
            $return = call_user_func($this->callable);
        }

        return array(
            'view' => $this->view,
            'callback' => $return
        );
    }

    /**
     * Create regex for processing
     * @param $matches
     * @return string
     */
    protected function getRegex( $matches ) {
        $key = substr(current($matches),1);

        $condition = isset($this->conditions[$key]) ? $this->conditions[$key] :
                     '.*?';

        return '(?P<'.$key.'>' . $condition . ')';
    }

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.