<?php
/**
 * Custom Autoloader for phpseclib3 and its dependencies.
 *
 * This script registers a function that automatically loads class files
 * when they are first used. It is configured for the specific directory
 * structure provided.
 */
spl_autoload_register(function ($class) {
    // The base directory for this autoloader file is .../backuply/lib/classes/
    // We use __DIR__ to build the correct path from this location.

    $prefixes = [
        // Namespace for phpseclib and its path relative to this file
        'phpseclib3\\' => __DIR__ . '/phpseclib/',

        // Namespace for the 'paragonie' dependency and its path
        // IMPORTANT: This is required for phpseclib3 to work.
        'ParagonIE\\ConstantTime\\' => __DIR__ . '/paragonie/constant_time_encoding/src/',
    ];

    // Loop through the namespace prefixes
    foreach ($prefixes as $prefix => $base_dir) {
        // Does the class use the namespace prefix?
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) !== 0) {
            // No, move to the next registered autoloader
            continue;
        }

        // Get the relative class name (e.g., Net\SFTP)
        $relative_class = substr($class, $len);

        // Replace the namespace separator with a directory separator and add .php
        $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

        // If the file exists, require it and we're done
        if (file_exists($file)) {
            include($file);
            return;
        }
    }
});
