From 9c1db35d812443a15ae3698a3e31bf6c563a7b85 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 22 May 2025 09:20:50 +0200 Subject: [PATCH] Add new MERCURE_ENABLED env var --- CHANGELOG.md | 4 +++- composer.json | 2 +- config/autoload/mercure.global.php | 1 + config/params/shlink_dev_env.php.dist | 1 + module/Core/src/Config/EnvVars.php | 3 +++ .../src/EventDispatcher/Helper/EnabledListenerChecker.php | 2 +- .../src/ShortUrl/Helper/ShortUrlTitleResolutionHelper.php | 2 +- .../EventDispatcher/Helper/EnabledListenerCheckerTest.php | 2 +- 8 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f791119..078a9835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ## [Unreleased] ### Added -* *Nothing* +* [#2438](https://github.com/shlinkio/shlink/issues/2438) Add `MERCURE_ENABLED` env var and corresponding config option, to more easily allow the mercure integration to be toggled. + + For BC, if this env vars is not present, we'll still consider the integration enabled if the `MERCURE_PUBLIC_HUB_URL` env var has a value. This is considered deprecated though, and next major version will rely only on `MERCURE_ENABLED`, so if you are using Mercure, make sure to set `MERCURE_ENABLED=true` to be ready. ### Changed * [#2406](https://github.com/shlinkio/shlink/issues/2406) Remove references to bootstrap from error templates, and instead inline the very minimum required styles. diff --git a/composer.json b/composer.json index e3548394..af4b04fd 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "pagerfanta/core": "^3.8", "ramsey/uuid": "^4.7", "shlinkio/doctrine-specification": "^2.2", - "shlinkio/shlink-common": "dev-main#e601317 as 7.1", + "shlinkio/shlink-common": "dev-main#7469270 as 7.1", "shlinkio/shlink-config": "^4.0", "shlinkio/shlink-event-dispatcher": "^4.2", "shlinkio/shlink-importer": "^5.6", diff --git a/config/autoload/mercure.global.php b/config/autoload/mercure.global.php index 83abed1a..55e8c5f8 100644 --- a/config/autoload/mercure.global.php +++ b/config/autoload/mercure.global.php @@ -12,6 +12,7 @@ return [ // This config is used by shlink-common. Do not delete 'mercure' => [ + 'enabled' => EnvVars::MERCURE_ENABLED->loadFromEnv(), 'public_hub_url' => EnvVars::MERCURE_PUBLIC_HUB_URL->loadFromEnv(), 'internal_hub_url' => EnvVars::MERCURE_INTERNAL_HUB_URL->loadFromEnv(), 'jwt_secret' => EnvVars::MERCURE_JWT_SECRET->loadFromEnv(), diff --git a/config/params/shlink_dev_env.php.dist b/config/params/shlink_dev_env.php.dist index 2a3905dc..d9de7022 100644 --- a/config/params/shlink_dev_env.php.dist +++ b/config/params/shlink_dev_env.php.dist @@ -58,6 +58,7 @@ return [ // EnvVars::MATOMO_API_TOKEN->value => , // Mercure + EnvVars::MERCURE_ENABLED->value => true, EnvVars::MERCURE_PUBLIC_HUB_URL->value => 'http://localhost:8002', EnvVars::MERCURE_INTERNAL_HUB_URL->value => 'http://shlink_mercure_proxy', EnvVars::MERCURE_JWT_SECRET->value => 'mercure_jwt_key_long_enough_to_avoid_error', diff --git a/module/Core/src/Config/EnvVars.php b/module/Core/src/Config/EnvVars.php index 2a835932..1f6f2c03 100644 --- a/module/Core/src/Config/EnvVars.php +++ b/module/Core/src/Config/EnvVars.php @@ -42,6 +42,7 @@ enum EnvVars: string case REDIS_SERVERS = 'REDIS_SERVERS'; case REDIS_SENTINEL_SERVICE = 'REDIS_SENTINEL_SERVICE'; case REDIS_PUB_SUB_ENABLED = 'REDIS_PUB_SUB_ENABLED'; + case MERCURE_ENABLED = 'MERCURE_ENABLED'; case MERCURE_PUBLIC_HUB_URL = 'MERCURE_PUBLIC_HUB_URL'; case MERCURE_INTERNAL_HUB_URL = 'MERCURE_INTERNAL_HUB_URL'; case MERCURE_JWT_SECRET = 'MERCURE_JWT_SECRET'; @@ -84,6 +85,7 @@ enum EnvVars: string case MEMORY_LIMIT = 'MEMORY_LIMIT'; case INITIAL_API_KEY = 'INITIAL_API_KEY'; case SKIP_INITIAL_GEOLITE_DOWNLOAD = 'SKIP_INITIAL_GEOLITE_DOWNLOAD'; + /** @deprecated Use REDIRECT_EXTRA_PATH */ case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH'; /** @deprecated */ @@ -159,6 +161,7 @@ enum EnvVars: string }, self::DB_USE_ENCRYPTION => false, + self::MERCURE_ENABLED => self::MERCURE_PUBLIC_HUB_URL->existsInEnv(), self::MERCURE_INTERNAL_HUB_URL => self::MERCURE_PUBLIC_HUB_URL->loadFromEnv(), self::DEFAULT_QR_CODE_SIZE, => DEFAULT_QR_CODE_SIZE, diff --git a/module/Core/src/EventDispatcher/Helper/EnabledListenerChecker.php b/module/Core/src/EventDispatcher/Helper/EnabledListenerChecker.php index c348cbbd..0b1e58f5 100644 --- a/module/Core/src/EventDispatcher/Helper/EnabledListenerChecker.php +++ b/module/Core/src/EventDispatcher/Helper/EnabledListenerChecker.php @@ -34,7 +34,7 @@ readonly class EnabledListenerChecker implements EnabledListenerCheckerInterface EventDispatcher\RedisPubSub\NotifyVisitToRedis::class, EventDispatcher\RedisPubSub\NotifyNewShortUrlToRedis::class => $this->redisPubSubEnabled, EventDispatcher\Mercure\NotifyVisitToMercure::class, - EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => $this->mercureOptions->isEnabled(), + EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => $this->mercureOptions->enabled, EventDispatcher\Matomo\SendVisitToMatomo::class => $this->matomoOptions->enabled, EventDispatcher\UpdateGeoLiteDb::class => $this->geoLiteOptions->hasLicenseKey(), default => false, // Any unknown async listener should not be enabled by default diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlTitleResolutionHelper.php b/module/Core/src/ShortUrl/Helper/ShortUrlTitleResolutionHelper.php index 366e18e2..f870bb87 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlTitleResolutionHelper.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlTitleResolutionHelper.php @@ -123,7 +123,7 @@ readonly class ShortUrlTitleResolutionHelper implements ShortUrlTitleResolutionH private function encodeToUtf8WithMbString(string $titleInOriginalEncoding, string $pageCharset): string|null { try { - return mb_convert_encoding($titleInOriginalEncoding, 'utf-8', $pageCharset); + return mb_convert_encoding($titleInOriginalEncoding, 'utf-8', $pageCharset) ?: null; } catch (Throwable $e) { $this->logger->warning('It was impossible to encode page title in UTF-8 with mb_convert_encoding. {e}', [ 'e' => $e, diff --git a/module/Core/test/EventDispatcher/Helper/EnabledListenerCheckerTest.php b/module/Core/test/EventDispatcher/Helper/EnabledListenerCheckerTest.php index 180ab48d..9102e482 100644 --- a/module/Core/test/EventDispatcher/Helper/EnabledListenerCheckerTest.php +++ b/module/Core/test/EventDispatcher/Helper/EnabledListenerCheckerTest.php @@ -148,7 +148,7 @@ class EnabledListenerCheckerTest extends TestCase return new EnabledListenerChecker( new RabbitMqOptions(enabled: $rabbitMqEnabled), $redisPubSubEnabled, - new MercureOptions(publicHubUrl: $mercureEnabled ? 'the-url' : null), + new MercureOptions(enabled: $mercureEnabled), new GeoLite2Options(licenseKey: $geoLiteEnabled ? 'the-key' : null), new MatomoOptions(enabled: $matomoEnabled), );