/** * {@inheritdoc} */ public function hydrate(Jarvis $app) { $app['annoReader'] = function ($app) { $cache = null; if (isset($app['doctrine.cache'])) { $cache = new DoctrineCache($app['doctrine.cache']); } return new Reader(new Parser(), $cache ?: new ArrayCache()); }; $app->lock('annoReader'); $app->addReceiver(JarvisEvents::CONTROLLER_EVENT, function (ControllerEvent $event) use($app) { $app->request->attributes->add((array) $event->arguments()); if (!is_array($event->callback())) { return; } list($controller, $action) = $event->callback(); $annotations = array_merge($app->annoReader->getClassAnnotations($controller)->toArray(), $app->annoReader->getMethodAnnotations($controller, $action)->toArray()); $handlers = $app->find(self::ANNO_SERVICE_HANDLER_BASE_ID . '*'); foreach ($annotations as $annotation) { foreach ($handlers as $handler) { if ($handler instanceof AnnotationHandlerInterface && $handler->supports($annotation)) { $handler->handle($annotation); } } } $arguments = []; $reflectionMethod = new \ReflectionMethod($controller, $action); foreach ($reflectionMethod->getParameters() as $reflectionParam) { if ($app->request->attributes->has($name = $reflectionParam->getName())) { $arguments[$name] = $app->request->attributes->get($name); } } $event->setArguments($arguments); }); }
/** * {@inheritdoc} */ public function hydrate(Jarvis $jarvis) { $jarvis['annotation.handler.rest.pagination'] = function ($jarvis) { return new PaginationHandler($jarvis->request); }; $jarvis['annotation.handler.rest.sort'] = function ($jarvis) { return new SortHandler($jarvis->request); }; $jarvis['annotation.handler.rest.criteria'] = function ($jarvis) { return new CriteriaHandler($jarvis->request); }; $jarvis->addReceiver(JarvisEvents::EXCEPTION_EVENT, function (ExceptionEvent $event) { $exception = $event->getException(); if (!$exception instanceof RestHttpException) { return; } $event->setResponse(new JsonResponse(['reason' => $exception->getMessage()], 0 === $exception->getCode() ? Response::HTTP_INTERNAL_SERVER_ERROR : $exception->getCode())); }, Jarvis::RECEIVER_HIGH_PRIORITY); }