tigerstyle-scent/autoloader.php
Ryan Malloy 1da0acd25a Initial commit: WordPress OAuth2 Server with PSR-4 architecture
- Implements complete OAuth2 authorization server for WordPress
- PSR-4 autoloading with WPOAuth2Server namespace structure
- Modular architecture with Auth, Client, Core, Storage components
- Successfully tested authorization code flow with bearer authentication
- Clean separation from WordPress plugin code for reusability
2025-09-16 20:53:00 -06:00

83 lines
2.3 KiB
PHP

<?php
/**
* WP OAuth Server CE - Autoloader
*
* PSR-4 compatible autoloader for the WP OAuth Server CE plugin.
*
* @package WPOAuth2Server
* @version 1.0.0
*/
defined('ABSPATH') or die('Direct access not allowed');
/**
* WP OAuth Server CE Autoloader
*/
class WPOAuth2Server_Autoloader {
private static $instance = null;
private $namespace_prefix = 'WPOAuth2Server\\';
private $base_directory;
/**
* Get singleton instance
*/
public static function instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize autoloader
*/
private function __construct() {
$this->base_directory = __DIR__ . '/';
spl_autoload_register([$this, 'autoload']);
}
/**
* PSR-4 autoloader implementation
*
* @param string $class The fully-qualified class name
*/
public function autoload($class) {
// Check if the class uses our namespace
$len = strlen($this->namespace_prefix);
if (strncmp($this->namespace_prefix, $class, $len) !== 0) {
return; // Not our namespace, let other autoloaders handle it
}
// Get the relative class name
$relative_class = substr($class, $len);
// Replace namespace separators with directory separators
$file = $this->base_directory . str_replace('\\', '/', $relative_class) . '.php';
// If the file exists, require it
if (file_exists($file)) {
require_once $file;
}
}
/**
* Register additional namespace mapping
*
* @param string $namespace_prefix The namespace prefix
* @param string $base_directory The base directory for this namespace
*/
public function add_namespace($namespace_prefix, $base_directory) {
// Normalize namespace prefix
$namespace_prefix = trim($namespace_prefix, '\\') . '\\';
// Normalize base directory
$base_directory = rtrim($base_directory, '/\\') . '/';
// Store the mapping (for future extension)
$this->namespace_mappings[$namespace_prefix] = $base_directory;
}
}
// Initialize the autoloader
WPOAuth2Server_Autoloader::instance();