10c5eb5e2SMichael Große<?php 20c5eb5e2SMichael Große 30c5eb5e2SMichael Große 40c5eb5e2SMichael Großenamespace dokuwiki\Debug; 50c5eb5e2SMichael Große 60c5eb5e2SMichael Großeuse Doku_Event; 7e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler; 80ecde6ceSAndreas Gohruse dokuwiki\Logger; 90c5eb5e2SMichael Große 100c5eb5e2SMichael Großeclass DebugHelper 110c5eb5e2SMichael Große{ 120c5eb5e2SMichael Große const INFO_DEPRECATION_LOG_EVENT = 'INFO_DEPRECATION_LOG'; 130c5eb5e2SMichael Große 140c5eb5e2SMichael Große /** 150c5eb5e2SMichael Große * Log accesses to deprecated fucntions to the debug log 160c5eb5e2SMichael Große * 170c5eb5e2SMichael Große * @param string $alternative (optional) The function or method that should be used instead 180c5eb5e2SMichael Große * @param int $callerOffset (optional) How far the deprecated method is removed from this one 19e4aa0139SAndreas Gohr * @param string $thing (optional) The deprecated thing, defaults to the calling method 200c5eb5e2SMichael Große * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 210c5eb5e2SMichael Große */ 22e4aa0139SAndreas Gohr public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1, $thing = '') 230c5eb5e2SMichael Große { 240c5eb5e2SMichael Große global $conf; 25e1d9dcc8SAndreas Gohr /** @var EventHandler $EVENT_HANDLER */ 260c5eb5e2SMichael Große global $EVENT_HANDLER; 27fdf261bdSAndreas Gohr if ( 28fdf261bdSAndreas Gohr !$conf['allowdebug'] && 29fdf261bdSAndreas Gohr ($EVENT_HANDLER === null || !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) 30fdf261bdSAndreas Gohr ){ 310c5eb5e2SMichael Große // avoid any work if no one cares 320c5eb5e2SMichael Große return; 330c5eb5e2SMichael Große } 340c5eb5e2SMichael Große 350c5eb5e2SMichael Große $backtrace = debug_backtrace(); 360c5eb5e2SMichael Große for ($i = 0; $i < $callerOffset; $i += 1) { 370c5eb5e2SMichael Große array_shift($backtrace); 380c5eb5e2SMichael Große } 390c5eb5e2SMichael Große 400c5eb5e2SMichael Große list($self, $call) = $backtrace; 410c5eb5e2SMichael Große 42e4aa0139SAndreas Gohr if (!$thing) { 43e4aa0139SAndreas Gohr $thing = trim( 44e4aa0139SAndreas Gohr (!empty($self['class']) ? ($self['class'] . '::') : '') . 45e4aa0139SAndreas Gohr $self['function'] . '()', ':'); 46e4aa0139SAndreas Gohr } 47e4aa0139SAndreas Gohr 480c5eb5e2SMichael Große self::triggerDeprecationEvent( 490c5eb5e2SMichael Große $backtrace, 500c5eb5e2SMichael Große $alternative, 51e4aa0139SAndreas Gohr $thing, 529ced17c4SPhy trim( 539ced17c4SPhy (!empty($call['class']) ? ($call['class'] . '::') : '') . 549ced17c4SPhy $call['function'] . '()', ':'), 55*5499b0daSAndreas Gohr $self['file'], 56*5499b0daSAndreas Gohr $self['line'] 570c5eb5e2SMichael Große ); 580c5eb5e2SMichael Große } 590c5eb5e2SMichael Große 600c5eb5e2SMichael Große /** 610c5eb5e2SMichael Große * This marks logs a deprecation warning for a property that should no longer be used 620c5eb5e2SMichael Große * 630c5eb5e2SMichael Große * This is usually called withing a magic getter or setter. 640c5eb5e2SMichael Große * For logging deprecated functions or methods see dbgDeprecatedFunction() 650c5eb5e2SMichael Große * 660c5eb5e2SMichael Große * @param string $class The class with the deprecated property 670c5eb5e2SMichael Große * @param string $propertyName The name of the deprecated property 680c5eb5e2SMichael Große * 690c5eb5e2SMichael Große * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 700c5eb5e2SMichael Große */ 710c5eb5e2SMichael Große public static function dbgDeprecatedProperty($class, $propertyName) 720c5eb5e2SMichael Große { 730c5eb5e2SMichael Große global $conf; 740c5eb5e2SMichael Große global $EVENT_HANDLER; 750c5eb5e2SMichael Große if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) { 760c5eb5e2SMichael Große // avoid any work if no one cares 770c5eb5e2SMichael Große return; 780c5eb5e2SMichael Große } 790c5eb5e2SMichael Große 800c5eb5e2SMichael Große $backtrace = debug_backtrace(); 810c5eb5e2SMichael Große array_shift($backtrace); 820c5eb5e2SMichael Große $call = $backtrace[1]; 830c5eb5e2SMichael Große $caller = trim($call['class'] . '::' . $call['function'] . '()', ':'); 840c5eb5e2SMichael Große $qualifiedName = $class . '::$' . $propertyName; 850c5eb5e2SMichael Große self::triggerDeprecationEvent( 860c5eb5e2SMichael Große $backtrace, 870c5eb5e2SMichael Große '', 880c5eb5e2SMichael Große $qualifiedName, 890c5eb5e2SMichael Große $caller, 900c5eb5e2SMichael Große $backtrace[0]['file'], 910c5eb5e2SMichael Große $backtrace[0]['line'] 920c5eb5e2SMichael Große ); 930c5eb5e2SMichael Große } 940c5eb5e2SMichael Große 950c5eb5e2SMichael Große /** 961b008e87SMichael Große * Trigger a custom deprecation event 971b008e87SMichael Große * 981b008e87SMichael Große * Usually dbgDeprecatedFunction() or dbgDeprecatedProperty() should be used instead. 991b008e87SMichael Große * This method is intended only for those situation where they are not applicable. 1001b008e87SMichael Große * 1011b008e87SMichael Große * @param string $alternative 1021b008e87SMichael Große * @param string $deprecatedThing 1031b008e87SMichael Große * @param string $caller 1041b008e87SMichael Große * @param string $file 1051b008e87SMichael Große * @param int $line 1061b008e87SMichael Große * @param int $callerOffset How many lines should be removed from the beginning of the backtrace 1071b008e87SMichael Große */ 1081b008e87SMichael Große public static function dbgCustomDeprecationEvent( 1091b008e87SMichael Große $alternative, 1101b008e87SMichael Große $deprecatedThing, 1111b008e87SMichael Große $caller, 1121b008e87SMichael Große $file, 1131b008e87SMichael Große $line, 1141b008e87SMichael Große $callerOffset = 1 1151b008e87SMichael Große ) { 1161b008e87SMichael Große global $conf; 1171b008e87SMichael Große /** @var EventHandler $EVENT_HANDLER */ 1181b008e87SMichael Große global $EVENT_HANDLER; 1191b008e87SMichael Große if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) { 1201b008e87SMichael Große // avoid any work if no one cares 1211b008e87SMichael Große return; 1221b008e87SMichael Große } 1231b008e87SMichael Große 1241b008e87SMichael Große $backtrace = array_slice(debug_backtrace(), $callerOffset); 1251b008e87SMichael Große 1261b008e87SMichael Große self::triggerDeprecationEvent( 1271b008e87SMichael Große $backtrace, 1281b008e87SMichael Große $alternative, 1291b008e87SMichael Große $deprecatedThing, 1301b008e87SMichael Große $caller, 1311b008e87SMichael Große $file, 1321b008e87SMichael Große $line 1331b008e87SMichael Große ); 1341b008e87SMichael Große 1351b008e87SMichael Große } 1361b008e87SMichael Große 1371b008e87SMichael Große /** 1380c5eb5e2SMichael Große * @param array $backtrace 1390c5eb5e2SMichael Große * @param string $alternative 1400c5eb5e2SMichael Große * @param string $deprecatedThing 1410c5eb5e2SMichael Große * @param string $caller 1420c5eb5e2SMichael Große * @param string $file 1430c5eb5e2SMichael Große * @param int $line 1440c5eb5e2SMichael Große */ 1450c5eb5e2SMichael Große private static function triggerDeprecationEvent( 1460c5eb5e2SMichael Große array $backtrace, 1470c5eb5e2SMichael Große $alternative, 1480c5eb5e2SMichael Große $deprecatedThing, 1490c5eb5e2SMichael Große $caller, 1500c5eb5e2SMichael Große $file, 1510c5eb5e2SMichael Große $line 1520c5eb5e2SMichael Große ) { 1530c5eb5e2SMichael Große $data = [ 1540c5eb5e2SMichael Große 'trace' => $backtrace, 1550c5eb5e2SMichael Große 'alternative' => $alternative, 1560c5eb5e2SMichael Große 'called' => $deprecatedThing, 1570c5eb5e2SMichael Große 'caller' => $caller, 1580c5eb5e2SMichael Große 'file' => $file, 1590c5eb5e2SMichael Große 'line' => $line, 1600c5eb5e2SMichael Große ]; 1610c5eb5e2SMichael Große $event = new Doku_Event(self::INFO_DEPRECATION_LOG_EVENT, $data); 1620c5eb5e2SMichael Große if ($event->advise_before()) { 1630c5eb5e2SMichael Große $msg = $event->data['called'] . ' is deprecated. It was called from '; 1640c5eb5e2SMichael Große $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line']; 1650c5eb5e2SMichael Große if ($event->data['alternative']) { 1660c5eb5e2SMichael Große $msg .= ' ' . $event->data['alternative'] . ' should be used instead!'; 1670c5eb5e2SMichael Große } 1680ecde6ceSAndreas Gohr Logger::getInstance(Logger::LOG_DEPRECATED)->log($msg); 1690c5eb5e2SMichael Große } 1700c5eb5e2SMichael Große $event->advise_after(); 1710c5eb5e2SMichael Große } 1720c5eb5e2SMichael Große} 173