<?php
/**
 * Simple PSR-4 autoloader for the SkyEmagSync namespace.
 *
 * @package SkyEmagSync
 */

namespace SkyEmagSync;

defined( 'ABSPATH' ) || exit;

class Autoloader {

    /**
     * Register the autoloader.
     */
    public static function register(): void {
        spl_autoload_register( [ self::class, 'load' ] );
    }

    /**
     * Load a class file given its fully-qualified class name.
     *
     * @param string $class_name Fully-qualified class name.
     */
    public static function load( string $class_name ): void {
        // Only handle our namespace.
        if ( strpos( $class_name, 'SkyEmagSync\\' ) !== 0 ) {
            return;
        }

        // Strip namespace prefix and convert \ to DIRECTORY_SEPARATOR.
        $relative = substr( $class_name, strlen( 'SkyEmagSync\\' ) );
        $path      = SKYEMAG_PLUGIN_DIR . 'includes/' . str_replace( '\\', DIRECTORY_SEPARATOR, $relative ) . '.php';

        if ( file_exists( $path ) ) {
            require_once $path;
        }
    }
}
