OSDN Git Service

dispatch route with a custom instance
authorfrostbane <frostbane@programmer.net>
Wed, 27 Nov 2019 09:50:40 +0000 (18:50 +0900)
committerfrostbane <frostbane@programmer.net>
Thu, 28 Nov 2019 04:45:40 +0000 (13:45 +0900)
phpunit.xml
src/Route.php
tests/Fixtures/CustomController.php [new file with mode: 0644]

index 8e4a894..d70ed86 100644 (file)
@@ -8,25 +8,28 @@
          convertWarningsToExceptions="true"
          processIsolation="false"
          stopOnFailure="false">
-    <testsuites>
-        <testsuite name="PHP Router">
-            <directory>tests</directory>
-        </testsuite>
-    </testsuites>
-
-    <filter>
-        <whitelist>
-            <directory>./</directory>
-            <exclude>
-                <directory>./tests/Fixtures</directory>
-                <directory>./vendor</directory>
-            </exclude>
-        </whitelist>
-    </filter>
-
-    <logging>
-        <log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
-        <log type="coverage-clover" target="logs/clover.xml"/>
-    </logging>
-
+  <testsuites>
+    <testsuite name="PHP Router">
+      <directory>tests</directory>
+    </testsuite>
+  </testsuites>
+  <filter>
+    <whitelist>
+      <directory>./</directory>
+      <exclude>
+        <directory>./tests/Fixtures</directory>
+        <directory>./vendor</directory>
+      </exclude>
+    </whitelist>
+  </filter>
+  <logging>
+    <log type="coverage-text"
+         target="php://stdout"
+         showUncoveredFiles="true" />
+    <log type="coverage-clover"
+         target="logs/clover.xml" />
+  </logging>
+  <php>
+    <server name="REQUEST_TIME" value="0" />
+  </php>
 </phpunit>
index 2cde91a..81729a5 100755 (executable)
@@ -71,6 +71,7 @@ class Route
         $this->name       = isset($config['name']) ? $config['name'] : null;
         $this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
         $action           = explode('::', $this->config['_controller']);
+        $this->class      = isset($action[0]) ? $action[0] : null;
         $this->action     = isset($action[1]) ? $action[1] : null;
     }
 
@@ -180,10 +181,9 @@ class Route
         return $this->config['_controller'];
     }
 
-    public function dispatch()
+    public function dispatch($instance = null)
     {
-        $action   = explode('::', $this->config['_controller']);
-        $instance = new $action[0]();
+        is_null($instance) and $instance = new $this->class();
 
         if ($this->parametersByName) {
             $this->parameters = array($this->parameters);
@@ -191,11 +191,11 @@ class Route
 
         ob_start();
 
-        if (empty($action[1]) || trim($action[1]) === '') {
+        if (empty($this->action) || trim($this->action) === '') {
             // __invoke on a class
             call_user_func_array($instance, $this->parameters);
         } else {
-            call_user_func_array(array($instance, $action[1]), $this->parameters);
+            call_user_func_array(array($instance, $this->action), $this->parameters);
         }
 
         $result = ob_get_clean();
diff --git a/tests/Fixtures/CustomController.php b/tests/Fixtures/CustomController.php
new file mode 100644 (file)
index 0000000..e768bdb
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace PHPRouter\Test\Fixtures;
+
+final class CustomController
+{
+    private $config;
+
+    public function __construct($config)
+    {
+        $this->config = $config;
+    }
+
+    public function index()
+    {
+    }
+}