[web-php] master: Enhancement: Use PSR-4 autoloader (#604)
| From: | Andreas Möller via GitHub | Date: | Tue, 05 Jul 2022 11:56:54 +0000 |
| Subject: | [web-php] master: Enhancement: Use PSR-4 autoloader (#604) | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-30257@lists.php.net to get a copy of this message | ||
Author: Andreas Möller (localheinz)
Committer: GitHub (web-flow)
Pusher: kamil-tekiela
Date: 2022-07-05T12:50:41+01:00
Commit: https://github.com/php/web-php/commit/a83ae2fa325bf63bcdfe3ea97e168ec2226eb101
Raw diff: https://github.com/php/web-php/commit/a83ae2fa325bf63bcdfe3ea97e168ec2226eb101.diff
Enhancement: Use PSR-4 autoloader (#604)
Changed paths:
A autoload.php
M bin/createNewsEntry
M bin/createReleaseEntry
M include/shared-manual.inc
Diff:
diff --git a/autoload.php b/autoload.php
new file mode 100644
index 000000000..5625b6062
--- /dev/null
+++ b/autoload.php
@@ -0,0 +1,28 @@
+<?php
+
+/**
+ * @see https://github.com/php-fig/fig-standards/blob/a1a0674a742c9d07c5dd450209fb33b115ee7b40/accepted/PSR-4-autoloader-examples.md#closure-example
+ */
+spl_autoload_register(static function (string $class): void {
+ $prefix = 'phpweb\\';
+ $directory = __DIR__ . '/src/';
+
+ $length = strlen($prefix);
+
+ if (strncmp($prefix, $class, $length) !== 0) {
+ return;
+ }
+
+ $relativeClass = substr(
+ $class,
+ $length
+ );
+
+ $file = $directory . str_replace('\\', '/', $relativeClass) .
'.php';
+
+ if (!file_exists($file)) {
+ return;
+ }
+
+ require $file;
+});
diff --git a/bin/createNewsEntry b/bin/createNewsEntry
index 0f6bdc99b..baae69935 100755
--- a/bin/createNewsEntry
+++ b/bin/createNewsEntry
@@ -2,7 +2,7 @@
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
-require_once __DIR__ . '/../src/News/Entry.php';
+require_once __DIR__ . '/../autoload.php';
use phpweb\News\Entry;
diff --git a/bin/createReleaseEntry b/bin/createReleaseEntry
index fbd3f9d2c..cd98b83d0 100755
--- a/bin/createReleaseEntry
+++ b/bin/createReleaseEntry
@@ -2,7 +2,7 @@
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
-require_once __DIR__ . '/../src/News/Entry.php';
+require_once __DIR__ . '/../autoload.php';
use phpweb\News\Entry;
diff --git a/include/shared-manual.inc b/include/shared-manual.inc
index 7d67a2c3e..2b1d935f6 100644
--- a/include/shared-manual.inc
+++ b/include/shared-manual.inc
@@ -23,7 +23,7 @@ $PGI = array(); $SIDEBAR_DATA = '';
// User note display functions
// =============================================================================
-require_once __DIR__ . '/../src/UserNotes/Sorter.php';
+require_once __DIR__ . '/../autoload.php';
use phpweb\UserNotes\Sorter;
// Print out all user notes for this manual page