|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\Integration; |
| 6 | + |
| 7 | +use Sentry\Integration\ErrorListenerIntegration; |
| 8 | +use Sentry\Integration\ExceptionListenerIntegration; |
| 9 | +use Sentry\Integration\FatalErrorListenerIntegration; |
| 10 | +use Sentry\Integration\IntegrationInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +final class IntegrationConfigurator |
| 16 | +{ |
| 17 | + private const ERROR_HANDLER_INTEGRATIONS = [ |
| 18 | + ErrorListenerIntegration::class => true, |
| 19 | + ExceptionListenerIntegration::class => true, |
| 20 | + FatalErrorListenerIntegration::class => true, |
| 21 | + ]; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var IntegrationInterface[] |
| 25 | + */ |
| 26 | + private $userIntegrations; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var bool |
| 30 | + */ |
| 31 | + private $registerErrorHandler; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param IntegrationInterface[] $userIntegrations |
| 35 | + */ |
| 36 | + public function __construct(array $userIntegrations, bool $registerErrorHandler) |
| 37 | + { |
| 38 | + $this->userIntegrations = $userIntegrations; |
| 39 | + $this->registerErrorHandler = $registerErrorHandler; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @see IntegrationRegistry::getIntegrationsToSetup() |
| 44 | + * |
| 45 | + * @param IntegrationInterface[] $defaultIntegrations |
| 46 | + * |
| 47 | + * @return IntegrationInterface[] |
| 48 | + */ |
| 49 | + public function __invoke(array $defaultIntegrations): array |
| 50 | + { |
| 51 | + $integrations = []; |
| 52 | + |
| 53 | + $userIntegrationsClasses = array_map('get_class', $this->userIntegrations); |
| 54 | + $pickedIntegrationsClasses = []; |
| 55 | + |
| 56 | + foreach ($defaultIntegrations as $defaultIntegration) { |
| 57 | + $integrationClassName = \get_class($defaultIntegration); |
| 58 | + |
| 59 | + if (!$this->registerErrorHandler && isset(self::ERROR_HANDLER_INTEGRATIONS[$integrationClassName])) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (!\in_array($integrationClassName, $userIntegrationsClasses, true) && !isset($pickedIntegrationsClasses[$integrationClassName])) { |
| 64 | + $integrations[] = $defaultIntegration; |
| 65 | + $pickedIntegrationsClasses[$integrationClassName] = true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($this->userIntegrations as $userIntegration) { |
| 70 | + $integrationClassName = \get_class($userIntegration); |
| 71 | + |
| 72 | + if (!isset($pickedIntegrationsClasses[$integrationClassName])) { |
| 73 | + $integrations[] = $userIntegration; |
| 74 | + $pickedIntegrationsClasses[$integrationClassName] = true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $integrations; |
| 79 | + } |
| 80 | +} |
0 commit comments