Skip to content

Commit

Permalink
adding selectors to PhRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Delrue authored and Jonas Delrue committed Sep 4, 2016
1 parent 5915619 commit 60202af
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
32 changes: 31 additions & 1 deletion src/Phroute/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function __construct(RouteDataInterface $data, HandlerResolverInterface $
*/
public function dispatch($httpMethod, $uri)
{
list($uri, $selectors) = $this->explodeSelectors($uri);

list($handler, $filters, $vars) = $this->dispatchRoute($httpMethod, trim($uri, '/'));

list($beforeFilter, $afterFilter) = $this->parseFilters($filters);
Expand All @@ -54,12 +56,38 @@ public function dispatch($httpMethod, $uri)
}

$resolvedHandler = $this->handlerResolver->resolve($handler);

array_push ($vars, $selectors);
$response = call_user_func_array($resolvedHandler, $vars);

return $this->dispatchFilters($afterFilter, $response);
}

/**
* Add selectors (eg. ?deleted=true to called function as argument)
*
* @param $uri
* @return array|url, array of selectors
*/
private function explodeSelectors($uri){
if(strpos($uri,'?') !== false){
list($url, $selectors) = explode("?", $_SERVER['REQUEST_URI']);

$arrayOfFilters = explode("&", $selectors);
$asArrayOfFilters = Array();
for( $i=0; $i<count($arrayOfFilters); $i++){
list($key, $value) = explode("=", $arrayOfFilters[$i]);
$asArrayOfFilters[$key] = $value;
}

$selectors = $asArrayOfFilters;

return array($url, $selectors);

}else{
return array($uri, array());
}

}
/**
* Dispatch a route filter.
*
Expand Down Expand Up @@ -186,6 +214,7 @@ private function dispatchVariableRoute($httpMethod, $uri)
{
if (!preg_match($data['regex'], $uri, $matches))
{

continue;
}

Expand All @@ -202,6 +231,7 @@ private function dispatchVariableRoute($httpMethod, $uri)

foreach (array_values($routes[$httpMethod][2]) as $i => $varName)
{

if(!isset($matches[$i + 1]) || $matches[$i + 1] === '')
{
unset($routes[$httpMethod][2][$varName]);
Expand Down
1 change: 1 addition & 0 deletions src/Phroute/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private function addStaticRoute($httpMethod, $routeData, $handler, $filters)
*/
private function addVariableRoute($httpMethod, $routeData, $handler, $filters)
{

list($regex, $variables) = $routeData;

if (isset($this->regexToRoutesMap[$regex][$httpMethod]))
Expand Down
10 changes: 8 additions & 2 deletions src/Phroute/RouteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ class RouteParser {
* Parse a route returning the correct data format to pass to the dispatch engine.
*
* @param $route
* @return array
* @return ?
*/
public function parse($route)
{
if (strpos($route, '{selectors}') == false) {
$route .= '/{selectors}?';
}else if (strpos($route, '{selectors}?') == false) {
$route = str_replace('{selectors}', '{selectors}?', $route);
}


$this->reset();

$route = strtr($route, $this->regexShortcuts);
Expand Down Expand Up @@ -106,7 +113,6 @@ public function parse($route)
}

$this->staticParts($route, strlen($route));

return [[implode('', $this->parts), $this->variables], array_values($this->reverseParts)];
}

Expand Down
5 changes: 5 additions & 0 deletions test/Dispatcher/DispatcherTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php



namespace Phroute\Phroute\Dispatcher;

use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\RouteParser;
use Phroute\Phroute\Dispatcher;
use Phroute\Phroute\Route;

include __DIR__ . '/../../vendor/autoload.php';

class Test {

public function route()
Expand Down Expand Up @@ -76,6 +80,7 @@ public function getParameterHyphenated($param)

public function getParameterOptional($param = 'default')
{
echo "ok";
return $param;
}

Expand Down
35 changes: 35 additions & 0 deletions test/Dispatcher/SelectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

include __DIR__ . '/../../vendor/autoload.php';


use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;

$router = new RouteCollector();

class Selectors {
public function getIndex($selectors){
print_r($selectors);
return "go";
}


}


$router->controller('/selectors', 'Selectors');



$dispatcher = new Dispatcher($router->getData());


// Todo code should be part of phroute...
$url = $_SERVER['REQUEST_URI'];


$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $url);

echo $response;
?>

0 comments on commit 60202af

Please sign in to comment.