3 namespace dokuwiki\Debug
;
5 use dokuwiki\Extension\Event
;
6 use dokuwiki\Extension\EventHandler
;
11 protected const INFO_DEPRECATION_LOG_EVENT
= 'INFO_DEPRECATION_LOG';
14 * Check if deprecation messages shall be handled
16 * This is either because its logging is not disabled or a deprecation handler was registered
20 public static function isEnabled()
22 /** @var EventHandler $EVENT_HANDLER */
23 global $EVENT_HANDLER;
25 !Logger
::getInstance(Logger
::LOG_DEPRECATED
)->isLogging() &&
26 (!$EVENT_HANDLER instanceof EventHandler ||
!$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG'))
28 // avoid any work if no one cares
35 * Log accesses to deprecated fucntions to the debug log
37 * @param string $alternative (optional) The function or method that should be used instead
38 * @param int $callerOffset (optional) How far the deprecated method is removed from this one
39 * @param string $thing (optional) The deprecated thing, defaults to the calling method
40 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
42 public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1, $thing = '')
44 if (!self
::isEnabled()) return;
46 $backtrace = debug_backtrace();
47 for ($i = 0; $i < $callerOffset; ++
$i) {
48 if (count($backtrace) > 1) array_shift($backtrace);
51 [$self, $call] = $backtrace;
53 self
::triggerDeprecationEvent(
56 self
::formatCall($self),
57 self
::formatCall($call),
58 $self['file'] ??
$call['file'] ??
'',
59 $self['line'] ??
$call['line'] ??
0
64 * Format the given backtrace info into a proper function/method call string
68 protected static function formatCall($call)
71 if (!empty($call['class'])) {
72 $thing .= $call['class'] . '::';
74 $thing .= $call['function'] . '()';
75 return trim($thing, ':');
79 * This marks logs a deprecation warning for a property that should no longer be used
81 * This is usually called withing a magic getter or setter.
82 * For logging deprecated functions or methods see dbgDeprecatedFunction()
84 * @param string $class The class with the deprecated property
85 * @param string $propertyName The name of the deprecated property
87 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
89 public static function dbgDeprecatedProperty($class, $propertyName)
91 if (!self
::isEnabled()) return;
93 $backtrace = debug_backtrace();
94 array_shift($backtrace);
95 $call = $backtrace[1];
96 $caller = trim($call['class'] . '::' . $call['function'] . '()', ':');
97 $qualifiedName = $class . '::$' . $propertyName;
98 self
::triggerDeprecationEvent(
103 $backtrace[0]['file'],
104 $backtrace[0]['line']
109 * Trigger a custom deprecation event
111 * Usually dbgDeprecatedFunction() or dbgDeprecatedProperty() should be used instead.
112 * This method is intended only for those situation where they are not applicable.
114 * @param string $alternative
115 * @param string $deprecatedThing
116 * @param string $caller
117 * @param string $file
119 * @param int $callerOffset How many lines should be removed from the beginning of the backtrace
121 public static function dbgCustomDeprecationEvent(
129 if (!self
::isEnabled()) return;
131 $backtrace = array_slice(debug_backtrace(), $callerOffset);
133 self
::triggerDeprecationEvent(
144 * @param array $backtrace
145 * @param string $alternative
146 * @param string $deprecatedThing
147 * @param string $caller
148 * @param string $file
151 private static function triggerDeprecationEvent(
160 'trace' => $backtrace,
161 'alternative' => $alternative,
162 'called' => $deprecatedThing,
167 $event = new Event(self
::INFO_DEPRECATION_LOG_EVENT
, $data);
168 if ($event->advise_before()) {
169 $msg = $event->data
['called'] . ' is deprecated. It was called from ';
170 $msg .= $event->data
['caller'] . ' in ' . $event->data
['file'] . ':' . $event->data
['line'];
171 if ($event->data
['alternative']) {
172 $msg .= ' ' . $event->data
['alternative'] . ' should be used instead!';
174 Logger
::getInstance(Logger
::LOG_DEPRECATED
)->log($msg);
176 $event->advise_after();