/** * Fetch all routes of the given api type from config files and * register it. */ public function registerRoutes() { $resources = $this->app['config']['routes']; foreach ($resources as $resourceKey => $resource) { $class = "{$resource}_Api_Route@getRoutes"; $this->app->call($class, [$this->app['api.router']]); } return $this; }
/** * Validate a request. * * @param \Symfony\Component\HttpFoundation\Request $request * * @return bool */ public function validateRequest($request) { #TODO Need to Add more validation $passed = true; foreach ($this->validators as $validator) { $validator = $this->container->make($validator); if ($validator instanceof Validator && $validator->validate($request)) { $passed = true; } } // The accept validator will always be run once any of the previous validators have // been run. This ensures that we only run the accept validator once we know we // have a request that is targetting the API. if ($passed) { $this->container->make('Sunel\\Api\\Http\\Validation\\Accept')->validate($request); } return $passed; }
/** * Get the route throttle. * * @return string|\Dingo\Api\Http\RateLimit\Throttle\Throttle */ public function getThrottle() { if (is_null($this->throttle)) { $this->throttle = array_pull($this->action, 'throttle', []); $this->findControllerOptions('throttles', function ($value) { $this->throttle = $value['throttle']; }); if (is_string($this->throttle)) { $this->throttle = $this->container->make($this->throttle); } } return $this->throttle; }
/** * Register the middleware. * * @return void */ protected function registerMiddleware() { $this->app->singleton('api.response', function ($app) { return new Response(); }); $this->app->singleton('Api\\RequestHandler', function ($app) { return new RequestHandler($app, $app['api.exception'], $app['api.router'], $app['api.http.validator']); }); $this->app->singleton('Sunel\\Api\\Http\\Middleware\\Auth', function ($app) { return new Middleware\Auth($app['api.router'], $app['api.auth']); }); $this->app->singleton('Sunel\\Api\\Http\\Middleware\\Tokenized', function ($app) { return new Middleware\Tokenized(new TokenParser()); }); }
/** * Dispatch a request. * * @param \Sunel\Api\Http\Request $request * * @throws \Exception * * @return \Sunel\Api\Http\Response */ public function dispatch(Request $request) { $this->currentRoute = null; $accept = $this->accept->parse($request); $this->container->instance('request', $request); $this->routesDispatched++; try { if (!isset($this->routes[$accept['version']])) { throw new UnknownVersionException(); } $routes = $this->routes[$accept['version']]; $this->container->setDispatcher(new GroupCountBased($routes->getData())); $this->normalizeRequestUri($request); $response = $this->container->dispatch($request); } catch (\Exception $exception) { $response = $this->exception->handle($exception); } return $this->prepareResponse($response, $request, $accept['format']); }