/**
  * @param ApiResource $apiResource
  * @param ContainerBuilder $container
  */
 protected function setupEntity(ApiResource $apiResource, ContainerBuilder $container)
 {
     $apiResourceServiceName = $apiResource->getResourceServiceName();
     $formHandlerServiceName = $apiResource->getFormHandlerServiceName();
     $entityHandlerServiceName = $apiResource->getEntityHandlerServiceName();
     $controllerServiceName = $apiResource->getControllerServiceName();
     $filterServiceName = $apiResource->getFilterServiceName();
     $filter = $apiResource->getFilterClass();
     // Filter
     if ($container->hasDefinition($filter)) {
         $filterDefinition = $container->getDefinition($filter);
         $container->setDefinition($filterServiceName, $filterDefinition);
     } elseif (class_exists($filter)) {
         $filterDefinition = new Definition($filter);
         $container->setDefinition($filterServiceName, $filterDefinition);
     }
     // Form Handler
     $formHandler = new Definition(FormHandler::class);
     $formHandler->addArgument(new Reference('doctrine.orm.entity_manager'));
     $formHandler->addArgument(new Reference('form.factory'));
     $formHandler->addArgument($this->getFormType($apiResource));
     $container->setDefinition($formHandlerServiceName, $formHandler);
     // Handler
     $entityHandler = new Definition(BaseHandler::class);
     $entityHandler->addArgument(new Reference('doctrine.orm.entity_manager'));
     $entityHandler->addArgument(new Reference($apiResourceServiceName));
     $entityHandler->addArgument(new Reference($formHandlerServiceName));
     if ($filter) {
         $entityHandler->addArgument(new Reference($filterServiceName));
     }
     $container->setDefinition($entityHandlerServiceName, $entityHandler);
     // Controller
     $controller = new Definition(RestApiController::class);
     $controller->addMethodCall('setContainer', [new Reference('service_container')]);
     $controller->addMethodCall('setHandler', [new Reference($entityHandlerServiceName)]);
     $container->setDefinition($controllerServiceName, $controller);
 }
 /**
  * @param ApiDoc $annotation
  * @param ApiResource $resource
  */
 protected function addFilter(ApiDoc $annotation, ApiResource $resource)
 {
     $filterClass = $resource->getFilterClass();
     if (class_exists($filterClass)) {
         $filter = new $filterClass();
     } elseif ($this->container->has($filterClass)) {
         $filter = $this->container->get($filterClass);
     } else {
         $filter = null;
     }
     /** @var FilterInterface $filter */
     if ($filter) {
         $builder = $this->container->has('bitecodes.doctrine_filter_builder') ? $this->container->get('bitecodes.doctrine_filter_builder') : new FilterBuilder();
         $filter->buildFilter($builder);
         foreach ($builder->getFilters() as $filterElement) {
             $name = $filterElement->getName();
             $options = $filterElement->getOptions();
             $description = !empty($options['description']) ? $options['description'] : '';
             $requirement = !empty($options['requirement']) ? $options['requirement'] : '';
             $annotation->addFilter($name, compact('description', 'requirement'));
         }
     }
 }