Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3751413
D1600.id4088.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D1600.id4088.diff
View Options
diff --git a/src/Registration/Autoloader.php b/src/Registration/Autoloader.php
new file mode 100644
--- /dev/null
+++ b/src/Registration/Autoloader.php
@@ -0,0 +1,39 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Registration;
+
+class Autoloader {
+
+ ///
+ /// PSR-4
+ ///
+
+ public static function registerPSR4 (string $namespace, string $path) : void {
+ spl_autoload_register(function ($class) use ($namespace, $path) {
+ $len = strlen($namespace);
+
+ if (substr($class, 0, $len) === $namespace) {
+ $classPath = Autoloader::getPathFor(substr($class, $len));
+ include $path . '/' . $classPath;
+ }
+ });
+ }
+
+ public static function getPathFor (string $name) : string {
+ return str_replace("\\", "/", $name) . '.php';
+ }
+
+ ///
+ /// Methods to register OmniTools library
+ ///
+
+ public static function selfRegister () : void {
+ self::registerPSR4("Keruald\\OmniTools\\", self::getLibraryPath());
+ }
+
+ public static function getLibraryPath () : string {
+ return dirname(__DIR__);
+ }
+
+}
diff --git a/tests/Registration/AutoloaderTest.php b/tests/Registration/AutoloaderTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Registration/AutoloaderTest.php
@@ -0,0 +1,61 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Registration;
+
+use Keruald\OmniTools\Registration\Autoloader;
+use PHPUnit\Framework\TestCase;
+
+use Acme\MockLib\Foo; // a mock class in a mock namespace to test autoload.
+
+class AutoloaderTest extends TestCase {
+
+ ///
+ /// Tests
+ ///
+
+ /**
+ * @dataProvider providePaths
+ */
+ public function testGetPathFor (string $class, string $expected) : void {
+ $this->assertEquals($expected, Autoloader::getPathFor($class));
+ }
+
+ public function testRegisterPSR4 () : void {
+ $class = Foo::class;
+ $this->assertFalse(
+ class_exists($class),
+ "Please reconfigure the test suite not to include the $class class."
+ );
+
+ Autoloader::registerPSR4("Acme\\MockLib\\", __DIR__ . "/MockLib");
+ $this->assertTrue(class_exists($class));
+ }
+
+ public function testGetLibraryPath () : void {
+ $this->assertStringStartsWith(
+ dirname(Autoloader::getLibraryPath()), // lib is in <root>/src
+ __DIR__ // we are in <root>/tests/…
+ );
+ }
+
+ public function testRegister () : void {
+ $count = count(spl_autoload_functions());
+
+ Autoloader::selfRegister();
+
+ $this->assertEquals(++$count, count(spl_autoload_functions()));
+ }
+
+ ///
+ /// Data provider
+ ///
+
+ public function providePaths () : iterable {
+ // Example from PSR-4 canonical document
+ yield ['File_Writer', 'File_Writer.php'];
+ yield ['Response\Status', 'Response/Status.php'];
+ yield ['Request', 'Request.php'];
+ }
+
+}
diff --git a/tests/Registration/MockLib/Foo.php b/tests/Registration/MockLib/Foo.php
new file mode 100644
--- /dev/null
+++ b/tests/Registration/MockLib/Foo.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Acme\MockLib;
+
+/**
+ * Class Foo
+ *
+ * This class allows to check if the autoloader can register
+ * the Acme\MockLib namespace and include this file.
+ *
+ * @package Acme\MockLib
+ */
+class Foo {
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 18, 08:21 (21 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250479
Default Alt Text
D1600.id4088.diff (3 KB)
Attached To
Mode
D1600: Add PSR-4 autoload register implementation
Attached
Detach File
Event Timeline
Log In to Comment