예제 #1
0
파일: PathMatcher.php 프로젝트: watoki/deli
 /**
  * @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)));
 }
예제 #2
0
파일: WebRouter.php 프로젝트: watoki/curir
 protected function findIndexNode(Request $request, Path $currentContext)
 {
     if ($currentContext->isEmpty()) {
         return null;
     }
     return parent::findIndexNode($request, $currentContext);
 }
예제 #3
0
 /**
  * @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);
 }
예제 #4
0
 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);
 }
예제 #5
0
 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);
     }
 }
예제 #6
0
파일: WebFixture.php 프로젝트: jonfm/domin
 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();
 }
예제 #7
0
파일: Wrapper.php 프로젝트: watoki/boxes
 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());
 }
예제 #8
0
파일: Box.php 프로젝트: watoki/boxes
 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;
 }
예제 #9
0
파일: Url.php 프로젝트: watoki/curir
 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 : '');
 }
예제 #10
0
 /**
  * @param string $path
  * @param array $args
  * @return Box
  */
 protected function box($path, $args = array())
 {
     return new Box(Path::fromString($path), new Map($args));
 }
예제 #11
0
파일: BoxFixture.php 프로젝트: watoki/boxes
 public function givenTheRequestTarget_Is($string)
 {
     $this->request->setTarget(Path::fromString($string));
 }
예제 #12
0
 /**
  * @param $path
  */
 public function whenIRoute($path)
 {
     $this->target = $this->router->route(new Request(new Path(), Path::fromString($path)));
 }
예제 #13
0
 public function givenTheRequestHasTheTarget($pathString)
 {
     $this->request = $this->request->withTarget(Path::fromString($pathString));
 }
예제 #14
0
 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);
 }
예제 #15
0
파일: Request.php 프로젝트: watoki/deli
 public function toString()
 {
     return json_encode(array('context' => $this->context->toString(), 'target' => $this->target->toString(), 'method' => $this->method, 'arguments' => $this->arguments->toArray()));
 }
예제 #16
0
 /**
  * @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);
 }
예제 #17
0
파일: fatal.php 프로젝트: watoki/deli
<?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();
예제 #18
0
 protected function determineTarget($server)
 {
     list(, $target) = $this->splitContextAndTarget($server);
     return Path::fromString(ltrim(urldecode($target), '/'));
 }
예제 #19
0
 public function addPath($pathString, TargetFactory $factory)
 {
     $this->add(new PathMatcher(Path::fromString($pathString)), $factory);
 }
예제 #20
0
 public function givenTheTargetPathIs($string)
 {
     $this->environment->target = Path::fromString($string);
 }