Skip to content

Commit

Permalink
增加路由配置模式
Browse files Browse the repository at this point in the history
  • Loading branch information
orangesir committed Mar 22, 2017
1 parent 4bfb425 commit 5e9df1c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
86 changes: 68 additions & 18 deletions src/Route.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php
namespace Pd;

use Pd\Exception\SystemException;

class Route {

protected $request;
protected $controller;
protected $controllerClassString;
protected $methodString;
protected $configArray;

public function getController() {
if($this->controller) {
Expand Down Expand Up @@ -46,6 +49,38 @@ public function setMethodString($methodString) {
public function setRequest(Request $request) {
$this->request = $request;
}

/**
* @return mixed
*/
public function getControllerClassString()
{
return $this->controllerClassString;
}

/**
* @return mixed
*/
public function getMethodString()
{
return $this->methodString;
}

/**
* @return mixed
*/
public function getConfigArray()
{
return $this->configArray;
}

/**
* @param mixed $configArray
*/
public function setConfigArray($configArray)
{
$this->configArray = $configArray;
}

public function parse() {
$this->controllerRelationMap($this->request);
Expand All @@ -56,24 +91,39 @@ public function parse() {
*/
public function controllerRelationMap(Request $request) {
$uri = $request->uri();
if(trim($uri,"/")=="") {
$this->setControllerClassString("\\Controller\\Home");
$this->setMethodString("index");
} else {
$exUri = explode("/", trim($uri,"/"));
if($this->getConfigArray()) {
// actionString ===> controllerclass::$method
foreach ($this->getConfigArray() as $regex => $actionString) {
if($regex==$uri || preg_match($regex, $uri)) {
$actions = explode("::", trim($actionString));
if(count($actions)!=2) {
throw new SystemException("route config error:".$regex."=>".$actionString);
}
$this->setControllerClassString($actions[0]);
$this->setMethodString($actions[1]);
break;
}
}
} else {
if(trim($uri,"/")=="") {
$this->setControllerClassString("\\Controller\\Home");
$this->setMethodString("index");
} else {
$exUri = explode("/", trim($uri,"/"));

$method = $exUri[count($exUri)-1];
if(strpos($method, "_")===0) {
// 不允许访问_开头的方法
throw new \Pd\Exception\NotActionException();
}
$controllerString = "";
for($i=0; $i<count($exUri)-1; $i++) {
$itemString = ucfirst($exUri[$i]);
$controllerString .= "\\".$itemString;
}
$this->setControllerClassString("\\Controller".$controllerString);
$this->setMethodString($method);
}
$method = $exUri[count($exUri)-1];
if(strpos($method, "_")===0) {
// 不允许访问_开头的方法
throw new \Pd\Exception\NotActionException();
}
$controllerString = "";
for($i=0; $i<count($exUri)-1; $i++) {
$itemString = ucfirst($exUri[$i]);
$controllerString .= "\\".$itemString;
}
$this->setControllerClassString("\\Controller".$controllerString);
$this->setMethodString($method);
}
}
}
}
25 changes: 25 additions & 0 deletions tests/Pd/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace tests;

use \Pd\Request;
use Pd\Route;

class RouteTest extends TestCase {

public function testConfigMapWithConfig() {
$request = $this->getMockBuilder(Request::class)
->getMock();
$request->method("uri")->willReturn("/test/info");

$configArray = array(
"/test/info" => '\Hello\ClassInfo::soft'
);

$route = new Route();
$route->setConfigArray($configArray);
$route->controllerRelationMap($request);

$this->assertEquals($route->getControllerClassString(), "\\Hello\\ClassInfo");
$this->assertEquals($route->getMethodString(),"soft");
}
}

0 comments on commit 5e9df1c

Please sign in to comment.