/** * @see \Components\Http_Scriptlet::dispatch() dispatch */ public static function dispatch(Http_Scriptlet_Context $context_, Uri $uri_) { $response = $context_->getResponse(); $content = null; try { // FIXME Temporary fix - create a explicit route for ui/scriptlet/embedded. if (Environment::isEmbedded()) { $scriptlet = new Ui_Scriptlet_Embedded(); $scriptlet->request = $context_->getRequest(); $scriptlet->response = $context_->getResponse(); $method = $scriptlet->request->getMethod(); if (false === method_exists($scriptlet, strtolower($method))) { throw new Http_Exception('ui/scriptlet', null, Http_Exception::NOT_FOUND); } $content = $scriptlet->{$method}(); } else { $content = parent::dispatch($context_, $uri_); } } catch (\Exception $e) { Runtime::addException($e); if ($e instanceof Http_Exception) { $e->sendHeader(); } } echo $content; }
public static function dispatch(Http_Scriptlet_Context $context_, Uri $uri_) { if (!($method = $uri_->shiftPathParam())) { throw new Http_Exception('rest/resource', null, Http_Exception::NOT_FOUND); } $resource = get_called_class(); if (null === self::$m_methods || false === isset(self::$m_methods[$resource][$method])) { self::initializeMethods(); if (false === isset(self::$m_methods[$resource][$method])) { throw new Http_Exception('rest/resource', null, Http_Exception::NOT_FOUND); } } $method = self::$m_methods[$resource][$method]; if (false === isset($method['methods'][$context_->getRequest()->getMethod()])) { throw new Http_Exception('rest/resource', null, Http_Exception::NOT_FOUND); } if (isset($method['path']) && count($uri_->getPathParams()) < count($method['path'])) { throw new Http_Exception('rest/resource', null, Http_Exception::NOT_FOUND); } /* @var $resource \Components\Rest_Resource */ $resource = new $resource(); $resource->request = $context_->getRequest(); $resource->response = $context_->getResponse(); $params = []; if (isset($method['path']) || isset($method['query'])) { $marshaller = Object_Marshaller::forMimetype($resource->response->getMimetype()); foreach ($method['path'] as $name => $type) { $params[$name] = $marshaller->unmarshal($uri_->shiftPathParam(), $type); } if (isset($method['query'])) { foreach ($method['query'] as $name => $options) { if ($uri_->hasQueryParam($options['name'])) { $params[$name] = $marshaller->unmarshal($uri_->getQueryParam($options['name']), $options['type']); } else { if ($options['value']) { $params[$name] = $marshaller->unmarshal($options['value'], $options['type']); } else { $params[$name] = null; } } } } } if ($result = call_user_func_array([$resource, $method['name']], $params)) { echo $marshaller->marshal($result); } }