<?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;
-use Fig\Http\Message\RequestMethodInterface;
+namespace PHPRouter;
class Route
{
/**
* Accepted HTTP methods for this route.
+ *
* @var string[]
*/
- private $methods = array(
- RequestMethodInterface::METHOD_GET,
- RequestMethodInterface::METHOD_POST,
- RequestMethodInterface::METHOD_PUT,
- RequestMethodInterface::METHOD_DELETE,
- );
+ private $methods;
/**
* Target for this route, can be anything.
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()
$this->parameters = $parameters;
}
- public function dispatch()
+ public function getValidController()
{
- $action = explode('::', $this->config['_controller']);
+ $class = $this->class;
+ $method = $this->action;
+
+ if ( !class_exists($class)) {
+ return null;
+ }
+
+ if (empty($method) || trim($method) === '') {
+ $method = "__invoke";
+ }
- if ($this->parametersByName) {
- $this->parameters = array($this->parameters);
+ try {
+ $classRexl = new \ReflectionClass($class);
+ } catch (\ReflectionException $ex) {
+ return null;
}
- $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
+ try {
+ $classRexl->getMethod($method);
+ } catch (\ReflectionException $ex) {
+ return null;
+ }
+
+ return $this->config['_controller'];
+ }
+
+ public function dispatch($instance = null)
+ {
+ is_null($instance) and $instance = new $this->class();
+
+ // todo figure out what parametersByName is for
+ $param = $this->parametersByName ?
+ array($this->parameters) :
+ $this->parameters;
- if (!is_null($this->action)) {
- $instance = new $action[0];
- call_user_func_array(array($instance, $this->action), $this->parameters);
+ ob_start();
+
+ if (empty($this->action) || trim($this->action) === '') {
+ // __invoke on a class
+ call_user_func_array($instance, $param);
} else {
- $instance = new $action[0]($this->parameters);
+ call_user_func_array(array($instance, $this->action), $param);
}
+
+ $result = ob_get_clean();
+
+ return $result;
}
public function getAction()
{
return $this->action;
}
+
+ public function getClass()
+ {
+ return $this->class;
+ }
}