Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12242603
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Reflection/CodeFile.php b/src/Reflection/CodeFile.php
new file mode 100644
index 0000000..0634bdb
--- /dev/null
+++ b/src/Reflection/CodeFile.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Keruald\OmniTools\Reflection;
+
+class CodeFile {
+
+ /**
+ * @var string
+ */
+ private $filename;
+
+ ///
+ /// Constructors
+ ///
+
+ public static function from (string $filename) : self {
+ $instance = new self;
+ $instance->filename = $filename;
+
+ return $instance;
+ }
+
+ ///
+ /// Getters and setters
+ ///
+
+ public function getFilename () : string {
+ return $this->filename;
+ }
+
+ public function setFilename (string $filename) : self {
+ $this->filename = $filename;
+
+ return $this;
+ }
+
+ ///
+ /// File properties methods
+ ///
+
+ public function exists () : bool {
+ return file_exists($this->filename);
+ }
+
+ public function isReadable () : bool {
+ return is_readable($this->filename);
+ }
+
+ ///
+ /// Include methods
+ ///
+
+ public function tryInclude () : bool {
+ if (!$this->canBeIncluded()) {
+ return false;
+ }
+
+ include($this->filename);
+
+ return true;
+ }
+
+ public function canBeIncluded () : bool {
+ return $this->exists() &&$this->isReadable();
+ }
+
+}
diff --git a/src/Registration/Autoloader.php b/src/Registration/Autoloader.php
index 1e61060..c3d6296 100644
--- a/src/Registration/Autoloader.php
+++ b/src/Registration/Autoloader.php
@@ -1,45 +1,29 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Registration;
class Autoloader {
///
/// PSR-4
///
public static function registerPSR4 (string $namespace, string $path) : void {
$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 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
index 81839f0..fee7515 100644
--- a/src/Registration/PSR4/Autoloader.php
+++ b/src/Registration/PSR4/Autoloader.php
@@ -1,55 +1,55 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Registration\PSR4;
-use Keruald\OmniTools\Registration\Autoloader as BaseAutoloader;
+use Keruald\OmniTools\Reflection\CodeFile;
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());
+ CodeFile::from($solver->resolve())->tryInclude();
});
}
}
diff --git a/tests/Reflection/CodeFileTest.php b/tests/Reflection/CodeFileTest.php
new file mode 100644
index 0000000..03c6caf
--- /dev/null
+++ b/tests/Reflection/CodeFileTest.php
@@ -0,0 +1,45 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Reflection;
+
+use Keruald\OmniTools\Reflection\CodeFile;
+use Keruald\OmniTools\Tests\WithData;
+use PHPUnit\Framework\TestCase;
+
+use Acme\MockLib\Bar; // a mock class in a mock namespace to test include
+
+class CodeFileTest extends TestCase {
+
+ use WithData;
+
+ /**
+ * @var CodeFile
+ */
+ private $validCodeFile;
+
+ /**
+ * @var CodeFile
+ */
+ private $notExistingCodeFile;
+
+ public function setUp () : void {
+ $file = $this->getDataPath("MockLib/Bar.php");
+ $this->validCodeFile = CodeFile::from($file);
+
+ $this->notExistingCodeFile = CodeFile::from("/notexisting");
+ }
+
+ public function testCanInclude () : void {
+ $this->assertTrue($this->validCodeFile->canBeIncluded());
+ $this->assertFalse($this->notExistingCodeFile->canBeIncluded());
+ }
+
+ public function testTryInclude () : void {
+ $this->assertTrue($this->validCodeFile->tryInclude());
+ $this->assertTrue(class_exists(Bar::class));
+
+ $this->assertFalse($this->notExistingCodeFile->tryInclude());
+ }
+
+}
diff --git a/tests/Registration/AutoloaderTest.php b/tests/Registration/AutoloaderTest.php
index a8fd9c6..a8ee921 100644
--- a/tests/Registration/AutoloaderTest.php
+++ b/tests/Registration/AutoloaderTest.php
@@ -1,48 +1,42 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Tests\Registration;
use Keruald\OmniTools\Registration\Autoloader;
use Keruald\OmniTools\Tests\WithData;
use PHPUnit\Framework\TestCase;
use Acme\MockLib\Foo; // a mock class in a mock namespace to test autoload.
class AutoloaderTest extends TestCase {
use WithData;
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\\", $this->getDataPath("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()));
}
- public function testCanInclude () : void {
- $file = $this->getDataPath("MockLib/Foo.php");
- $this->assertTrue(Autoloader::canInclude($file));
-
- $this->assertFalse(Autoloader::canInclude("/notexisting"));
- }
}
diff --git a/tests/data/MockLib/Bar.php b/tests/data/MockLib/Bar.php
new file mode 100644
index 0000000..797ef08
--- /dev/null
+++ b/tests/data/MockLib/Bar.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Acme\MockLib;
+
+/**
+ * Class Bar
+ *
+ * This class allows to check if the autoloader can register
+ * the Acme\MockLib namespace and include this file.
+ *
+ * @package Acme\MockLib
+ */
+class Bar {
+
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Oct 12, 09:38 (5 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3065671
Default Alt Text
(7 KB)
Attached To
Mode
rKOT Keruald OmniTools
Attached
Detach File
Event Timeline
Log In to Comment