<?php

/**
 * Temporary workaround for the "Uninitialized string offset 0" warning in ClassLoader.php
 *
 * This file registers an autoloader hook that catches empty class names,
 * logs them with stack traces, but doesn't throw exceptions, allowing the
 * application to continue running.
 *
 * Usage: Include this file at the beginning of your application bootstrap process,
 * before any other autoloading occurs.
 */

// Register the autoloader hook
spl_autoload_register(function($class) {
    if (empty($class)) {
        // Log the empty class name with stack trace
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
        $logMessage = date('Y-m-d H:i:s') . " - Empty class name detected in autoloader.\nStack trace:\n" .
            json_encode($trace, JSON_PRETTY_PRINT) . "\n\n";

        // Log to file
        file_put_contents(
            __DIR__ . '/empty_class_debug.log',
            $logMessage,
            FILE_APPEND
        );

        // Also log to error_log for server logs
        error_log('Empty class name detected in autoloader. See empty_class_debug.log for details.');

        // Return false to continue with other autoloaders
        return false;
    }

    // Not an empty class name, let other autoloaders handle it
    return false;
}, true, true); // prepend=true to ensure this runs before other autoloaders
