OSDN Git Service

Fix issue #69 and properly implement PR #65 & #67 + unit tests
authorAntoine Pous <gecko@dvp.io>
Thu, 27 Oct 2016 11:36:25 +0000 (13:36 +0200)
committerAntoine Pous <gecko@dvp.io>
Thu, 27 Oct 2016 11:36:25 +0000 (13:36 +0200)
src/Route.php
tests/src/PHPRouterTest/RouteTest.php

index d833f7a..96c09f5 100755 (executable)
@@ -160,16 +160,23 @@ class Route
     public function dispatch()
     {
         $action = explode('::', $this->config['_controller']);
-        $instance = new $action[0];
 
         if ($this->parametersByName) {
             $this->parameters = array($this->parameters);
         }
 
-        if(empty($action[1]) || trim($action[1]) == '') {
-          $action[1] = '__construct';
+        $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
+
+        if (!is_null($this->action)) {
+            $instance = new $action[0];
+            call_user_func_array(array($instance, $this->action), $this->parameters);
+        } else {
+            $instance = new $action[0]($this->parameters);
         }
+    }
 
-        call_user_func_array(array($instance, $action[1]), $this->parameters);
+    public function getAction()
+    {
+        return $this->action;
     }
 }
index e2b5cd0..a8e7b2b 100644 (file)
@@ -88,4 +88,9 @@ class RouteTest extends PHPUnit_Framework_TestCase
         $this->routeWithParameters->setName('pageroute');
         $this->assertEquals('pageroute', $this->routeWithParameters->getName());
     }
+
+    public function testGetAction()
+    {
+        $this->assertEquals('page', $this->routeWithParameters->getAction());
+    }
 }