/** * @param \watoki\deli\Request $request * @return \watoki\deli\Request|null */ public function matches(Request $request) { if (!$this->path->getElements()) { return $request; } $target = $request->getTarget()->getElements(); $elements = $this->path->getElements(); for ($i = 0; $i < count($elements); $i++) { $p = $elements[$i]; if ($i >= count($target)) { return null; } else { if (substr($p, 0, 1) == '{' && substr($p, -1) == '}') { $key = substr($p, 1, -1); $value = $target[$i]; $request = $request->withArgument($key, $value); } else { if ($target[$i] != $p) { return null; } } } } return $request->withContext($request->getContext()->appendedAll(array_slice($target, 0, $i - 1)))->withTarget(new Path(array_slice($target, $i - 1))); }
protected function findIndexNode(Request $request, Path $currentContext) { if ($currentContext->isEmpty()) { return null; } return parent::findIndexNode($request, $currentContext); }
/** * @param Request|WebRequest $request * @return \watoki\curir\delivery\WebResponse */ public function respond(Request $request) { $this->app->prepare($request); if (!$this->isContainerTarget($request)) { $request = $request->withTarget(Path::fromString('execute'))->withArgument(ExecuteResource::ACTION_ARG, $request->getTarget()->toString()); } return parent::respond($request); }
public static function quickRoot($rootDirectory, $defaultPath = 'index', $namespace = '', Factory $factory = null) { $factory = $factory ?: self::init(); $router = new WebRouter($factory, $rootDirectory, $namespace); $router->setDefaultTarget(CallbackTarget::factory(function (WebRequest $request) use($router, $defaultPath) { return $router->route($request->withTarget(Path::fromString($defaultPath)))->respond(); })); self::quickRoute($router, $factory); }
private function createTargetFromClass($fullClassName, Request $request, Path $context) { $object = $this->factory->getInstance($fullClassName); $nextRequest = $request->withContext($request->getContext()->appendedAll($context->getElements())); $nextRequest = $nextRequest->withTarget(new Path(array_slice($request->getTarget()->getElements(), count($context->getElements())))); if ($object instanceof Responding) { return new RespondingTarget($nextRequest, $object); } else { return new ObjectTarget($nextRequest, $object, $this->factory); } }
public function whenIGet_From($path, $resourceClass) { $request = $this->request->withTarget(Path::fromString($path))->withMethod('get'); $stub = new TestDelivererStub($request); $router = new NoneRouter(RespondingTarget::factory($this->factory, $this->factory->getInstance($resourceClass))); $delivery = new WebDelivery($router, $stub, $stub); $stub->onDeliver(function (WebResponse $response) { if ($response instanceof ErrorResponse) { throw $response->getException(); } $this->model = $response->getBody(); }); $delivery->run(); }
private function wrapAsset(Element $element, $attributeName) { $attribute = $element->getAttribute($attributeName); if (!$attribute) { return; } $url = Url::fromString($attribute->getValue()); if ($url->isAbsolute()) { return; } $path = $url->getPath(); $path->insertAll($this->path->slice(0, -1), 0); $url->setPath($path); $element->setAttribute($attributeName, $url->toString()); }
public function dispatch(WebRequest $request, Router $router) { $arguments = $request->getArguments(); $request->setTarget($this->target); if ($arguments->isEmpty()) { $arguments->merge($this->arguments); $request->setMethod(WebRequest::METHOD_GET); } else { if ($arguments->has(self::$TARGET_KEY)) { $request->setTarget(Path::fromString($arguments->get(self::$TARGET_KEY))); } if ($arguments->has(WebRequest::$METHOD_KEY)) { $request->setMethod($arguments->get(WebRequest::$METHOD_KEY)); } else { $request->setMethod(WebRequest::METHOD_GET); } } $this->response = $router->route($request)->respond(); if ($this->response->getHeaders()->has(WebResponse::HEADER_LOCATION)) { throw new WrappedRedirection($this->response->getHeaders()->get(WebResponse::HEADER_LOCATION)); } return $request; }
public function toString() { $queries = array(); foreach ($this->flattenParams($this->parameters) as $key => $value) { $queries[] = $key . '=' . urlencode($value); } $port = $this->port ? self::PORT_SEPARATOR . $this->port : ''; $scheme = $this->scheme ? $this->scheme . self::SCHEME_SEPARATOR : ''; $server = $this->host && $this->isAbsolute() ? $scheme . self::HOST_PREFIX . $this->host . $port : ''; return $server . parent::toString() . ($queries ? self::QUERY_STRING_SEPARATOR . implode('&', $queries) : '') . ($this->fragment ? self::FRAGMENT_SEPARATOR . $this->fragment : ''); }
/** * @param string $path * @param array $args * @return Box */ protected function box($path, $args = array()) { return new Box(Path::fromString($path), new Map($args)); }
public function givenTheRequestTarget_Is($string) { $this->request->setTarget(Path::fromString($string)); }
/** * @param $path */ public function whenIRoute($path) { $this->target = $this->router->route(new Request(new Path(), Path::fromString($path))); }
public function givenTheRequestHasTheTarget($pathString) { $this->request = $this->request->withTarget(Path::fromString($pathString)); }
private function parseExtension(Path $target) { $extension = null; $elements = $target->getElements(); if (count($elements) && strpos($elements[count($elements) - 1], '.')) { $parts = explode('.', array_pop($elements)); $extension = array_pop($parts); $elements[] = implode('.', $parts); } return array(new Path($elements), $extension); }
public function toString() { return json_encode(array('context' => $this->context->toString(), 'target' => $this->target->toString(), 'method' => $this->method, 'arguments' => $this->arguments->toArray())); }
/** * @param Request|WebRequest $request * @return \watoki\curir\delivery\WebResponse */ public function respond(Request $request) { $request = $request->withTarget(Path::fromString('execute'))->withArgument(ExecuteResource::ACTION_ARG, $request->getTarget()->toString()); return parent::respond($request); }
<?php use spec\watoki\deli\fixtures\TestDelivererStub; use watoki\deli\Delivery; use watoki\deli\Path; use watoki\deli\Request; use watoki\deli\router\NoneRouter; use watoki\deli\target\CallbackTarget; require_once __DIR__ . '/../../vendor/autoload.php'; error_reporting(0); $router = new NoneRouter(CallbackTarget::factory(function () { /** @noinspection PhpUndefinedFunctionInspection */ causeFatalError(); })); $test = new TestDelivererStub(new Request(new Path(), Path::fromString('some/target'))); $test->onDeliver(function ($response) { echo $response; }); $delivery = new Delivery($router, $test, $test); $delivery->run();
protected function determineTarget($server) { list(, $target) = $this->splitContextAndTarget($server); return Path::fromString(ltrim(urldecode($target), '/')); }
public function addPath($pathString, TargetFactory $factory) { $this->add(new PathMatcher(Path::fromString($pathString)), $factory); }
public function givenTheTargetPathIs($string) { $this->environment->target = Path::fromString($string); }