Beispiel #1
0
 /**
  * @param RequestContext $requestContext
  * @param InterceptorStack $stack
  * @return Result
  */
 public function intercept(RequestContext $requestContext, InterceptorStack $stack)
 {
     try {
         return $stack->next();
     } catch (Exception $e) {
         error_log("Internal error: {$e}");
         return Results::http('Internal error', 500);
     }
 }
 /**
  * @param RequestContext $requestContext
  * @param InterceptorStack $stack
  * @return Result
  */
 public function intercept(RequestContext $requestContext, InterceptorStack $stack)
 {
     $annotations = Annotations::ofClass($requestContext->controllerMethod->getDeclaringClass(), PublicAccess::class);
     $isPublic = count($annotations) > 0;
     if ($isPublic) {
         return $stack->next();
     }
     if (key_exists('loggedInUser', $_SESSION)) {
         $requestContext->inject('loggedInUser', $_SESSION['loggedInUser']);
         return $stack->next();
     }
     return Results::http('Unauthorized', 401);
 }
Beispiel #3
0
 /**
  * @param RequestContext $requestContext
  * @param array $pathParams
  * @return Result
  * @throws RouterConfigurationException
  */
 public function process(RequestContext $requestContext, array &$pathParams)
 {
     $reflectionMethod = $requestContext->controllerMethod;
     $invokeControllerMethod = function () use($requestContext, $reflectionMethod, &$pathParams) {
         $controller = $this->instantiateController($reflectionMethod, $requestContext);
         $methodArgs = $this->getMethodArgs($reflectionMethod, $pathParams);
         return $reflectionMethod->invokeArgs($controller, $methodArgs);
     };
     $interceptorStack = new InterceptorStack($this->interceptors, $requestContext, $invokeControllerMethod);
     $result = $interceptorStack->next();
     if ($result === null) {
         return Results::nothing();
     }
     if ($result instanceof Result) {
         return $result;
     }
     throw new RouterConfigurationException("Controller method {$requestContext->controllerMethodFullName} should return an instance of Router\\Result");
 }
Beispiel #4
0
 /**
  * @get('path' => '/fake-login')
  * @param string $name
  * @return Result
  */
 public function fakeLogin($name)
 {
     $_SESSION['loggedInUser'] = $name;
     return Results::http("{$name} logged in!");
 }
Beispiel #5
0
 /**
  * @param $requestContext
  * @return Result
  */
 private function handleNotFound($requestContext)
 {
     $notFoundHandler = $this->notFoundHandler;
     $result = null;
     if ($notFoundHandler !== null) {
         $result = $notFoundHandler($requestContext);
     }
     if ($result === null) {
         $result = Results::http("Not Found", 404);
     }
     return $result;
 }