Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/Registration/Autoloader.php b/src/Registration/Autoloader.php
index 859db3d..1e61060 100644
--- a/src/Registration/Autoloader.php
+++ b/src/Registration/Autoloader.php
@@ -1,40 +1,45 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Registration;
-use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
-
class Autoloader {
///
/// PSR-4
///
public static function registerPSR4 (string $namespace, string $path) : void {
- spl_autoload_register(function ($class) use ($namespace, $path) {
- if (StringUtilities::startsWith($class, $namespace)) {
- $len = strlen($namespace);
- $classPath = Autoloader::getPathFor(substr($class, $len));
- include $path . '/' . $classPath;
- }
- });
+ $loader = new PSR4\Autoloader($namespace, $path);
+ $loader->register();
+ }
+
+ ///
+ /// Include methods
+ ///
+
+ public static function tryInclude (string $filename) : void {
+ if (!self::canInclude($filename)) {
+ return;
+ }
+
+ include($filename);
}
- public static function getPathFor (string $name) : string {
- return str_replace("\\", "/", $name) . '.php';
+ public static function canInclude (string $filename) : bool {
+ return file_exists($filename) && is_readable($filename);
}
///
/// 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/src/Registration/PSR4/Autoloader.php b/src/Registration/PSR4/Autoloader.php
new file mode 100644
index 0000000..81839f0
--- /dev/null
+++ b/src/Registration/PSR4/Autoloader.php
@@ -0,0 +1,55 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Registration\PSR4;
+
+use Keruald\OmniTools\Registration\Autoloader as BaseAutoloader;
+
+final class Autoloader {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var string
+ */
+ private $namespace;
+
+ /**
+ * @var string The base path where files for this namespace are located
+ */
+ private $path;
+
+ ///
+ /// Constructor
+ ///
+
+ public function __construct (string $namespace, string $path) {
+ $this->namespace = $namespace;
+ $this->path = $path;
+ }
+
+ ///
+ /// Public methods
+ ///
+
+ public function getSolver (string $class) : Solver {
+ return new Solver($this->namespace, $this->path, $class);
+ }
+
+ public function register () : void {
+ $loader = $this;
+
+ spl_autoload_register(function ($class) use ($loader) {
+ $solver = $loader->getSolver($class);
+
+ if (!$solver->canResolve()) {
+ return;
+ }
+
+ BaseAutoloader::tryInclude($solver->resolve());
+ });
+ }
+
+}
diff --git a/src/Registration/PSR4/Solver.php b/src/Registration/PSR4/Solver.php
new file mode 100644
index 0000000..c3dc87b
--- /dev/null
+++ b/src/Registration/PSR4/Solver.php
@@ -0,0 +1,66 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Registration\PSR4;
+
+use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
+
+final class Solver {
+
+ /**
+ * @var string
+ */
+ private $namespace;
+
+ /**
+ * @var string The base path for the namespace
+ */
+ private $path;
+
+ /**
+ * @var string The fully qualif class name
+ */
+ private $class;
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (string $namespace, string $path, string $class) {
+ $this->namespace = $namespace;
+ $this->path = $path;
+ $this->class = $class;
+ }
+
+ ///
+ /// Resolve methods
+ ///
+
+ public function resolve () : string {
+ return $this->path
+ . '/'
+ . $this->getRelativePath();
+ }
+
+ public function canResolve () : bool {
+ return StringUtilities::startsWith($this->class, $this->namespace);
+ }
+
+ public static function getPathFor (string $name) : string {
+ return str_replace("\\", "/", $name) . '.php';
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ private function getRelativePath () : string {
+ return self::getPathFor($this->getLocalClassName());
+ }
+
+ private function getLocalClassName () : string {
+ $len = strlen($this->namespace);
+ return substr($this->class, $len);
+ }
+
+}
diff --git a/tests/Registration/AutoloaderTest.php b/tests/Registration/AutoloaderTest.php
index fd69f7f..6f19f95 100644
--- a/tests/Registration/AutoloaderTest.php
+++ b/tests/Registration/AutoloaderTest.php
@@ -1,61 +1,45 @@
<?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 testCanInclude () : void {
+ $file = __DIR__ . "/MockLib/Foo.php";
+ $this->assertTrue(Autoloader::canInclude($file));
- 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'];
+ $this->assertFalse(Autoloader::canInclude("/notexisting"));
}
-
}
diff --git a/tests/Registration/PSR4AutoloaderTest.php b/tests/Registration/PSR4AutoloaderTest.php
new file mode 100644
index 0000000..7d21da5
--- /dev/null
+++ b/tests/Registration/PSR4AutoloaderTest.php
@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Registration;
+
+use Keruald\OmniTools\Registration\PSR4\Solver;
+use PHPUnit\Framework\TestCase;
+
+class PSR4AutoloaderTest extends TestCase {
+
+ ///
+ /// Tests
+ ///
+
+ /**
+ * @dataProvider providePaths
+ */
+ public function testGetPathFor (string $class, string $expected) : void {
+ $this->assertEquals($expected, Solver::getPathFor($class));
+ }
+
+ ///
+ /// 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'];
+ }
+
+
+}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Sep 19, 00:35 (4 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2991981
Default Alt Text
(7 KB)

Event Timeline