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