<?php

namespace WPSN\Core;

defined('ABSPATH') || exit;

/**
 * Tiny PSR-4-style autoloader for environments where Composer autoload isn't bundled.
 *
 * In production you may still ship vendor/autoload.php; this is a safe fallback.
 */
final class Autoloader
{
    private const PREFIX = 'WPSN\\';
    private const BASE_DIR = __DIR__ . '/../';

    public static function register(): void
    {
        spl_autoload_register([self::class, 'autoload'], true, true);
    }

    private static function autoload(string $class): void
    {
        if (strncmp(self::PREFIX, $class, strlen(self::PREFIX)) !== 0) {
            return;
        }

        $relative = substr($class, strlen(self::PREFIX));
        $relative = str_replace('\\', '/', $relative);

        $file = self::BASE_DIR . $relative . '.php';
        if (is_readable($file)) {
            require_once $file;
        }
    }
}
