wordpress-event-bridge-for-.../includes/class-autoloader.php
André Menrath f50ee7441b
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 54s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 57s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Failing after 56s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Failing after 58s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Failing after 54s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Failing after 53s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Failing after 53s
Merge branch 'main' into custom_summary
2024-12-06 16:23:10 +01:00

67 lines
1.9 KiB
PHP

<?php
/**
* Class responsible for autoloading Event Bridge for ActivityPub class files.
*
* The Autoloader class is responsible for automatically loading class files as needed
* to ensure a clean and organized codebase. It maps class names to their corresponding
* file locations within the GatherPress plugin.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
/**
* Class Autoloader.
*
* This class is responsible for automatic loading of classes and namespaces.
*
* @since 1.0.0
*/
class Autoloader {
/**
* Register method for autoloader.
*
* @since 1.0.0
*
* @return void
*/
public static function register(): void {
spl_autoload_register(
function ( $full_class ) {
$base_dir = EVENT_BRIDGE_FOR_ACTIVITYPUB_PLUGIN_DIR . '/includes/';
$base = 'Event_Bridge_For_ActivityPub\\';
if ( strncmp( $full_class, $base, strlen( $base ) ) === 0 ) {
$maybe_uppercase = str_replace( $base, '', $full_class );
$class = strtolower( $maybe_uppercase );
// All classes should be capitalized. If this is instead looking for a lowercase method, we ignore that.
if ( $maybe_uppercase === $class ) {
return;
}
if ( false !== strpos( $class, '\\' ) ) {
$parts = explode( '\\', $class );
$class = array_pop( $parts );
$sub_dir = strtr( implode( '/', $parts ), '_', '-' );
$base_dir = $base_dir . $sub_dir . '/';
}
$filename = 'class-' . strtr( $class, '_', '-' );
$file = $base_dir . $filename . '.php';
if ( file_exists( $file ) && is_readable( $file ) ) {
require_once $file;
} else {
\wp_die( sprintf( esc_html( 'Required class not found or not readable: %s' ), esc_html( $full_class ) ) );
}
}
}
);
}
}