Skip to content

Commit

Permalink
Denote global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Dec 9, 2024
1 parent 5f1f45d commit c7b15de
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions includes/class-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Autoloader {
*/
public function __construct( $prefix, $path ) {
$this->prefix = $prefix;
$this->prefix_length = strlen( $prefix );
$this->path = rtrim( $path . '/' );
$this->prefix_length = \strlen( $prefix );
$this->path = \rtrim( $path . '/' );
}

/**
Expand All @@ -63,7 +63,7 @@ public function __construct( $prefix, $path ) {
*/
public static function register_path( $prefix, $path ) {
$loader = new self( $prefix, $path );
spl_autoload_register( array( $loader, 'load' ) );
\spl_autoload_register( array( $loader, 'load' ) );
}

/**
Expand All @@ -72,34 +72,34 @@ public static function register_path( $prefix, $path ) {
* @param string $class_name The class to be loaded.
*/
public function load( $class_name ) {
if ( strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
if ( \strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
return;
}

// Strip prefix from the start (ala PSR-4).
$class_name = substr( $class_name, $this->prefix_length + 1 );
$class_name = strtolower( $class_name );
$class_name = \substr( $class_name, $this->prefix_length + 1 );
$class_name = \strtolower( $class_name );
$dir = '';

$last_ns_pos = strripos( $class_name, self::NS_SEPARATOR );
$last_ns_pos = \strripos( $class_name, self::NS_SEPARATOR );
if ( false !== $last_ns_pos ) {
$namespace = substr( $class_name, 0, $last_ns_pos );
$namespace = str_replace( '_', '-', $namespace );
$class_name = substr( $class_name, $last_ns_pos + 1 );
$dir = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
$namespace = \substr( $class_name, 0, $last_ns_pos );
$namespace = \str_replace( '_', '-', $namespace );
$class_name = \substr( $class_name, $last_ns_pos + 1 );
$dir = \str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}

$path = $this->path . $dir . 'class-' . str_replace( '_', '-', $class_name ) . '.php';
$path = $this->path . $dir . 'class-' . \str_replace( '_', '-', $class_name ) . '.php';

if ( ! file_exists( $path ) ) {
$path = $this->path . $dir . 'interface-' . str_replace( '_', '-', $class_name ) . '.php';
if ( ! \file_exists( $path ) ) {
$path = $this->path . $dir . 'interface-' . \str_replace( '_', '-', $class_name ) . '.php';
}

if ( ! file_exists( $path ) ) {
$path = $this->path . $dir . 'trait-' . str_replace( '_', '-', $class_name ) . '.php';
if ( ! \file_exists( $path ) ) {
$path = $this->path . $dir . 'trait-' . \str_replace( '_', '-', $class_name ) . '.php';
}

if ( file_exists( $path ) ) {
if ( \file_exists( $path ) ) {
require_once $path;
}
}
Expand Down

0 comments on commit c7b15de

Please sign in to comment.