Skip to content

Commit c89d528

Browse files
committed
Enhancement: Use PSR-4 autoloader
1 parent 38d4e07 commit c89d528

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

autoload.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/php-fig/fig-standards/blob/a1a0674a742c9d07c5dd450209fb33b115ee7b40/accepted/PSR-4-autoloader-examples.md#closure-example
5+
*/
6+
spl_autoload_register(static function (string $class): void {
7+
$prefix = 'phpweb\\';
8+
$directory = __DIR__ . '/src/';
9+
10+
$length = strlen($prefix);
11+
12+
if (strncmp($prefix, $class, $length) !== 0) {
13+
return;
14+
}
15+
16+
$relativeClass = substr(
17+
$class,
18+
$length
19+
);
20+
21+
$file = $directory . str_replace('\\', '/', $relativeClass) . '.php';
22+
23+
if (!file_exists($file)) {
24+
return;
25+
}
26+
27+
require $file;
28+
});

bin/createNewsEntry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php
33
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
44

5-
require_once __DIR__ . '/../src/News/Entry.php';
5+
require_once __DIR__ . '/../autoload.php';
66

77
use phpweb\News\Entry;
88

bin/createReleaseEntry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php
33
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
44

5-
require_once __DIR__ . '/../src/News/Entry.php';
5+
require_once __DIR__ . '/../autoload.php';
66

77
use phpweb\News\Entry;
88

include/shared-manual.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $PGI = array(); $SIDEBAR_DATA = '';
2323
// User note display functions
2424
// =============================================================================
2525

26-
require_once __DIR__ . '/../src/UserNotes/Sorter.php';
26+
require_once __DIR__ . '/../autoload.php';
2727
use phpweb\UserNotes\Sorter;
2828

2929
// Print out all user notes for this manual page

0 commit comments

Comments
 (0)