OSDN Git Service

add more route unit tests
[php-libraries/Router.git] / src / Route.php
index a18a47b..bff2403 100755 (executable)
@@ -1,20 +1,5 @@
 <?php
-/**
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license.
- */
+
 namespace PHPRouter;
 
 class Route
@@ -27,9 +12,10 @@ class Route
 
     /**
      * Accepted HTTP methods for this route.
+     *
      * @var string[]
      */
-    private $methods = array('GET', 'POST', 'PUT', 'DELETE');
+    private $methods;
 
     /**
      * Target for this route, can be anything.
@@ -41,7 +27,7 @@ class Route
      * The name of this route, used for reversed routing
      * @var string
      */
-    private $name = null;
+    private $name;
 
     /**
      * Custom parameter filters for this route
@@ -63,21 +49,35 @@ class Route
     private $parametersByName;
 
     /**
+     * @var null|string
+     */
+    private $action;
+
+    /**
      * @var array
      */
     private $config;
 
     /**
+     * @var null|string
+     */
+    private $class;
+
+    /**
      * @param       $resource
      * @param array $config
      */
     public function __construct($resource, array $config)
     {
-        $this->url     = $resource;
-        $this->config  = $config;
-        $this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
-        $this->target  = isset($config['target']) ? $config['target'] : null;
-        $this->name    = isset($config['name']) ? $config['name'] : null;
+        $this->url        = $resource;
+        $this->config     = $config;
+        $this->methods    = isset($config['methods']) ? (array)$config['methods'] : array();
+        $this->target     = isset($config['target']) ? $config['target'] : null;
+        $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;
     }
 
     public function getUrl()
@@ -157,15 +157,64 @@ class Route
         $this->parameters = $parameters;
     }
 
-    public function dispatch()
+    public function getValidController()
+    {
+        $class  = $this->class;
+        $method = $this->action;
+
+        if ( !class_exists($class)) {
+            return null;
+        }
+
+        if (empty($method) || trim($method) === '') {
+            $method = "__invoke";
+        }
+
+        try {
+            $classRexl = new \ReflectionClass($class);
+        } catch (\ReflectionException $ex) {
+            return null;
+        }
+
+        try {
+            $classRexl->getMethod($method);
+        } catch (\ReflectionException $ex) {
+            return null;
+        }
+
+        return $this->config['_controller'];
+    }
+
+    public function dispatch($instance = null)
     {
-        $action = explode('::', $this->config['_controller']);
-        $instance = new $action[0];
+        is_null($instance) and $instance = new $this->class();
+
+        // todo figure out what parametersByName is for
+        $param = $this->parametersByName ?
+            array($this->parameters) :
+            $this->parameters;
+
+        ob_start();
 
-        if ($this->parametersByName) {
-            $this->parameters = array($this->parameters);
+        if (empty($this->action) || trim($this->action) === '') {
+            // __invoke on a class
+            call_user_func_array($instance, $param);
+        } else {
+            call_user_func_array(array($instance, $this->action), $param);
         }
 
-        call_user_func_array(array($instance, $action[1]), $this->parameters);
+        $result = ob_get_clean();
+
+        return $result;
+    }
+
+    public function getAction()
+    {
+        return $this->action;
+    }
+
+    public function getClass()
+    {
+        return $this->class;
     }
 }