10c5eb5e2SMichael Große<?php 20c5eb5e2SMichael Große 30c5eb5e2SMichael Große 40c5eb5e2SMichael Großenamespace dokuwiki\Debug; 50c5eb5e2SMichael Große 60c5eb5e2SMichael Großeuse Doku_Event; 7*e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler; 80c5eb5e2SMichael Große 90c5eb5e2SMichael Großeclass DebugHelper 100c5eb5e2SMichael Große{ 110c5eb5e2SMichael Große const INFO_DEPRECATION_LOG_EVENT = 'INFO_DEPRECATION_LOG'; 120c5eb5e2SMichael Große 130c5eb5e2SMichael Große /** 140c5eb5e2SMichael Große * Log accesses to deprecated fucntions to the debug log 150c5eb5e2SMichael Große * 160c5eb5e2SMichael Große * @param string $alternative (optional) The function or method that should be used instead 170c5eb5e2SMichael Große * @param int $callerOffset (optional) How far the deprecated method is removed from this one 180c5eb5e2SMichael Große * 190c5eb5e2SMichael Große * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 200c5eb5e2SMichael Große */ 210c5eb5e2SMichael Große public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1) 220c5eb5e2SMichael Große { 230c5eb5e2SMichael Große global $conf; 24*e1d9dcc8SAndreas Gohr /** @var EventHandler $EVENT_HANDLER */ 250c5eb5e2SMichael Große global $EVENT_HANDLER; 260c5eb5e2SMichael Große if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) { 270c5eb5e2SMichael Große // avoid any work if no one cares 280c5eb5e2SMichael Große return; 290c5eb5e2SMichael Große } 300c5eb5e2SMichael Große 310c5eb5e2SMichael Große $backtrace = debug_backtrace(); 320c5eb5e2SMichael Große for ($i = 0; $i < $callerOffset; $i += 1) { 330c5eb5e2SMichael Große array_shift($backtrace); 340c5eb5e2SMichael Große } 350c5eb5e2SMichael Große 360c5eb5e2SMichael Große list($self, $call) = $backtrace; 370c5eb5e2SMichael Große 380c5eb5e2SMichael Große self::triggerDeprecationEvent( 390c5eb5e2SMichael Große $backtrace, 400c5eb5e2SMichael Große $alternative, 410c5eb5e2SMichael Große trim($self['class'] . '::' . $self['function'] . '()', ':'), 420c5eb5e2SMichael Große trim($call['class'] . '::' . $call['function'] . '()', ':'), 430c5eb5e2SMichael Große $call['file'], 440c5eb5e2SMichael Große $call['line'] 450c5eb5e2SMichael Große ); 460c5eb5e2SMichael Große } 470c5eb5e2SMichael Große 480c5eb5e2SMichael Große /** 490c5eb5e2SMichael Große * This marks logs a deprecation warning for a property that should no longer be used 500c5eb5e2SMichael Große * 510c5eb5e2SMichael Große * This is usually called withing a magic getter or setter. 520c5eb5e2SMichael Große * For logging deprecated functions or methods see dbgDeprecatedFunction() 530c5eb5e2SMichael Große * 540c5eb5e2SMichael Große * @param string $class The class with the deprecated property 550c5eb5e2SMichael Große * @param string $propertyName The name of the deprecated property 560c5eb5e2SMichael Große * 570c5eb5e2SMichael Große * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT 580c5eb5e2SMichael Große */ 590c5eb5e2SMichael Große public static function dbgDeprecatedProperty($class, $propertyName) 600c5eb5e2SMichael Große { 610c5eb5e2SMichael Große global $conf; 620c5eb5e2SMichael Große global $EVENT_HANDLER; 630c5eb5e2SMichael Große if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) { 640c5eb5e2SMichael Große // avoid any work if no one cares 650c5eb5e2SMichael Große return; 660c5eb5e2SMichael Große } 670c5eb5e2SMichael Große 680c5eb5e2SMichael Große $backtrace = debug_backtrace(); 690c5eb5e2SMichael Große array_shift($backtrace); 700c5eb5e2SMichael Große $call = $backtrace[1]; 710c5eb5e2SMichael Große $caller = trim($call['class'] . '::' . $call['function'] . '()', ':'); 720c5eb5e2SMichael Große $qualifiedName = $class . '::$' . $propertyName; 730c5eb5e2SMichael Große self::triggerDeprecationEvent( 740c5eb5e2SMichael Große $backtrace, 750c5eb5e2SMichael Große '', 760c5eb5e2SMichael Große $qualifiedName, 770c5eb5e2SMichael Große $caller, 780c5eb5e2SMichael Große $backtrace[0]['file'], 790c5eb5e2SMichael Große $backtrace[0]['line'] 800c5eb5e2SMichael Große ); 810c5eb5e2SMichael Große } 820c5eb5e2SMichael Große 830c5eb5e2SMichael Große /** 840c5eb5e2SMichael Große * @param array $backtrace 850c5eb5e2SMichael Große * @param string $alternative 860c5eb5e2SMichael Große * @param string $deprecatedThing 870c5eb5e2SMichael Große * @param string $caller 880c5eb5e2SMichael Große * @param string $file 890c5eb5e2SMichael Große * @param int $line 900c5eb5e2SMichael Große */ 910c5eb5e2SMichael Große private static function triggerDeprecationEvent( 920c5eb5e2SMichael Große array $backtrace, 930c5eb5e2SMichael Große $alternative, 940c5eb5e2SMichael Große $deprecatedThing, 950c5eb5e2SMichael Große $caller, 960c5eb5e2SMichael Große $file, 970c5eb5e2SMichael Große $line 980c5eb5e2SMichael Große ) { 990c5eb5e2SMichael Große $data = [ 1000c5eb5e2SMichael Große 'trace' => $backtrace, 1010c5eb5e2SMichael Große 'alternative' => $alternative, 1020c5eb5e2SMichael Große 'called' => $deprecatedThing, 1030c5eb5e2SMichael Große 'caller' => $caller, 1040c5eb5e2SMichael Große 'file' => $file, 1050c5eb5e2SMichael Große 'line' => $line, 1060c5eb5e2SMichael Große ]; 1070c5eb5e2SMichael Große $event = new Doku_Event(self::INFO_DEPRECATION_LOG_EVENT, $data); 1080c5eb5e2SMichael Große if ($event->advise_before()) { 1090c5eb5e2SMichael Große $msg = $event->data['called'] . ' is deprecated. It was called from '; 1100c5eb5e2SMichael Große $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line']; 1110c5eb5e2SMichael Große if ($event->data['alternative']) { 1120c5eb5e2SMichael Große $msg .= ' ' . $event->data['alternative'] . ' should be used instead!'; 1130c5eb5e2SMichael Große } 1140c5eb5e2SMichael Große dbglog($msg); 1150c5eb5e2SMichael Große } 1160c5eb5e2SMichael Große $event->advise_after(); 1170c5eb5e2SMichael Große } 1180c5eb5e2SMichael Große} 119