relative dir. * * @var array */ private static $map = array(); /** * @param string $root Plugin root path with trailing slash. * @param array $map Namespace relative to ReactWoo\Atomic\ => directory. * @return void */ public static function register( $root, array $map ) { self::$root = trailingslashit( (string) $root ); uksort( $map, static function ( $a, $b ) { return strlen( (string) $b ) - strlen( (string) $a ); } ); self::$map = $map; spl_autoload_register( array( __CLASS__, 'load' ) ); } /** * @param string $class Fully-qualified class name. * @return void */ public static function load( $class ) { $prefix = 'ReactWoo\\Atomic\\'; if ( 0 !== strpos( $class, $prefix ) ) { return; } $relative = substr( $class, strlen( $prefix ) ); foreach ( self::$map as $ns => $dir ) { $ns = (string) $ns; if ( '' === $ns ) { continue; } if ( $relative !== $ns && 0 !== strpos( $relative, $ns . '\\' ) ) { continue; } $rest = trim( substr( $relative, strlen( $ns ) ), '\\' ); if ( '' === $rest ) { return; } $file = self::$root . trailingslashit( str_replace( '\\', '/', (string) $dir ) ) . str_replace( '\\', '/', $rest ) . '.php'; if ( is_readable( $file ) ) { require_once $file; } return; } } }