improved performence
This commit is contained in:
parent
3fe539d0aa
commit
6492b080de
2 changed files with 17 additions and 18 deletions
|
@ -23,6 +23,8 @@
|
|||
|
||||
namespace Volkszaehler\Controller;
|
||||
|
||||
use Volkszaehler\View;
|
||||
|
||||
/**
|
||||
* Controller superclass for all controllers
|
||||
*
|
||||
|
@ -39,7 +41,7 @@ abstract class Controller {
|
|||
* @param View $view
|
||||
* @param EntityManager $em
|
||||
*/
|
||||
public function __construct(\Volkszaehler\View\View $view, \Doctrine\ORM\EntityManager $em) {
|
||||
public function __construct(View\View $view, \Doctrine\ORM\EntityManager $em) {
|
||||
$this->view = $view;
|
||||
$this->em = $em;
|
||||
}
|
||||
|
@ -49,13 +51,19 @@ abstract class Controller {
|
|||
*
|
||||
* @param string $operation runs the operation if class method is available
|
||||
*/
|
||||
public function run($operation, array $identifiers = array()) {
|
||||
if (!is_callable(array($this, $operation))) {
|
||||
throw new \Exception('Invalid context operation: ' . $operation);
|
||||
public function run($op, array $arg = array()) {
|
||||
if (!method_exists($this, $op)) {
|
||||
throw new \Exception('Invalid context operation: ' . $op);
|
||||
}
|
||||
|
||||
switch(count($arg)) { // improved performence
|
||||
case 0: return $this->{$op}();
|
||||
case 1: return $this->{$op}($arg[0]);
|
||||
case 2: return $this->{$op}($arg[0], $arg[1]);
|
||||
case 3: return $this->{$op}($arg[0], $arg[1], $arg[2]);
|
||||
default: return call_user_func_array(array($this, $op), $arg);
|
||||
}
|
||||
|
||||
return call_user_func_array(array($this, $operation), $identifiers);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
|
|
@ -116,8 +116,6 @@ class Router {
|
|||
$this->pathInfo = self::getPathInfo();
|
||||
$this->format = pathinfo($this->pathInfo, PATHINFO_EXTENSION);
|
||||
|
||||
Util\Debug::log('env vars', $_SERVER);
|
||||
|
||||
if (!array_key_exists($this->format, self::$viewMapping)) {
|
||||
$this->view = new View\JSON($request, $response); // fallback view
|
||||
|
||||
|
@ -143,8 +141,7 @@ class Router {
|
|||
*/
|
||||
public function run() {
|
||||
$operation = self::getOperation($this->view->request);
|
||||
$context = substr($this->pathInfo, 1, strrpos($this->pathInfo, '.') -1); // remove leading slash and format
|
||||
$context = explode('/', $context); // split into path segments
|
||||
$context = explode('/', substr($this->pathInfo, 1, strrpos($this->pathInfo, '.')-1)); // parse pathinfo
|
||||
|
||||
if (!array_key_exists($context[0], self::$controllerMapping)) {
|
||||
if (empty($context[0])) {
|
||||
|
@ -158,13 +155,7 @@ class Router {
|
|||
$class = self::$controllerMapping[$context[0]];
|
||||
$controller = new $class($this->view, $this->em);
|
||||
|
||||
if (isset($pathInfo[1])) {
|
||||
$result = $controller->run($operation, array_slice($context, 1));
|
||||
}
|
||||
else {
|
||||
$result = $controller->run($operation);
|
||||
}
|
||||
|
||||
$result = $controller->run($operation, array_slice($context, 1));
|
||||
$this->view->add($result);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue