Skip to content

Commit c922021

Browse files
authored
Support PSR-3 logger in requirement checks (#451) (#452)
1 parent b8c708c commit c922021

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"ext-mbstring": "*",
3535
"ext-openssl": "*",
3636
"guzzlehttp/guzzle": "^7.9.2",
37+
"psr/log": "^2.0|^3.0",
3738
"spomky-labs/base64url": "^2.0.4",
3839
"symfony/polyfill-php83": "^1.33",
3940
"web-token/jwt-library": "^3.4.9|^4.0.6"

src/Utils.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Base64Url\Base64Url;
1414
use Jose\Component\Core\JWK;
1515
use Jose\Component\Core\Util\Ecc\PublicKey;
16+
use Psr\Log\LoggerInterface;
1617

1718
class Utils
1819
{
@@ -55,17 +56,31 @@ public static function unserializePublicKey(string $data): array
5556
];
5657
}
5758

59+
private static function logWarning(string $message, ?LoggerInterface $logger): void
60+
{
61+
$logger !== null
62+
? $logger->warning($message)
63+
: trigger_error($message, E_USER_WARNING);
64+
}
65+
66+
private static function logNotice(string $message, ?LoggerInterface $logger): void
67+
{
68+
$logger !== null
69+
? $logger->notice($message)
70+
: trigger_error($message, E_USER_NOTICE);
71+
}
72+
5873
/**
5974
* Generates user warning/notice if some requirements are not met.
6075
* Does not throw exception to allow unusual or polyfill environments.
6176
*/
62-
public static function checkRequirement(): void
77+
public static function checkRequirement(?LoggerInterface $logger = null): void
6378
{
64-
self::checkRequirementExtension();
65-
self::checkRequirementKeyCipherHash();
79+
self::checkRequirementExtension($logger);
80+
self::checkRequirementKeyCipherHash($logger);
6681
}
6782

68-
public static function checkRequirementExtension(): void
83+
public static function checkRequirementExtension(?LoggerInterface $logger = null): void
6984
{
7085
$requiredExtensions = [
7186
'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
7489
];
7590
foreach ($requiredExtensions as $extension => $message) {
7691
if (!extension_loaded($extension)) {
77-
trigger_error($message, E_USER_WARNING);
92+
self::logWarning($message, $logger);
7893
}
7994
}
8095

8196
// Check optional extensions.
8297
if (!extension_loaded('bcmath') && !extension_loaded('gmp')) {
83-
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);
98+
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);
8499
}
85100
}
86101

87-
public static function checkRequirementKeyCipherHash(): void
102+
public static function checkRequirementKeyCipherHash(?LoggerInterface $logger = null): void
88103
{
89104
// Print your current openssl version with: OPENSSL_VERSION_TEXT
90105
// Check for outdated openssl without EC support.
@@ -93,11 +108,11 @@ public static function checkRequirementKeyCipherHash(): void
93108
];
94109
$availableCurves = openssl_get_curve_names();
95110
if ($availableCurves === false) {
96-
trigger_error('[WebPush] Openssl does not support curves.', E_USER_WARNING);
111+
self::logWarning('[WebPush] Openssl does not support curves.', $logger);
97112
} else {
98113
foreach ($requiredCurves as $curve => $message) {
99114
if (!in_array($curve, $availableCurves, true)) {
100-
trigger_error($message, E_USER_WARNING);
115+
self::logWarning($message, $logger);
101116
}
102117
}
103118
}
@@ -109,7 +124,7 @@ public static function checkRequirementKeyCipherHash(): void
109124
$availableCiphers = openssl_get_cipher_methods();
110125
foreach ($requiredCiphers as $cipher => $message) {
111126
if (!in_array($cipher, $availableCiphers, true)) {
112-
trigger_error($message, E_USER_WARNING);
127+
self::logWarning($message, $logger);
113128
}
114129
}
115130

@@ -120,7 +135,7 @@ public static function checkRequirementKeyCipherHash(): void
120135
$availableHash = hash_hmac_algos();
121136
foreach ($requiredHash as $hash => $message) {
122137
if (!in_array($hash, $availableHash, true)) {
123-
trigger_error($message, E_USER_WARNING);
138+
self::logWarning($message, $logger);
124139
}
125140
}
126141
}

src/WebPush.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
use GuzzleHttp\Psr7\Request;
1919
use Psr\Http\Message\RequestInterface;
2020
use Psr\Http\Message\ResponseInterface;
21+
use Psr\Log\LoggerInterface;
2122

2223
class WebPush
2324
{
2425
protected Client $client;
2526
protected array $auth;
27+
protected ?LoggerInterface $logger;
2628

2729
/**
2830
* @var null|array Array of array of Notifications
@@ -52,15 +54,23 @@ class WebPush
5254
/**
5355
* WebPush constructor.
5456
*
55-
* @param array $auth Some servers need authentication
56-
* @param array $defaultOptions TTL, urgency, topic, batchSize, requestConcurrency
57-
* @param int|null $timeout Timeout of POST request
57+
* @param array $auth Some servers need authentication
58+
* @param array $defaultOptions TTL, urgency, topic, batchSize, requestConcurrency
59+
* @param int|null $timeout Timeout of POST request
60+
* @param LoggerInterface|null $logger Optional PSR-3 logger; if provided, replaces trigger_error() calls
5861
*
5962
* @throws \ErrorException
6063
*/
61-
public function __construct(array $auth = [], array $defaultOptions = [], ?int $timeout = 30, array $clientOptions = [])
62-
{
63-
Utils::checkRequirement();
64+
public function __construct(
65+
array $auth = [],
66+
array $defaultOptions = [],
67+
?int $timeout = 30,
68+
array $clientOptions = [],
69+
?LoggerInterface $logger = null
70+
) {
71+
$this->logger = $logger;
72+
73+
Utils::checkRequirement($this->logger);
6474

6575
if (isset($auth['VAPID'])) {
6676
$auth['VAPID'] = VAPID::validate($auth['VAPID']);

0 commit comments

Comments
 (0)