public function dispatch($c, $action) { if (!$c instanceof DF_Web) { throw new DF_Error_InvalidArgumentException('c', $c, 'DF_Web'); } if (!$action instanceof DF_Web_Action) { throw new DF_Error_InvalidArgumentException('action', $action, 'DF_Web_Action'); } $ctrl_name = $action->get_controller(); $method = $action->get_method(); $arguments = $action->get_arguments(); $req = $c->request; $verb = $req->get_method(); $verb = strtoupper($verb); $rest_method = $method . "_" . $verb; $controller = $c->controller($ctrl_name); # Prepend the context object to the argumentlist if ($arguments == NULL) { $arguments = array(); } array_unshift($arguments, $c); if (!method_exists($controller, $rest_method)) { $c->response->status(405); #Method not allowed TODO check that it is 405 or other? $argstr = DF_Web_Utils_Arguments::flatten_arguments_list($arguments); self::$LOGGER->info("Action {$ctrl_name}->{$method} does not handle {$verb}"); #throw new DF_Web_Detach_Exception($action, "Method not allowed: $verb"); #throw new DF_Web_Exception("Method not allowed: $verb"); return false; } try { $ret = call_user_func_array(array($controller, $rest_method), $arguments); return $ret; } catch (DF_Web_Detach_Exception $ex) { // Rethrow the exception to continue breaking the chain throw $ex; } catch (DF_Web_Exception $ex) { $c->add_error($ex); self::$LOGGER->error("Fatal exception: " . $ex->getMessage()); } catch (Exception $ex) { $c->add_error($ex); self::$LOGGER->error("Fatal exception, breaking off chain: " . $ex->getMessage()); throw new DF_Web_Detach_Exception($this, "Got an error we cannot handle"); } return false; }
require_once 'DF/Error.php'; require_once 'DF/Error/InvalidArgumentException.php'; class DF_Web_Utils_Arguments { public static $LOGGER = NULL; public static function flatten_arguments_list($arguments) { if (!is_array($arguments)) { throw new DF_Error_InvalidArgumentException('arguments', $arguments, 'array'); } $str = array(); foreach ($arguments as $arg) { if (NULL === $arg) { $str[] = "NULL"; } elseif (gettype($arg) == 'object') { $str[] = get_class($arg); } elseif (gettype($arg) == 'string') { $str[] = '"' . $arg . '"'; } elseif (gettype($arg) == 'array') { $str[] = 'Array'; } else { $type = gettype($arg); self::$LOGGER->warn("Building argument list. Unknown type: {$type} - value: {$arg}"); $str[] = '"' . $arg . '"'; } } return join(', ', $str); } } DF_Web_Utils_Arguments::$LOGGER = DF_Web_Logger::logger('DF_Web_Utils_Arguments');
public function toString() { $args = ""; if ($this->arguments) { $args = DF_Web_Utils_Arguments::flatten_arguments_list($this->arguments); } $ctrl = $this->controller; $method = $this->method; return "{$ctrl}->{$method}({$args})"; }