public static function build($rootNode, $vals = null) { $xsz = new XML_Serializer(array('rootName' => $rootNode, 'mode' => 'simplexml')); $xsz->serialize($vals); $xml = $xsz->getSerializedData(); Nimble::log($xml, PEAR_LOG_DEBUG); return $xml; }
/** * respond to a given request format directly, using a specific function/method * - instead of the default Controller->render() action which loads a view. * * Would be nice to use a lambda function with a closure here, so we could pass the controller * itself to the function at runtime. But for php<5.3 have to settle for this. * * example call, in a controller action: * $this->respond_to('xml', array('XMLview', 'build'), array('loginResult', $this->result)); * # pre-supposes that xml_view plugin is loaded via Nimble::plugins('xml_view') * * @param $format string format we are expecting to respond to (xml, json, etc) * @param $func mixed function name, reference or array suitable for call_user_func_array() * @param $args array opt arguments to be passed to the function * @throws NimbleException */ public function respond_to($format, $func = null, $args = array()) { if ($this->format() == $format) { if (is_callable($func)) { $funcname = is_array($func) ? $func[0] . '::' . $func[1] : (string) $funcname; Nimble::log("responding to {$format} with {$funcname}", NIMBLE_LOG_DEBUG); echo call_user_func_array($func, $args); $this->has_rendered = true; if (isset($this->headers_by_format[$format])) { $this->headers[] = $this->headers_by_format[$format]; } } else { throw new NimbleException('Uncallable function given to respond_to()'); } } }
/** * Match the HTTP request's URL and HTTP method against the stored routes and, if a match is found, call the appropriate controller's method. * If the client you're using doesn't support sending HTTP requests with methods * other than GET or POST, set $_POST['_method'] to the actual HTTP method you wish to use. */ public function dispatch($test = false) { $this->load_plugins(); foreach ($this->routes as $rule => $conf) { // if a vaild _method is passed in a post set it to the REQUEST_METHOD so that we can route for DELETE and PUT methods if (isset($_POST['_method']) && !empty($_POST['_method']) && in_array(strtoupper($_POST['_method']), Route::$allowed_methods)) { $_SERVER['REQUEST_METHOD'] = strtoupper($_POST['_method']); } if (empty($_SERVER['REQUEST_METHOD'])) { if (!$test) { throw new NimbleException('No valid Request Method given.'); } else { return false; } } /** test to see if its a valid route */ if (preg_match($conf[0], $this->url, $matches) && (empty($conf[3]) || $_SERVER['REQUEST_METHOD'] == $conf[3])) { /** Only declared variables in URL regex */ $matches = $this->parse_urls_args($matches); $this->klass = new $conf[1](); /** set the layout tempalte to the default */ $this->klass->set_layout_template(); $this->klass->format = $conf[4] ? array_pop($matches) : $this->klass->default_format; Nimble::log("dispatching '/{$this->url}' as {$conf[1]}::{$conf[2]} {$conf[3]} {$this->klass->format}", NIMBLE_LOG_INFO); ob_start(); // call before filters call_user_func(array($this->klass, "run_before_filters"), $conf[2]); // call methods on controller class call_user_func_array(array($this->klass, $conf[2]), array($matches)); if (!$this->klass->has_rendered && isset($this->config['view_path'])) { $dir = str_replace('Controller', '', $conf[1]); $dir = strtolower(Inflector::underscore($dir)); $tpl = empty($this->klass->template) ? $conf[2] : $this->klass->template; $view = FileUtils::join($dir, $tpl); $this->klass->render($view); } // call after filters call_user_func(array($this->klass, "run_after_filters"), $conf[2]); $out = ob_get_clean(); if (count($this->klass->headers) > 0) { foreach (array_values($this->klass->headers) as $header) { if (!$this->test_mode) { header($header[0], true, empty($header[1]) ? null : $header[1]); } } } print $out; if (!$test) { exit; } } } if (!$test) { if (class_exists('r404')) { if (method_exists('r404', $_SERVER['REQUEST_METHOD'])) { call_user_func(array('r404', $_SERVER['REQUEST_METHOD'])); } else { call_user_func(array('r404', 'renderError')); } } else { throw new NimbleException("Routing Error. No suitable routes found and no 404 handler exists."); } } }