<?php
/**
 * Minimal PSR-4 autoloader (no Composer required on host).
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder;

final class Autoloader {

	private static string $base = '';
	private static string $prefix = '';

	public static function register( string $base_dir, string $prefix ): void {
		self::$base   = rtrim( $base_dir, '/\\' ) . DIRECTORY_SEPARATOR;
		self::$prefix = $prefix;
		spl_autoload_register( array( self::class, 'load' ) );
	}

	public static function load( string $class ): void {
		if ( strncmp( $class, self::$prefix, strlen( self::$prefix ) ) !== 0 ) {
			return;
		}
		$relative = substr( $class, strlen( self::$prefix ) );
		$relative = str_replace( '\\', DIRECTORY_SEPARATOR, $relative );
		$file     = self::$base . $relative . '.php';
		if ( is_file( $file ) ) {
			require_once $file;
		}
	}
}
