1<?php 2 3 4namespace dokuwiki\Debug; 5 6use Doku_Event; 7use dokuwiki\Extension\EventHandler; 8 9class DebugHelper 10{ 11 const INFO_DEPRECATION_LOG_EVENT = 'INFO_DEPRECATION_LOG'; 12 13 /** 14 * Log accesses to deprecated fucntions to the debug log 15 * 16 * @param string $alternative (optional) The function or method that should be used instead 17 * @param int $callerOffset (optional) How far the deprecated method is removed from this one 18 * 19 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 20 */ 21 public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1) 22 { 23 global $conf; 24 /** @var EventHandler $EVENT_HANDLER */ 25 global $EVENT_HANDLER; 26 if ( 27 !$conf['allowdebug'] && 28 ($EVENT_HANDLER === null || !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) 29 ){ 30 // avoid any work if no one cares 31 return; 32 } 33 34 $backtrace = debug_backtrace(); 35 for ($i = 0; $i < $callerOffset; $i += 1) { 36 array_shift($backtrace); 37 } 38 39 list($self, $call) = $backtrace; 40 41 self::triggerDeprecationEvent( 42 $backtrace, 43 $alternative, 44 trim($self['class'] . '::' . $self['function'] . '()', ':'), 45 trim($call['class'] . '::' . $call['function'] . '()', ':'), 46 $call['file'], 47 $call['line'] 48 ); 49 } 50 51 /** 52 * This marks logs a deprecation warning for a property that should no longer be used 53 * 54 * This is usually called withing a magic getter or setter. 55 * For logging deprecated functions or methods see dbgDeprecatedFunction() 56 * 57 * @param string $class The class with the deprecated property 58 * @param string $propertyName The name of the deprecated property 59 * 60 * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 61 */ 62 public static function dbgDeprecatedProperty($class, $propertyName) 63 { 64 global $conf; 65 global $EVENT_HANDLER; 66 if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) { 67 // avoid any work if no one cares 68 return; 69 } 70 71 $backtrace = debug_backtrace(); 72 array_shift($backtrace); 73 $call = $backtrace[1]; 74 $caller = trim($call['class'] . '::' . $call['function'] . '()', ':'); 75 $qualifiedName = $class . '::$' . $propertyName; 76 self::triggerDeprecationEvent( 77 $backtrace, 78 '', 79 $qualifiedName, 80 $caller, 81 $backtrace[0]['file'], 82 $backtrace[0]['line'] 83 ); 84 } 85 86 /** 87 * @param array $backtrace 88 * @param string $alternative 89 * @param string $deprecatedThing 90 * @param string $caller 91 * @param string $file 92 * @param int $line 93 */ 94 private static function triggerDeprecationEvent( 95 array $backtrace, 96 $alternative, 97 $deprecatedThing, 98 $caller, 99 $file, 100 $line 101 ) { 102 $data = [ 103 'trace' => $backtrace, 104 'alternative' => $alternative, 105 'called' => $deprecatedThing, 106 'caller' => $caller, 107 'file' => $file, 108 'line' => $line, 109 ]; 110 $event = new Doku_Event(self::INFO_DEPRECATION_LOG_EVENT, $data); 111 if ($event->advise_before()) { 112 $msg = $event->data['called'] . ' is deprecated. It was called from '; 113 $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line']; 114 if ($event->data['alternative']) { 115 $msg .= ' ' . $event->data['alternative'] . ' should be used instead!'; 116 } 117 dbglog($msg); 118 } 119 $event->advise_after(); 120 } 121} 122