<?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;
private $parametersByName;
/**
- * @var string
+ * @var null|string
*/
private $action;
private $config;
/**
+ * @var null|string
+ */
+ private $class;
+
+ /**
* @param $resource
* @param array $config
*/
$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;
}
$this->parameters = $parameters;
}
- public function getValidRouteAction()
+ public function getValidController()
{
- $action = explode('::', $this->config['_controller']);
- $class = @$action[0];
- $method = @$action[1];
+ $class = $this->class;
+ $method = $this->action;
if ( !class_exists($class)) {
return null;
}
- $instance = new $class();
-
- if (empty($action[1]) || trim($action[1]) === '') {
+ if (empty($method) || trim($method) === '') {
$method = "__invoke";
}
- if ( !method_exists($instance, $method)) {
+ 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()
+ 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);
- }
+ // todo figure out what parametersByName is for
+ $param = $this->parametersByName ?
+ array($this->parameters) :
+ $this->parameters;
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);
+ call_user_func_array($instance, $param);
} else {
- call_user_func_array(array($instance, $action[1]), $this->parameters);
+ call_user_func_array(array($instance, $this->action), $param);
}
$result = ob_get_clean();
{
return $this->action;
}
+
+ public function getClass()
+ {
+ return $this->class;
+ }
}