Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3754438
D1617.id4132.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D1617.id4132.diff
View Options
diff --git a/src/Registration/Autoloader.php b/src/Registration/Autoloader.php
--- a/src/Registration/Autoloader.php
+++ b/src/Registration/Autoloader.php
@@ -3,8 +3,6 @@
namespace Keruald\OmniTools\Registration;
-use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
-
class Autoloader {
///
@@ -12,17 +10,24 @@
///
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);
}
///
diff --git a/src/Registration/PSR4/Autoloader.php b/src/Registration/PSR4/Autoloader.php
new file mode 100644
--- /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
--- /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
--- a/tests/Registration/AutoloaderTest.php
+++ b/tests/Registration/AutoloaderTest.php
@@ -10,17 +10,6 @@
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(
@@ -47,15 +36,10 @@
$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
--- /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
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 19, 07:28 (20 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2252061
Default Alt Text
D1617.id4132.diff (6 KB)
Attached To
Mode
D1617: Refactor autoloader
Attached
Detach File
Event Timeline
Log In to Comment