Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3802744
D2500.id6296.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
D2500.id6296.diff
View Options
diff --git a/DirectoryTest.php b/DirectoryTest.php
new file mode 100644
--- /dev/null
+++ b/DirectoryTest.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace Keruald\OmniTools\IO;
+
+use Keruald\OmniTools\Tests\WithData;
+use PHPUnit\Framework\TestCase;
+
+class DirectoryTest extends TestCase {
+
+ use WithData;
+
+ const TEST_DIRECTORY = "MockLib";
+
+ private string $path;
+ private Directory $directory;
+
+ protected function setUp () : void {
+ $this->path = $this->getDataPath(self::TEST_DIRECTORY);
+ $this->directory = new Directory($this->path);
+ }
+
+ public function testGlob () {
+ $expected = [
+ new File($this->path . '/Bar.php'),
+ new File($this->path . '/Foo.php'),
+ ];
+ $actual = $this->directory->glob("*.php");
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testIsReadable () {
+ $this->assertTrue($this->directory->isReadable());
+ }
+
+ public function testIsWritable () {
+ $result = tempnam($this->path, "test-write-");
+
+ if ($result === false) {
+ $this->markTestSkipped("Can't test write operation.");
+ }
+
+ $canWrite = str_starts_with($result, $this->path);
+ unlink($result);
+
+ $this->assertSame($canWrite, $this->directory->isWritable());
+ }
+
+ public function testExists () {
+ $this->assertTrue($this->directory->exists());
+ }
+
+ public function testExistsWhenItDoesNot () {
+ $directory = new Directory("/nonexistent");
+ $this->assertFalse($directory->exists());
+ }
+
+ public function testExistsWhenItMatchesFile () {
+ $path = $this->getDataPath(self::TEST_DIRECTORY . "/Foo.php");
+ $directory = new Directory($path);
+
+ $this->assertFalse($directory->exists());
+ }
+
+ public function testSetPath () {
+ $this->directory->setPath("/bar/foo");
+ $this->assertEquals("/bar/foo", $this->directory->getPath());
+ }
+
+ public function testGetPath () {
+ $this->assertEquals($this->path, $this->directory->getPath());
+ }
+
+ public function testGetDirectoryName () {
+ $actual = $this->directory->getDirectoryName();
+ $this->assertEquals(self::TEST_DIRECTORY, $actual);
+ }
+
+ public function testGetPathInfo () {
+ $expected = [
+ 'dirname' => $this->getDataDirectory(),
+ 'basename' => self::TEST_DIRECTORY,
+ 'filename' => self::TEST_DIRECTORY,
+ ];
+
+ $this->assertEquals($expected, $this->directory->getPathInfo());
+ }
+
+ public function testGetParentDirectory () {
+ $actual = $this->directory->getParentDirectory();
+ $this->assertEquals($this->getDataDirectory(), $actual);
+ }
+
+}
diff --git a/src/IO/Directory.php b/src/IO/Directory.php
new file mode 100644
--- /dev/null
+++ b/src/IO/Directory.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Keruald\OmniTools\IO;
+
+class Directory {
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (
+ private string $path,
+ ) {}
+
+ ///
+ /// Getters and setters
+ ///
+
+ public function getPath () : string {
+ return $this->path;
+ }
+
+ public function setPath (string $path) : self {
+ $this->path = $path;
+
+ return $this;
+ }
+
+ ///
+ /// Directory properties methods
+ ///
+
+ public function exists () : bool {
+ return is_dir($this->path);
+ }
+
+ public function isReadable () : bool {
+ return is_readable($this->path);
+ }
+
+ public function isWritable () : bool {
+ return is_writable($this->path);
+ }
+
+ public function getPathInfo () : array {
+ return pathinfo($this->path);
+ }
+
+ public function getParentDirectory () : string {
+ return pathinfo($this->path, PATHINFO_DIRNAME);
+ }
+
+ public function getDirectoryName () : string {
+ return pathinfo($this->path, PATHINFO_BASENAME);
+ }
+
+ ///
+ /// Search files
+ ///
+
+ /**
+ * @param string $pattern Gets files in the directory matching a specific
+ * pattern, using the PHP glob function.
+ * @return File[]
+ */
+ public function glob (string $pattern) : array {
+ return array_map(
+ function ($file) {
+ return new File($file);
+ }, glob("$this->path/$pattern")
+ );
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 30, 11:33 (21 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2272919
Default Alt Text
D2500.id6296.diff (4 KB)
Attached To
Mode
D2500: Fetch files from a directory
Attached
Detach File
Event Timeline
Log In to Comment