diff --git a/composer.json b/composer.json index 23b500b..4cbdfa8 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "ext-mbstring": "*", "ext-openssl": "*", "guzzlehttp/guzzle": "^7.9.2", + "psr/log": "^2.0|^3.0", "spomky-labs/base64url": "^2.0.4", "symfony/polyfill-php83": "^1.33", "web-token/jwt-library": "^3.4.9|^4.0.6" diff --git a/src/Utils.php b/src/Utils.php index 195f585..ab6c2ba 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -13,6 +13,7 @@ use Base64Url\Base64Url; use Jose\Component\Core\JWK; use Jose\Component\Core\Util\Ecc\PublicKey; +use Psr\Log\LoggerInterface; class Utils { @@ -55,17 +56,31 @@ public static function unserializePublicKey(string $data): array ]; } + private static function logWarning(string $message, ?LoggerInterface $logger): void + { + $logger !== null + ? $logger->warning($message) + : trigger_error($message, E_USER_WARNING); + } + + private static function logNotice(string $message, ?LoggerInterface $logger): void + { + $logger !== null + ? $logger->notice($message) + : trigger_error($message, E_USER_NOTICE); + } + /** * Generates user warning/notice if some requirements are not met. * Does not throw exception to allow unusual or polyfill environments. */ - public static function checkRequirement(): void + public static function checkRequirement(?LoggerInterface $logger = null): void { - self::checkRequirementExtension(); - self::checkRequirementKeyCipherHash(); + self::checkRequirementExtension($logger); + self::checkRequirementKeyCipherHash($logger); } - public static function checkRequirementExtension(): void + public static function checkRequirementExtension(?LoggerInterface $logger = null): void { $requiredExtensions = [ 'curl' => '[WebPush] curl extension is not loaded but is required. You can fix this in your php.ini.', @@ -74,17 +89,17 @@ public static function checkRequirementExtension(): void ]; foreach ($requiredExtensions as $extension => $message) { if (!extension_loaded($extension)) { - trigger_error($message, E_USER_WARNING); + self::logWarning($message, $logger); } } // Check optional extensions. if (!extension_loaded('bcmath') && !extension_loaded('gmp')) { - trigger_error('It is highly recommended to install the GMP or BCMath extension to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.', E_USER_NOTICE); + self::logNotice('It is highly recommended to install the GMP or BCMath extension to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.', $logger); } } - public static function checkRequirementKeyCipherHash(): void + public static function checkRequirementKeyCipherHash(?LoggerInterface $logger = null): void { // Print your current openssl version with: OPENSSL_VERSION_TEXT // Check for outdated openssl without EC support. @@ -93,11 +108,11 @@ public static function checkRequirementKeyCipherHash(): void ]; $availableCurves = openssl_get_curve_names(); if ($availableCurves === false) { - trigger_error('[WebPush] Openssl does not support curves.', E_USER_WARNING); + self::logWarning('[WebPush] Openssl does not support curves.', $logger); } else { foreach ($requiredCurves as $curve => $message) { if (!in_array($curve, $availableCurves, true)) { - trigger_error($message, E_USER_WARNING); + self::logWarning($message, $logger); } } } @@ -109,7 +124,7 @@ public static function checkRequirementKeyCipherHash(): void $availableCiphers = openssl_get_cipher_methods(); foreach ($requiredCiphers as $cipher => $message) { if (!in_array($cipher, $availableCiphers, true)) { - trigger_error($message, E_USER_WARNING); + self::logWarning($message, $logger); } } @@ -120,7 +135,7 @@ public static function checkRequirementKeyCipherHash(): void $availableHash = hash_hmac_algos(); foreach ($requiredHash as $hash => $message) { if (!in_array($hash, $availableHash, true)) { - trigger_error($message, E_USER_WARNING); + self::logWarning($message, $logger); } } } diff --git a/src/WebPush.php b/src/WebPush.php index 2f338e4..0fdb794 100644 --- a/src/WebPush.php +++ b/src/WebPush.php @@ -18,11 +18,13 @@ use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Log\LoggerInterface; class WebPush { protected Client $client; protected array $auth; + protected ?LoggerInterface $logger; /** * @var null|array Array of array of Notifications @@ -52,15 +54,23 @@ class WebPush /** * WebPush constructor. * - * @param array $auth Some servers need authentication - * @param array $defaultOptions TTL, urgency, topic, batchSize, requestConcurrency - * @param int|null $timeout Timeout of POST request + * @param array $auth Some servers need authentication + * @param array $defaultOptions TTL, urgency, topic, batchSize, requestConcurrency + * @param int|null $timeout Timeout of POST request + * @param LoggerInterface|null $logger Optional PSR-3 logger; if provided, replaces trigger_error() calls * * @throws \ErrorException */ - public function __construct(array $auth = [], array $defaultOptions = [], ?int $timeout = 30, array $clientOptions = []) - { - Utils::checkRequirement(); + public function __construct( + array $auth = [], + array $defaultOptions = [], + ?int $timeout = 30, + array $clientOptions = [], + ?LoggerInterface $logger = null + ) { + $this->logger = $logger; + + Utils::checkRequirement($this->logger); if (isset($auth['VAPID'])) { $auth['VAPID'] = VAPID::validate($auth['VAPID']);