Moving folders

This commit is contained in:
Chris Smith
2025-12-12 21:34:09 +01:00
parent ae9dc9d805
commit 2700955f31
14 changed files with 209 additions and 141 deletions

33
api/src/Autoloader.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
class Autoloader
{
private string $baseDir;
private string $namespace;
public function __construct(string $namespace, string $baseDir)
{
$this->namespace = rtrim($namespace, '\\') . '\\';
$this->baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
public function register(): void
{
spl_autoload_register([$this, 'loadClass']);
}
private function loadClass(string $class): void
{
if (strpos($class, $this->namespace) !== 0) {
return;
}
$relativeClass = substr($class, strlen($this->namespace));
$file = $this->baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
}
}