Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F46634960
D4186.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D4186.id.diff
View Options
diff --git a/omnitools/src/IO/Directory.php b/omnitools/src/IO/Directory.php
--- a/omnitools/src/IO/Directory.php
+++ b/omnitools/src/IO/Directory.php
@@ -2,6 +2,8 @@
namespace Keruald\OmniTools\IO;
+use Keruald\OmniTools\Collections\Vector;
+
class Directory {
///
@@ -12,6 +14,14 @@
private string $path,
) {}
+ public static function fromPathFragments (iterable $fragments) : self {
+ $path = Vector::from($fragments)
+ ->implode(DIRECTORY_SEPARATOR)
+ ->getValue();
+
+ return new self($path);
+ }
+
///
/// Getters and setters
///
@@ -75,6 +85,20 @@
);
}
+ /**
+ * Count files in the directory matching a specific pattern.
+ * Non-recursive.
+ *
+ * @param string $pattern Glob pattern to match files, defaults to "*"
+ * @return int
+ */
+ public function countFiles (string $pattern = "*") : int {
+ $matches = $this->glob($pattern);
+ $filter = fn (File $file) => is_file($file->getPath());
+
+ return count(array_filter($matches, $filter));
+ }
+
/**
* @return Directory[]
*/
@@ -86,4 +110,12 @@
);
}
+ ///
+ /// Generate File from path
+ ///
+
+ public function getFile (string $filename) : File {
+ return new File($this->path . DIRECTORY_SEPARATOR . $filename);
+ }
+
}
diff --git a/omnitools/src/IO/File.php b/omnitools/src/IO/File.php
--- a/omnitools/src/IO/File.php
+++ b/omnitools/src/IO/File.php
@@ -3,6 +3,8 @@
namespace Keruald\OmniTools\IO;
+use RuntimeException;
+
class File {
/**
@@ -71,4 +73,18 @@
return pathinfo($this->path, PATHINFO_EXTENSION);
}
+ ///
+ /// IO helper methods
+ ///
+
+ public function read () : string {
+ $contents = @file_get_contents($this->path);
+
+ if ($contents === false) {
+ throw new RuntimeException("Unable to read file: $this->path");
+ }
+
+ return $contents;
+ }
+
}
diff --git a/omnitools/tests/IO/DirectoryTest.php b/omnitools/tests/IO/DirectoryTest.php
--- a/omnitools/tests/IO/DirectoryTest.php
+++ b/omnitools/tests/IO/DirectoryTest.php
@@ -22,6 +22,25 @@
$this->directory = new Directory($this->path);
}
+ public function testFromPathFragments () : void {
+ $fragments = [
+ $this->getDataDirectory(),
+ self::TEST_DIRECTORY,
+ ];
+
+ $this->assertEquals(
+ $this->directory,
+ Directory::fromPathFragments($fragments)
+ );
+ }
+
+ public function testFromPathFragmentsWithOnlyOneFragmentEqualsPath () : void {
+ $fragments = [$this->getDataDirectory()];
+ $dir = Directory::fromPathFragments($fragments);
+
+ $this->assertEquals($this->getDataDirectory(), $dir->getPath());
+ }
+
public function testGlob () : void {
$expected = [
new File($this->path . '/Bar.php'),
@@ -32,6 +51,27 @@
$this->assertEquals($expected, $actual);
}
+ public function testCountFiles () : void {
+ $this->assertSame(2, $this->directory->countFiles());
+ $this->assertSame(2, $this->directory->countFiles("*.php"));
+ $this->assertSame(1, $this->directory->countFiles("F*.php"));
+ $this->assertSame(0, $this->directory->countFiles("*.md"));
+ }
+
+ public function testCountFilesIgnoresDirectories () : void {
+ $path = $this->getDataPath("SolarSystemLib");
+ $directory = new Directory($path);
+
+ $this->assertSame(0, $directory->countFiles("Pl*"),
+ "Directory should not count directories.");
+ }
+
+ public function testGetFile () : void {
+ $expected = new File($this->path . DIRECTORY_SEPARATOR . "Foo.php");
+
+ $this->assertEquals($expected, $this->directory->getFile("Foo.php"));
+ }
+
public function testIsReadable () : void {
$this->assertTrue($this->directory->isReadable());
}
diff --git a/omnitools/tests/IO/FileTest.php b/omnitools/tests/IO/FileTest.php
--- a/omnitools/tests/IO/FileTest.php
+++ b/omnitools/tests/IO/FileTest.php
@@ -4,16 +4,37 @@
use Keruald\OmniTools\IO\File;
use Keruald\OmniTools\OS\CurrentOS;
+use Keruald\OmniTools\Tests\WithData;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
+use RuntimeException;
use TypeError;
class FileTest extends TestCase {
+ use WithData;
+
///
/// Tests
///
+
+ public function testRead () : void {
+ $path = $this->getDataPath("MockLib/Foo.php");
+
+ $file = new File($path);
+ $this->assertStringEqualsFile($path, $file->read());
+ }
+
+ public function testReadWhenNotExisting () : void {
+ $path = $this->getDataPath("MockLib/NotExisting.php");
+
+ $file = new File($path);
+
+ $this->expectException(RuntimeException::class);
+ $file->read();
+ }
+
#[DataProvider('provideFilesAndDirectories')]
public function testGetDirectory (string $filename, string $expected) : void {
if (CurrentOS::isPureWindows()) {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Aug 28, 19:36 (7 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4033074
Default Alt Text
D4186.id.diff (5 KB)
Attached To
Mode
D4186: Solve Obsidian Workspaces interaction needs with files and directories
Attached
Detach File
Event Timeline
Log In to Comment