/** * {@inheritDoc} */ public function process(ContainerBuilder $container) { foreach ($container->findTaggedServiceIds('api.error') as $id => $tags) { foreach ($tags as $index => $attributes) { try { if (empty($attributes['handler'])) { throw new \RuntimeException('Missing required attribute "handler"'); } $handlerId = $this->handlerPass->getHandlerId($attributes['handler']); $errorId = $this->handlerPass->getErrors($handlerId); $errorDefinition = $container->getDefinition($errorId); $factoryDefinition = $container->getDefinition($id); $class = $factoryDefinition->getClass(); $class = $container->getParameterBag()->resolveValue($class); $refClass = new \ReflectionClass($class); $requiredInterface = 'FivePercent\\Component\\Error\\ErrorFactoryInterface'; if (!$refClass->implementsInterface($requiredInterface)) { throw new \RuntimeException(sprintf('The error factory should implement "%s" interface.', $requiredInterface)); } $errorDefinition->addMethodCall('addFactory', [new Reference($id)]); } catch (\Exception $e) { throw new \RuntimeException(sprintf('Could not compile error factory for tag index "%d" and service id "%s".', $index, $id), 0, $e); } } } }
/** * Process by annotation * * @param ContainerBuilder $container * @param string $id * @param string $handlerId */ private function processByAnnotation(ContainerBuilder $container, $id, $handlerId) { $serviceLoaderId = $this->handlerPass->getServiceLoader($handlerId); $serviceLoaderDefinition = $container->getDefinition($serviceLoaderId); $loaderClass = $serviceLoaderDefinition->getClass(); $loaderClass = $container->getParameterBag()->resolveValue($loaderClass); $serviceDefinition = $container->getDefinition($id); $serviceClass = $serviceDefinition->getClass(); $serviceClass = $container->getParameterBag()->resolveValue($serviceClass); $refLoaderClass = new \ReflectionClass($loaderClass); $requiredLoaderClass = 'FivePercent\\Bundle\\ApiBundle\\SMD\\Loader\\ServiceAnnotationLoader'; if (!$refLoaderClass->isSubclassOf($requiredLoaderClass) && $refLoaderClass->getName() !== $requiredLoaderClass) { throw new \RuntimeException(sprintf('The service loader for handler "%s" should be implemented of "%s" class.', $handlerId, $requiredLoaderClass)); } $serviceLoaderDefinition->addMethodCall('addService', [$id, $serviceClass]); }