Page MenuHomeDevCentral

D4186.id10966.diff
No OneTemporary

D4186.id10966.diff

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

Mime Type
text/plain
Expires
Sat, Aug 29, 13:59 (15 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4033074
Default Alt Text
D4186.id10966.diff (5 KB)

Event Timeline