/** * @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; }
/** * @test * @profile */ public function testDispatch() { split_time('reset'); Rest_Test_Unit_Case_Resource_Foo::serve('resource/foo'); split_time('Invoke Rest_Test_Unit_Case_Resource_Foo::serve(resource/foo)'); Http_Scriptlet_Context::push(new Http_Scriptlet_Context(Environment::uriComponents())); split_time('Initialize Components\\Http_Scriptlet_Context'); $uri = Uri::valueOf(Environment::uriComponents('rest', 'resource', 'foo', 'poke', '1234.json')); split_time("Invoke Uri::valueOf({$uri})"); ob_start(); split_time('reset'); Http_Scriptlet_Context::current()->dispatch($uri, Http_Scriptlet_Request::METHOD_GET); split_time("Invoke Components\\Http_Scriptlet_Context\$dispatch([{$uri}], GET)"); $result = ob_get_clean(); assertEquals(json_encode(true), $result); split_time('reset'); $uri = Uri::valueOf(Environment::uriComponents('rest', 'resource', 'foo', 'poke', '1234.json')); $uri->setQueryParam('log', 'false'); split_time("Invoke Uri::valueOf({$uri})"); ob_start(); split_time('reset'); Http_Scriptlet_Context::current()->dispatch($uri, Http_Scriptlet_Request::METHOD_GET); split_time("Invoke Components\\Http_Scriptlet_Context\$dispatch([{$uri}], GET)"); $result = ob_get_clean(); assertEquals(json_encode(false), $result); }
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); } }
/** * @param \Components\Http_Scriptlet_Context $context_ * @param \Components\Uri $uri_ */ public static function dispatch(Http_Scriptlet_Context $context_, Uri $uri_) { $uri = $context_->getRequest()->getUri(); $extension = $uri->getFileExtension(); $name = $uri->getFilename(true); $uri_->popPathParam(); $pathTarget = Environment::pathWeb() . $uri_->getPath() . '/' . \str\decodeUrl($name, true) . ".{$extension}"; if (false === is_file($pathTarget)) { $fileTarget = Io::file($pathTarget); $directoryTarget = $fileTarget->getDirectory(); if (false === $directoryTarget->exists()) { $directoryTarget->create(); } $info = @json_decode(\str\decodeBase64Url($name)); if (false === isset($info[0])) { throw new Http_Exception('media/scriptlet/engine', sprintf('Not found [%s].', $uri), Http_Exception::NOT_FOUND); } $pathSource = Environment::pathWeb($info[0]); if (false === is_file($pathSource)) { throw new Http_Exception('media/scriptlet/engine', sprintf('Not found [%s].', $uri), Http_Exception::NOT_FOUND); } if (isset($info[1]) || isset($info[2])) { if (!isset($info[1])) { $info[1] = 0; } if (!isset($info[2])) { $info[2] = 0; } Io::image($pathSource)->scale(Point::of($info[1], $info[2]))->saveAs($fileTarget); } else { Io::file($pathSource)->copy($fileTarget); } } header('Content-Length: ' . Io::fileSize($pathTarget)->bytes()); readfile($pathTarget); }
/** * @param \Components\Http_Scriptlet_Context $context_ * @param \Components\Uri $uri_ */ public static function dispatch(Http_Scriptlet_Context $context_, Uri $uri_) { if (__CLASS__ === get_called_class()) { $segments = $uri_->getPathParams(true); $count = count($segments); $params = []; for ($i = $count; 0 < $i; $i--) { $path = implode('/', $segments); foreach (self::$m_routes as $pattern => $scriptlet) { $matches = []; if (1 === preg_match($pattern, $path, $matches)) { $uri_->setPathParams($params); foreach ($segments as $segment) { $context_->getContextUri()->pushPathParam($segment); } $scriptlet::dispatch($context_, $uri_); return; } } array_unshift($params, array_pop($segments)); } if (null !== ($scriptlet = self::$m_default)) { $scriptlet::dispatch($context_, $uri_); return; } } else { $scriptlet = new static(); $scriptlet->request = $context_->getRequest(); $scriptlet->response = $context_->getResponse(); $method = $scriptlet->request->getMethod(); if (method_exists($scriptlet, strtolower($method))) { return $scriptlet->{$method}(); } } throw new Http_Exception('http/scriptlet', null, Http_Exception::NOT_FOUND); }