| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 
 | <?php
namespace dokuwiki\Debug;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Logger;
class DebugHelper
{
    protected const INFO_DEPRECATION_LOG_EVENT = 'INFO_DEPRECATION_LOG';
    /**
     * Check if deprecation messages shall be handled
     *
     * This is either because its logging is not disabled or a deprecation handler was registered
     *
     * @return bool
     */
    public static function isEnabled()
    {
        /** @var EventHandler $EVENT_HANDLER */
        global $EVENT_HANDLER;
        if (
            !Logger::getInstance(Logger::LOG_DEPRECATED)->isLogging() &&
            (!$EVENT_HANDLER instanceof EventHandler || !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG'))
        ) {
            // avoid any work if no one cares
            return false;
        }
        return true;
    }
    /**
     * Log accesses to deprecated fucntions to the debug log
     *
     * @param string $alternative (optional) The function or method that should be used instead
     * @param int $callerOffset (optional) How far the deprecated method is removed from this one
     * @param string $thing (optional) The deprecated thing, defaults to the calling method
     * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
     */
    public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1, $thing = '')
    {
        if (!self::isEnabled()) return;
        $backtrace = debug_backtrace();
        for ($i = 0; $i < $callerOffset; ++$i) {
            if (count($backtrace) > 1) array_shift($backtrace);
        }
        [$self, $call] = $backtrace;
        self::triggerDeprecationEvent(
            $backtrace,
            $alternative,
            self::formatCall($self),
            self::formatCall($call),
            $self['file'] ?? $call['file'] ?? '',
            $self['line'] ?? $call['line'] ?? 0
        );
    }
    /**
     * Format the given backtrace info into a proper function/method call string
     * @param array $call
     * @return string
     */
    protected static function formatCall($call)
    {
        $thing = '';
        if (!empty($call['class'])) {
            $thing .= $call['class'] . '::';
        }
        $thing .= $call['function'] . '()';
        return trim($thing, ':');
    }
    /**
     * This marks logs a deprecation warning for a property that should no longer be used
     *
     * This is usually called withing a magic getter or setter.
     * For logging deprecated functions or methods see dbgDeprecatedFunction()
     *
     * @param string $class The class with the deprecated property
     * @param string $propertyName The name of the deprecated property
     *
     * @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
     */
    public static function dbgDeprecatedProperty($class, $propertyName)
    {
        if (!self::isEnabled()) return;
        $backtrace = debug_backtrace();
        array_shift($backtrace);
        $call = $backtrace[1];
        $caller = trim($call['class'] . '::' . $call['function'] . '()', ':');
        $qualifiedName = $class . '::$' . $propertyName;
        self::triggerDeprecationEvent(
            $backtrace,
            '',
            $qualifiedName,
            $caller,
            $backtrace[0]['file'],
            $backtrace[0]['line']
        );
    }
    /**
     * Trigger a custom deprecation event
     *
     * Usually dbgDeprecatedFunction() or dbgDeprecatedProperty() should be used instead.
     * This method is intended only for those situation where they are not applicable.
     *
     * @param string $alternative
     * @param string $deprecatedThing
     * @param string $caller
     * @param string $file
     * @param int $line
     * @param int $callerOffset How many lines should be removed from the beginning of the backtrace
     */
    public static function dbgCustomDeprecationEvent(
        $alternative,
        $deprecatedThing,
        $caller,
        $file,
        $line,
        $callerOffset = 1
    ) {
        if (!self::isEnabled()) return;
        $backtrace = array_slice(debug_backtrace(), $callerOffset);
        self::triggerDeprecationEvent(
            $backtrace,
            $alternative,
            $deprecatedThing,
            $caller,
            $file,
            $line
        );
    }
    /**
     * @param array $backtrace
     * @param string $alternative
     * @param string $deprecatedThing
     * @param string $caller
     * @param string $file
     * @param int $line
     */
    private static function triggerDeprecationEvent(
        array $backtrace,
        $alternative,
        $deprecatedThing,
        $caller,
        $file,
        $line
    ) {
        $data = [
            'trace' => $backtrace,
            'alternative' => $alternative,
            'called' => $deprecatedThing,
            'caller' => $caller,
            'file' => $file,
            'line' => $line,
        ];
        $event = new Event(self::INFO_DEPRECATION_LOG_EVENT, $data);
        if ($event->advise_before()) {
            $msg = $event->data['called'] . ' is deprecated. It was called from ';
            $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line'];
            if ($event->data['alternative']) {
                $msg .= ' ' . $event->data['alternative'] . ' should be used instead!';
            }
            Logger::getInstance(Logger::LOG_DEPRECATED)->log($msg);
        }
        $event->advise_after();
    }
}
 |