diff --git a/src/vennv/vapm/Error.php b/src/vennv/vapm/Error.php index dbffeb8d8..e96c21be5 100644 --- a/src/vennv/vapm/Error.php +++ b/src/vennv/vapm/Error.php @@ -34,4 +34,6 @@ final class Error public const FILE_DOES_NOT_EXIST = "Error: File does not exist!"; + public const FILE_ALREADY_EXISTS = "Error: File already exists!"; + } \ No newline at end of file diff --git a/src/vennv/vapm/Stream.php b/src/vennv/vapm/Stream.php index e44bff326..64a899d9e 100644 --- a/src/vennv/vapm/Stream.php +++ b/src/vennv/vapm/Stream.php @@ -53,6 +53,20 @@ public static function append(string $path, string $data) : Promise; */ public static function delete(string $path) : Promise; + /** + * @throws Throwable + * + * Use this to create a file. + */ + public static function create(string $path) : Promise; + + /** + * @throws Throwable + * + * Use this to create a file or overwrite a file. + */ + public static function overWrite(string $path, string $data) : Promise; + } final class Stream implements StreamInterface @@ -178,4 +192,58 @@ public static function delete(string $path): Promise }); } + /** + * @throws Throwable + */ + public static function create(string $path): Promise + { + return new Promise(function($resolve , $reject) use ($path): void + { + $generator = function($path) use ($reject): Generator + { + if (!file_exists($path)) + { + yield touch($path); + } + else + { + $reject(Error::FILE_ALREADY_EXISTS); + } + }; + + $generator($path); + + $resolve(); + }); + } + + /** + * @throws Throwable + */ + public static function overWrite(string $path, string $data): Promise + { + return new Promise(function($resolve , $reject) use ($path, $data): void + { + $generator = function($path, $data) use ($reject): Generator + { + $handle = fopen($path, 'w+'); + + if ($handle === false) + { + $reject(Error::UNABLE_TO_OPEN_FILE); + } + else + { + stream_set_blocking($handle, false); + + yield fwrite($handle, $data); + } + }; + + $generator($path, $data); + + $resolve(); + }); + } + } \ No newline at end of file