Skip to content

Commit 8cffffb

Browse files
authored
Merge pull request #41 from nicolas-grekas/env-var
Allow configuring behavior via the DOCTRINE_DEPRECATIONS env var
2 parents 523d538 + 3e3dd8c commit 8cffffb

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ Enable Doctrine deprecations to be sent to a PSR3 logger:
1919
```
2020

2121
Enable Doctrine deprecations to be sent as `@trigger_error($message, E_USER_DEPRECATED)`
22-
messages.
22+
messages by setting the `DOCTRINE_DEPRECATIONS` environment variable to `trigger`.
23+
Alternatively, call:
2324

2425
```php
2526
\Doctrine\Deprecations\Deprecation::enableWithTriggerError();
2627
```
2728

28-
If you only want to enable deprecation tracking, without logging or calling `trigger_error` then call:
29+
If you only want to enable deprecation tracking, without logging or calling `trigger_error`
30+
then set the `DOCTRINE_DEPRECATIONS` environment variable to `track`.
31+
Alternatively, call:
2932

3033
```php
3134
\Doctrine\Deprecations\Deprecation::enableTrackingDeprecations();

lib/Doctrine/Deprecations/Deprecation.php

+34-6
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Deprecation
4646
private const TYPE_TRIGGER_ERROR = 2;
4747
private const TYPE_PSR_LOGGER = 4;
4848

49-
/** @var int */
50-
private static $type = self::TYPE_NONE;
49+
/** @var self::TYPE_*|null */
50+
private static $type;
5151

5252
/** @var LoggerInterface|null */
5353
private static $logger;
@@ -72,7 +72,9 @@ class Deprecation
7272
*/
7373
public static function trigger(string $package, string $link, string $message, ...$args): void
7474
{
75-
if (self::$type === self::TYPE_NONE) {
75+
$type = self::$type ?? self::getTypeFromEnv();
76+
77+
if ($type === self::TYPE_NONE) {
7678
return;
7779
}
7880

@@ -118,7 +120,9 @@ public static function trigger(string $package, string $link, string $message, .
118120
*/
119121
public static function triggerIfCalledFromOutside(string $package, string $link, string $message, ...$args): void
120122
{
121-
if (self::$type === self::TYPE_NONE) {
123+
$type = self::$type ?? self::getTypeFromEnv();
124+
125+
if ($type === self::TYPE_NONE) {
122126
return;
123127
}
124128

@@ -161,7 +165,9 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
161165
*/
162166
private static function delegateTriggerToBackend(string $message, array $backtrace, string $link, string $package): void
163167
{
164-
if ((self::$type & self::TYPE_PSR_LOGGER) > 0) {
168+
$type = self::$type ?? self::getTypeFromEnv();
169+
170+
if (($type & self::TYPE_PSR_LOGGER) > 0) {
165171
$context = [
166172
'file' => $backtrace[0]['file'],
167173
'line' => $backtrace[0]['line'],
@@ -172,7 +178,7 @@ private static function delegateTriggerToBackend(string $message, array $backtra
172178
self::$logger->notice($message, $context);
173179
}
174180

175-
if (! ((self::$type & self::TYPE_TRIGGER_ERROR) > 0)) {
181+
if (! (($type & self::TYPE_TRIGGER_ERROR) > 0)) {
176182
return;
177183
}
178184

@@ -263,4 +269,26 @@ public static function getTriggeredDeprecations(): array
263269
{
264270
return self::$ignoredLinks;
265271
}
272+
273+
/**
274+
* @return self::TYPE_*
275+
*/
276+
private static function getTypeFromEnv(): int
277+
{
278+
switch ($_SERVER['DOCTRINE_DEPRECATIONS'] ?? $_ENV['DOCTRINE_DEPRECATIONS'] ?? null) {
279+
case 'trigger':
280+
self::$type = self::TYPE_TRIGGER_ERROR;
281+
break;
282+
283+
case 'track':
284+
self::$type = self::TYPE_TRACK_DEPRECATIONS;
285+
break;
286+
287+
default:
288+
self::$type = self::TYPE_NONE;
289+
break;
290+
}
291+
292+
return self::$type;
293+
}
266294
}

tests/Doctrine/Deprecations/DeprecationTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,36 @@ public function testDeprecationCalledFromOutsideInRoot(): void
248248

249249
$this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount());
250250
}
251+
252+
public function testDeprecationTrackByEnv(): void
253+
{
254+
$reflectionProperty = new ReflectionProperty(Deprecation::class, 'type');
255+
$reflectionProperty->setAccessible(true);
256+
$reflectionProperty->setValue(null);
257+
258+
Deprecation::trigger('Foo', 'link', 'message');
259+
$this->assertSame(0, Deprecation::getUniqueTriggeredDeprecationsCount());
260+
261+
$reflectionProperty->setValue(null);
262+
$_SERVER['DOCTRINE_DEPRECATIONS'] = 'track';
263+
264+
Deprecation::trigger('Foo', __METHOD__, 'message');
265+
$this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount());
266+
}
267+
268+
public function testDeprecationTriggerByEnv(): void
269+
{
270+
$reflectionProperty = new ReflectionProperty(Deprecation::class, 'type');
271+
$reflectionProperty->setAccessible(true);
272+
$reflectionProperty->setValue(null);
273+
$_ENV['DOCTRINE_DEPRECATIONS'] = 'trigger';
274+
275+
$this->expectErrorHandler(
276+
'message (DeprecationTest.php:%d called by TestCase.php:%d, ' . __METHOD__ . ', package Foo)',
277+
__METHOD__
278+
);
279+
280+
Deprecation::trigger('Foo', __METHOD__, 'message');
281+
$this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount());
282+
}
251283
}

0 commit comments

Comments
 (0)