Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3753045
D1620.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D1620.diff
View Options
diff --git a/src/Reflection/CodeFile.php b/src/Reflection/CodeFile.php
new file mode 100644
--- /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
--- a/src/Registration/Autoloader.php
+++ b/src/Registration/Autoloader.php
@@ -15,22 +15,6 @@
}
///
- /// 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
///
diff --git a/src/Registration/PSR4/Autoloader.php b/src/Registration/PSR4/Autoloader.php
--- a/src/Registration/PSR4/Autoloader.php
+++ b/src/Registration/PSR4/Autoloader.php
@@ -3,7 +3,7 @@
namespace Keruald\OmniTools\Registration\PSR4;
-use Keruald\OmniTools\Registration\Autoloader as BaseAutoloader;
+use Keruald\OmniTools\Reflection\CodeFile;
final class Autoloader {
@@ -48,7 +48,7 @@
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
--- /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
--- a/tests/Registration/AutoloaderTest.php
+++ b/tests/Registration/AutoloaderTest.php
@@ -39,10 +39,4 @@
$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
--- /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/plain
Expires
Mon, Nov 18, 22:46 (20 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2251398
Default Alt Text
D1620.diff (4 KB)
Attached To
Mode
D1620: Allow to include a PHP code file
Attached
Detach File
Event Timeline
Log In to Comment