Example #1
0
 /**
  * Executes a request.
  *
  * @param request Request to execute.
  *
  * @return Response Response to send.
  */
 public function execute(Request $request)
 {
     try {
         $segments = $request->getPathSegments();
         if (count($segments) > 0) {
             $firstSegment = $segments[0];
             if (array_key_exists($firstSegment, $this->controllers)) {
                 $controller = $this->controllers[$firstSegment];
                 $controllerRequest = $request->consume();
                 $resource = $controller->getResource($controllerRequest);
                 if (isset($resource)) {
                     $verb = $request->getVerb();
                     if (method_exists($resource, $verb)) {
                         return $resource->{$verb}($controllerRequest);
                     } else {
                         return Response::notSupported();
                     }
                 }
             }
         }
         return Response::notFound();
     } catch (\Exception $e) {
         return Response::error($e->getMessage());
     }
 }
 public function handle(Request $request, $db)
 {
     // only GET is implemented so far
     if ($request->getVerb() == 'GET') {
         return $this->getAction($request, $db);
     }
     return false;
 }
 public function handle(Request $request, $db)
 {
     if ($request->getVerb() == 'GET') {
         return $this->getAction($request, $db);
     } else {
         throw new Exception("method not supported");
     }
     return false;
 }
 public function handle(Request $request, $db)
 {
     $this->oauthModel = $request->getOauthModel($db);
     // only POST is implemented so far
     if ($request->getVerb() == 'POST') {
         return $this->postAction($request, $db);
     }
     return false;
 }
 public function handle(Request $request, $db)
 {
     // only GET is implemented so far
     if ($request->getVerb() == 'GET') {
         return $this->getAction($request, $db);
     } elseif ($request->getVerb() == 'POST') {
         return $this->postAction($request, $db);
     } elseif ($request->getVerb() == 'DELETE') {
         return $this->deleteAction($request, $db);
     } elseif ($request->getVerb() == 'PUT') {
         return $this->putAction($request, $db);
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function getRoute(Request $request)
 {
     $badMethod = false;
     foreach ($this->rules as $rule) {
         if (preg_match('%^/v' . $this->version . $rule['path'] . '%', $request->getPathInfo(), $matches)) {
             if (isset($rule['verbs']) && !in_array($request->getVerb(), $rule['verbs'])) {
                 $badMethod = true;
                 continue;
             }
             // Determine numeric keys
             $exclude = array_filter(array_keys($matches), function ($val) {
                 return is_integer($val);
             });
             // Remove numeric keys from matches
             $params = array_diff_key($matches, array_flip($exclude));
             return new Route($rule['controller'], $rule['action'], $params);
         }
     }
     if ($badMethod) {
         throw new Exception('Method not supported', 415);
     }
     throw new Exception('Endpoint not found', 404);
 }
Example #7
0
 /**
  *
  */
 public static function dispatch(Request $request)
 {
     //$url, $verb, $headers, $get_args, $body) {
     $url = $request->getUrl();
     $handler_verb = $verb = $request->getVerb();
     if (!isset(self::$handlers[$verb])) {
         if (!count(self::$handlers[''])) {
             // error -- invalid verb
             $resp = new ResponseMethodNotAllowed('Could not find a handler for the "' . $verb . '" HTTP verb.', $request);
             $resp->render();
             return;
         } else {
             $handler_verb = '';
         }
     }
     if ($handler_verb !== '' && self::real_dispatch($request, $handler_verb)) {
         return;
     }
     // try multi-verb handlers
     if (self::real_dispatch($request, '')) {
         return;
     }
     // could this be dispatched for any other verb?
     if (self::could_dispatch($url)) {
         // yes, so the verb is wrong
         $resp = new ResponseMethodNotAllowed('The HTTP verb "' . $verb . '" cannot be used on the requested URL.', $request);
         $resp->render();
         return;
     }
     // error -- no handler for URL
     $resp = new ResponseNotFound('Could not find a handler for the requested URL.', $request);
     $resp->render();
     return;
 }
 /**
  * Confirms that it is possible to set arbitrary HTTP verbs.
  */
 public function testVerbs()
 {
     $request = new Request('http://127.0.0.1');
     $this->assertEquals('GET', $request->getVerb());
     $request->setVerb('DELETE');
     $this->assertEquals('DELETE', $request->getVerb());
     /* Setting a payload implicitly sets the HTTP verb to 'POST' unless
        another one has been set. */
     $request = new Request('http://127.0.0.1');
     $args = array('foo' => 'bar');
     $request->setPayload($args);
     $this->assertEquals('POST', $request->getVerb());
     $request->setVerb('PATCH');
     $this->assertEquals('PATCH', $request->getVerb());
     $this->assertEquals($args, $request->getPayload());
     // We can set the verb and the payload in the opposite order too
     $request = new Request('http://127.0.0.1');
     $request->setVerb('BUTT');
     $payload = json_encode($args);
     $request->setPayload($payload);
     $this->assertEquals('BUTT', $request->getVerb());
     $this->assertEquals($payload, $request->getPayload());
     /* But if we try to force the verb to GET, we'll have a problem when we
        try to get the cURL options, which is the point when conflicts are
        resolved. */
     $request->setVerb('GET');
     $this->assertThrows('LogicException', array($request, 'getCurlOptions'));
 }
Example #9
0
 /**
  * Ensures that a verb can be set on the request with setVerb
  *
  * @param string $verb Verb to set
  *
  * @return void
  *
  * @test
  * @dataProvider methodProvider
  */
 public function setVerbAllowsForSettingRequestVerb($verb)
 {
     $request = new \Request($this->config, []);
     $request->setVerb($verb);
     $this->assertEquals($verb, $request->getVerb());
 }