/**
|
* Router
|
*
|
* @author can acar
|
* @copyright 2011
|
* @version $Id$
|
* @access public
|
*/
|
class Router
|
{
|
|
protected static $instance = array();
|
protected $Route;
|
|
/**
|
* Router::__construct()
|
*
|
* @return
|
*/
|
private function __construct()
|
{
|
}
|
|
/**
|
* Router::instance()
|
*
|
* @return
|
*/
|
public static function instance()
|
{
|
if( isset( self::$instance ) and ( self::$instance instanceof self ) )
|
{
|
|
return self::$instance;
|
} else
|
{
|
|
self::$instance = new self();
|
return self::$instance;
|
}
|
}
|
|
/**
|
* Router::Connect()
|
*
|
* @param mixed $path
|
* @param mixed $params
|
* @param mixed $filter
|
* @return
|
*/
|
public function Connect( $path, $params = array(), $filter = array() )
|
{
|
|
$this->Route[$path] = new Route( $path, $params, $filter );
|
|
}
|
|
|
/**
|
* Router::Match()
|
*
|
* @param mixed $url
|
* @return
|
*/
|
public function Match( $url )
|
{
|
$Matches = array();
|
$url = '/'.trim($url,'/');
|
|
foreach ( $this->Route as $Route )
|
{
|
|
$regex = preg_replace( '/:(\w+)/', '(?P<$1>\w+)', $Route->path );
|
$regex = str_replace( '/', '\/', $regex );
|
|
if( preg_match( "/^" . $regex . "$/", $url, $Match ) )
|
{
|
|
foreach ( $Route->params as $key => $value )
|
{
|
$Matches[$key] = $value;
|
|
}
|
|
foreach ( $Route->filter as $property => $Regex )
|
{
|
if( array_key_exists( $property, $Match ) )
|
{
|
|
$o = preg_match( '/^' . $Regex . '$/', $Match[$property], $t );
|
$Matches[$property] = $t[0];
|
}
|
}
|
unset($property,$Regex,$Match);
|
}
|
}unset($Route);
|
return $Matches;
|
|
}
|
|
}
|
******************************
|
/**
|
* Route
|
*
|
* @author can acar
|
* @copyright 2011
|
* @version $Id$
|
* @access public
|
*/
|
class Route
|
{
|
|
public $path;
|
public $params = array();
|
public $filter;
|
|
/**
|
* Route::__construct()
|
*
|
* @param mixed $path
|
* @param mixed $params
|
* @param mixed $filter
|
* @return
|
*/
|
public function __construct( $path, $params, $filter )
|
{
|
$this->path = '/'.trim( $path,'/');
|
$this->params = $params;
|
$this->filter = $filter;
|
return $this;
|
}
|
|
|
} |