4 namespace dokuwiki\Debug
;
7 use dokuwiki\Extension\EventHandler
;
12 protected const INFO_DEPRECATION_LOG_EVENT
= 'INFO_DEPRECATION_LOG';
15 * Check if deprecation messages shall be handled
17 * This is either because its logging is not disabled or a deprecation handler was registered
21 public static function isEnabled()
23 /** @var EventHandler $EVENT_HANDLER */
24 global $EVENT_HANDLER;
26 !Logger
::getInstance(Logger
::LOG_DEPRECATED
)->isLogging() &&
27 (!$EVENT_HANDLER instanceof EventHandler ||
!$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG'))
29 // avoid any work if no one cares
36 * Log accesses to deprecated fucntions to the debug log
38 * @param string $alternative (optional) The function or method that should be used instead
39 * @param int $callerOffset (optional) How far the deprecated method is removed from this one
40 * @param string $thing (optional) The deprecated thing, defaults to the calling method
41 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
43 public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1, $thing = '')
45 if (!self
::isEnabled()) return;
47 $backtrace = debug_backtrace();
48 for ($i = 0; $i < $callerOffset; ++
$i) {
49 if(count($backtrace) > 1) array_shift($backtrace);
52 [$self, $call] = $backtrace;
56 (empty($self['class']) ?
('') : $self['class'] . '::') .
57 $self['function'] . '()', ':');
60 self
::triggerDeprecationEvent(
65 (empty($call['class']) ?
('') : $call['class'] . '::') .
66 $call['function'] . '()', ':'),
67 $self['file'] ??
$call['file'] ??
'',
68 $self['line'] ??
$call['line'] ??
0
73 * This marks logs a deprecation warning for a property that should no longer be used
75 * This is usually called withing a magic getter or setter.
76 * For logging deprecated functions or methods see dbgDeprecatedFunction()
78 * @param string $class The class with the deprecated property
79 * @param string $propertyName The name of the deprecated property
81 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
83 public static function dbgDeprecatedProperty($class, $propertyName)
85 if (!self
::isEnabled()) return;
87 $backtrace = debug_backtrace();
88 array_shift($backtrace);
89 $call = $backtrace[1];
90 $caller = trim($call['class'] . '::' . $call['function'] . '()', ':');
91 $qualifiedName = $class . '::$' . $propertyName;
92 self
::triggerDeprecationEvent(
97 $backtrace[0]['file'],
103 * Trigger a custom deprecation event
105 * Usually dbgDeprecatedFunction() or dbgDeprecatedProperty() should be used instead.
106 * This method is intended only for those situation where they are not applicable.
108 * @param string $alternative
109 * @param string $deprecatedThing
110 * @param string $caller
111 * @param string $file
113 * @param int $callerOffset How many lines should be removed from the beginning of the backtrace
115 public static function dbgCustomDeprecationEvent(
124 if (!self
::isEnabled()) return;
126 $backtrace = array_slice(debug_backtrace(), $callerOffset);
128 self
::triggerDeprecationEvent(
140 * @param array $backtrace
141 * @param string $alternative
142 * @param string $deprecatedThing
143 * @param string $caller
144 * @param string $file
147 private static function triggerDeprecationEvent(
157 'trace' => $backtrace,
158 'alternative' => $alternative,
159 'called' => $deprecatedThing,
164 $event = new Doku_Event(self
::INFO_DEPRECATION_LOG_EVENT
, $data);
165 if ($event->advise_before()) {
166 $msg = $event->data
['called'] . ' is deprecated. It was called from ';
167 $msg .= $event->data
['caller'] . ' in ' . $event->data
['file'] . ':' . $event->data
['line'];
168 if ($event->data
['alternative']) {
169 $msg .= ' ' . $event->data
['alternative'] . ' should be used instead!';
171 Logger
::getInstance(Logger
::LOG_DEPRECATED
)->log($msg);
173 $event->advise_after();