-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from m3m0r7/add-dir-and-file
Add Dir and File class
- Loading branch information
Showing
8 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/Dir.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable; | ||
|
||
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs; | ||
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_; | ||
use RubyVM\VM\Core\Runtime\BasicObject\Symbolizable; | ||
use RubyVM\VM\Core\Runtime\BasicObject\SymbolizeInterface; | ||
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface; | ||
use RubyVM\VM\Exception\RuntimeException; | ||
|
||
#[BindAliasAs('Dir')] | ||
class Dir extends Enumerable implements RubyClassInterface, SymbolizeInterface | ||
{ | ||
use Symbolizable; | ||
protected ?\ArrayIterator $iterator = null; | ||
|
||
/** | ||
* @var \DirectoryIterator[] | ||
*/ | ||
protected array $files = []; | ||
|
||
protected string $directory; | ||
|
||
public function __construct(protected SymbolInterface $symbol) | ||
{ | ||
$this->directory = (string) $this->symbol; | ||
$this->files = array_values( | ||
iterator_to_array( | ||
new \DirectoryIterator($this->symbol->valueOf()), | ||
) | ||
); | ||
} | ||
|
||
public static function pwd(): RubyClassInterface | ||
{ | ||
return String_::createBy(getcwd()); | ||
} | ||
|
||
public function getIterator(): \ArrayIterator | ||
{ | ||
return $this->iterator ??= new \ArrayIterator($this->files); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return (bool) $this->offsetGet($offset); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->files[$offset] ?? throw new RuntimeException( | ||
sprintf('File not found #%s', $offset), | ||
); | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
throw new RuntimeException('File cannot set'); | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
throw new RuntimeException('File cannot unset'); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->files); | ||
} | ||
|
||
public static function createBy(?string $directory = null): self | ||
{ | ||
return new self(new StringSymbol((string) ($directory ?? getcwd()))); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/IO/File.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable\IO; | ||
|
||
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs; | ||
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_; | ||
use RubyVM\VM\Core\Runtime\BasicObject\SymbolizeInterface; | ||
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\NilSymbol; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\NumberSymbol; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface; | ||
use RubyVM\VM\Exception\RuntimeException; | ||
|
||
#[BindAliasAs('File')] | ||
class File extends IO implements RubyClassInterface, SymbolizeInterface | ||
{ | ||
public function __construct(protected NilSymbol|StringSymbol $path, protected StringSymbol $mode, protected NumberSymbol $permission) | ||
{ | ||
$this->path = $path; | ||
$this->mode = $mode; | ||
$this->permission = $permission; | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
throw new RuntimeException('File cannot iterate'); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
throw new RuntimeException('File cannot exists'); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
throw new RuntimeException('File cannot get'); | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
throw new RuntimeException('File cannot set'); | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
throw new RuntimeException('File cannot unset'); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
throw new RuntimeException('File cannot count'); | ||
} | ||
|
||
public static function read(String_ $path): String_ | ||
{ | ||
return String_::createBy( | ||
file_get_contents( | ||
(string) $path, | ||
), | ||
); | ||
} | ||
|
||
public static function createBy(?string $path = null, ?string $mode = null, ?int $permission = null): self | ||
{ | ||
return new self( | ||
$path === null | ||
? new NilSymbol() | ||
: new StringSymbol($path), | ||
new StringSymbol($mode ?? 'r'), | ||
new NumberSymbol($permission ?? 0666), | ||
); | ||
} | ||
|
||
public function symbol(): SymbolInterface | ||
{ | ||
return $this->path; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/IO/IO.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable\IO; | ||
|
||
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable\Enumerable; | ||
|
||
abstract class IO extends Enumerable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/Version/RubyVM/Call/BasicObject/Kernel/Object_/Enumerable/DirTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\RubyVM\Version\RubyVM\Call\BasicObject\Kernel\Object_\Enumerable; | ||
|
||
use RubyVM\VM\Core\Runtime\Executor\ExecutedStatus; | ||
use Tests\RubyVM\Helper\TestApplication; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
class DirTest extends TestApplication | ||
{ | ||
public function testPwd(): void | ||
{ | ||
$rubyVMManager = $this->createRubyVMFromCode( | ||
<<< '_' | ||
puts Dir.pwd + "\n" | ||
_, | ||
); | ||
|
||
$executor = $rubyVMManager | ||
->rubyVM | ||
->disassemble(); | ||
|
||
$this->assertSame(ExecutedStatus::SUCCESS, $executor->execute()->executedStatus); | ||
$dir = getcwd(); | ||
$this->assertSame("{$dir}\n", $rubyVMManager->stdOut->readAll()); | ||
} | ||
|
||
public function testCount(): void | ||
{ | ||
$rubyVMManager = $this->createRubyVMFromCode( | ||
<<< '_' | ||
puts Dir.new(Dir.pwd).count.to_s + "\n" | ||
_, | ||
); | ||
|
||
$executor = $rubyVMManager | ||
->rubyVM | ||
->disassemble(); | ||
|
||
$this->assertSame(ExecutedStatus::SUCCESS, $executor->execute()->executedStatus); | ||
$count = count(iterator_to_array(new \DirectoryIterator(getcwd() ?: './'))); | ||
$this->assertSame("{$count}\n", $rubyVMManager->stdOut->readAll()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Version/RubyVM/Call/BasicObject/Kernel/Object_/Enumerable/IO/FileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\RubyVM\Version\RubyVM\Call\BasicObject\Kernel\Object_\Enumerable; | ||
|
||
use RubyVM\VM\Core\Runtime\Executor\ExecutedStatus; | ||
use Tests\RubyVM\Helper\TestApplication; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
class FileTest extends TestApplication | ||
{ | ||
public function testRead(): void | ||
{ | ||
$file = __FILE__; | ||
|
||
$rubyVMManager = $this->createRubyVMFromCode( | ||
<<< _ | ||
puts File.read("{$file}") + "\n" | ||
_, | ||
); | ||
|
||
$executor = $rubyVMManager | ||
->rubyVM | ||
->disassemble(); | ||
|
||
$this->assertSame(ExecutedStatus::SUCCESS, $executor->execute()->executedStatus); | ||
$this->assertSame(file_get_contents($file) . "\n", $rubyVMManager->stdOut->readAll()); | ||
} | ||
} |