Deprecation.php
177 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\Deprecations; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Psr\Log\LoggerInterface; |
| 6 | use function array_key_exists; |
| 7 | use function array_reduce; |
| 8 | use function assert; |
| 9 | use function debug_backtrace; |
| 10 | use function sprintf; |
| 11 | use function str_replace; |
| 12 | use function strpos; |
| 13 | use function strrpos; |
| 14 | use function substr; |
| 15 | use function trigger_error; |
| 16 | use const DEBUG_BACKTRACE_IGNORE_ARGS; |
| 17 | use const DIRECTORY_SEPARATOR; |
| 18 | use const E_USER_DEPRECATED; |
| 19 | class Deprecation |
| 20 | { |
| 21 | private const TYPE_NONE = 0; |
| 22 | private const TYPE_TRACK_DEPRECATIONS = 1; |
| 23 | private const TYPE_TRIGGER_ERROR = 2; |
| 24 | private const TYPE_PSR_LOGGER = 4; |
| 25 | private static $type; |
| 26 | private static $logger; |
| 27 | private static $ignoredPackages = []; |
| 28 | private static $triggeredDeprecations = []; |
| 29 | private static $ignoredLinks = []; |
| 30 | private static $deduplication = \true; |
| 31 | public static function trigger(string $package, string $link, string $message, ...$args) : void |
| 32 | { |
| 33 | $type = self::$type ?? self::getTypeFromEnv(); |
| 34 | if ($type === self::TYPE_NONE) { |
| 35 | return; |
| 36 | } |
| 37 | if (isset(self::$ignoredLinks[$link])) { |
| 38 | return; |
| 39 | } |
| 40 | if (array_key_exists($link, self::$triggeredDeprecations)) { |
| 41 | self::$triggeredDeprecations[$link]++; |
| 42 | } else { |
| 43 | self::$triggeredDeprecations[$link] = 1; |
| 44 | } |
| 45 | if (self::$deduplication === \true && self::$triggeredDeprecations[$link] > 1) { |
| 46 | return; |
| 47 | } |
| 48 | if (isset(self::$ignoredPackages[$package])) { |
| 49 | return; |
| 50 | } |
| 51 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
| 52 | $message = sprintf($message, ...$args); |
| 53 | self::delegateTriggerToBackend($message, $backtrace, $link, $package); |
| 54 | } |
| 55 | public static function triggerIfCalledFromOutside(string $package, string $link, string $message, ...$args) : void |
| 56 | { |
| 57 | $type = self::$type ?? self::getTypeFromEnv(); |
| 58 | if ($type === self::TYPE_NONE) { |
| 59 | return; |
| 60 | } |
| 61 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
| 62 | // first check that the caller is not from a tests folder, in which case we always let deprecations pass |
| 63 | if (isset($backtrace[1]['file'], $backtrace[0]['file']) && strpos($backtrace[1]['file'], DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR) === \false) { |
| 64 | $path = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $package) . DIRECTORY_SEPARATOR; |
| 65 | if (strpos($backtrace[0]['file'], $path) === \false) { |
| 66 | return; |
| 67 | } |
| 68 | if (strpos($backtrace[1]['file'], $path) !== \false) { |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | if (isset(self::$ignoredLinks[$link])) { |
| 73 | return; |
| 74 | } |
| 75 | if (array_key_exists($link, self::$triggeredDeprecations)) { |
| 76 | self::$triggeredDeprecations[$link]++; |
| 77 | } else { |
| 78 | self::$triggeredDeprecations[$link] = 1; |
| 79 | } |
| 80 | if (self::$deduplication === \true && self::$triggeredDeprecations[$link] > 1) { |
| 81 | return; |
| 82 | } |
| 83 | if (isset(self::$ignoredPackages[$package])) { |
| 84 | return; |
| 85 | } |
| 86 | $message = sprintf($message, ...$args); |
| 87 | self::delegateTriggerToBackend($message, $backtrace, $link, $package); |
| 88 | } |
| 89 | private static function delegateTriggerToBackend(string $message, array $backtrace, string $link, string $package) : void |
| 90 | { |
| 91 | $type = self::$type ?? self::getTypeFromEnv(); |
| 92 | if (($type & self::TYPE_PSR_LOGGER) > 0) { |
| 93 | $context = ['file' => $backtrace[0]['file'] ?? null, 'line' => $backtrace[0]['line'] ?? null, 'package' => $package, 'link' => $link]; |
| 94 | assert(self::$logger !== null); |
| 95 | self::$logger->notice($message, $context); |
| 96 | } |
| 97 | if (!(($type & self::TYPE_TRIGGER_ERROR) > 0)) { |
| 98 | return; |
| 99 | } |
| 100 | $message .= sprintf(' (%s:%d called by %s:%d, %s, package %s)', self::basename($backtrace[0]['file'] ?? 'native code'), $backtrace[0]['line'] ?? 0, self::basename($backtrace[1]['file'] ?? 'native code'), $backtrace[1]['line'] ?? 0, $link, $package); |
| 101 | @trigger_error($message, E_USER_DEPRECATED); |
| 102 | } |
| 103 | private static function basename(string $filename) : string |
| 104 | { |
| 105 | $pos = strrpos($filename, DIRECTORY_SEPARATOR); |
| 106 | if ($pos === \false) { |
| 107 | return $filename; |
| 108 | } |
| 109 | return substr($filename, $pos + 1); |
| 110 | } |
| 111 | public static function enableTrackingDeprecations() : void |
| 112 | { |
| 113 | self::$type = self::$type ?? self::getTypeFromEnv(); |
| 114 | self::$type |= self::TYPE_TRACK_DEPRECATIONS; |
| 115 | } |
| 116 | public static function enableWithTriggerError() : void |
| 117 | { |
| 118 | self::$type = self::$type ?? self::getTypeFromEnv(); |
| 119 | self::$type |= self::TYPE_TRIGGER_ERROR; |
| 120 | } |
| 121 | public static function enableWithPsrLogger(LoggerInterface $logger) : void |
| 122 | { |
| 123 | self::$type = self::$type ?? self::getTypeFromEnv(); |
| 124 | self::$type |= self::TYPE_PSR_LOGGER; |
| 125 | self::$logger = $logger; |
| 126 | } |
| 127 | public static function withoutDeduplication() : void |
| 128 | { |
| 129 | self::$deduplication = \false; |
| 130 | } |
| 131 | public static function disable() : void |
| 132 | { |
| 133 | self::$type = self::TYPE_NONE; |
| 134 | self::$logger = null; |
| 135 | self::$deduplication = \true; |
| 136 | self::$ignoredLinks = []; |
| 137 | foreach (self::$triggeredDeprecations as $link => $count) { |
| 138 | self::$triggeredDeprecations[$link] = 0; |
| 139 | } |
| 140 | } |
| 141 | public static function ignorePackage(string $packageName) : void |
| 142 | { |
| 143 | self::$ignoredPackages[$packageName] = \true; |
| 144 | } |
| 145 | public static function ignoreDeprecations(string ...$links) : void |
| 146 | { |
| 147 | foreach ($links as $link) { |
| 148 | self::$ignoredLinks[$link] = \true; |
| 149 | } |
| 150 | } |
| 151 | public static function getUniqueTriggeredDeprecationsCount() : int |
| 152 | { |
| 153 | return array_reduce(self::$triggeredDeprecations, static function (int $carry, int $count) { |
| 154 | return $carry + $count; |
| 155 | }, 0); |
| 156 | } |
| 157 | public static function getTriggeredDeprecations() : array |
| 158 | { |
| 159 | return self::$triggeredDeprecations; |
| 160 | } |
| 161 | private static function getTypeFromEnv() : int |
| 162 | { |
| 163 | switch ($_SERVER['DOCTRINE_DEPRECATIONS'] ?? $_ENV['DOCTRINE_DEPRECATIONS'] ?? null) { |
| 164 | case 'trigger': |
| 165 | self::$type = self::TYPE_TRIGGER_ERROR; |
| 166 | break; |
| 167 | case 'track': |
| 168 | self::$type = self::TYPE_TRACK_DEPRECATIONS; |
| 169 | break; |
| 170 | default: |
| 171 | self::$type = self::TYPE_NONE; |
| 172 | break; |
| 173 | } |
| 174 | return self::$type; |
| 175 | } |
| 176 | } |
| 177 |