|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Frosh\Tools\Components\Health\Checker\PerformanceChecker; |
| 6 | + |
| 7 | +use Frosh\Tools\Components\Health\Checker\CheckerInterface; |
| 8 | +use Frosh\Tools\Components\Health\HealthCollection; |
| 9 | +use Frosh\Tools\Components\Health\SettingsResult; |
| 10 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 11 | + |
| 12 | +class MessengerAutoSetupChecker implements PerformanceCheckerInterface, CheckerInterface |
| 13 | +{ |
| 14 | + public function __construct( |
| 15 | + #[Autowire(param: 'env(MESSENGER_TRANSPORT_DSN)')] |
| 16 | + private readonly string $messageTransportDsn, |
| 17 | + #[Autowire(param: 'env(MESSENGER_TRANSPORT_LOW_PRIORITY_DSN)')] |
| 18 | + private readonly string $messageTransportDsnLowPriority, |
| 19 | + #[Autowire(param: 'env(MESSENGER_TRANSPORT_FAILURE_DSN)')] |
| 20 | + private readonly string $messageTransportDsnFailure, |
| 21 | + ) {} |
| 22 | + |
| 23 | + public function collect(HealthCollection $collection): void |
| 24 | + { |
| 25 | + if ($this->isAutoSetupEnabled($this->messageTransportDsn) || $this->isAutoSetupEnabled($this->messageTransportDsnLowPriority) || $this->isAutoSetupEnabled($this->messageTransportDsnFailure)) { |
| 26 | + $collection->add( |
| 27 | + SettingsResult::info( |
| 28 | + 'messenger-auto-setup', |
| 29 | + 'Messenger auto_setup', |
| 30 | + 'enabled', |
| 31 | + 'disabled', |
| 32 | + 'https://developer.shopware.com/docs/guides/hosting/performance/performance-tweaks.html#disable-auto-setup', |
| 33 | + ), |
| 34 | + ); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private function isAutoSetupEnabled(string $messageTransportDsn): bool |
| 39 | + { |
| 40 | + $queryParams = \parse_url($messageTransportDsn, \PHP_URL_QUERY); |
| 41 | + |
| 42 | + // Messenger DSN is invalid. Therefore, we can't really check |
| 43 | + if ($queryParams === false) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if ($queryParams === null) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + $query = []; |
| 52 | + \parse_str($queryParams, $query); |
| 53 | + |
| 54 | + $query += ['auto_setup' => true]; |
| 55 | + |
| 56 | + return filter_var($query['auto_setup'], \FILTER_VALIDATE_BOOL); |
| 57 | + } |
| 58 | +} |
0 commit comments