Browse Source

add BuiltinProxy class

olinox14 1 year ago
parent
commit
e86af5ccbd
2 changed files with 222 additions and 0 deletions
  1. 215 0
      src/BuiltinProxy.php
  2. 7 0
      src/Path.php

+ 215 - 0
src/BuiltinProxy.php

@@ -0,0 +1,215 @@
+<?php
+
+namespace Path;
+
+use JetBrains\PhpStorm\ExpectedValues;
+
+/**
+ * A proxy for PHP builtin file methods
+ * Mostly for compatibility and testing purposes
+ */
+class BuiltinProxy
+{
+    public function is_dir(string $filename): bool
+    {
+        return is_dir($filename);
+    }
+
+    public function is_file(string $filename): bool
+    {
+        return is_file($filename);
+    }
+
+    public function file_exists(string $filename): bool
+    {
+        return file_exists($filename);
+    }
+
+    public function pathinfo(
+        string $path,
+        #[ExpectedValues([PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION, PATHINFO_FILENAME])] int $flags = PATHINFO_ALL
+    ): array|string
+    {
+        return pathinfo($path, $flags);
+    }
+
+    public function opendir(string $directory, $context): false|resource
+    {
+        return opendir($directory, $context);
+    }
+
+    public function mkdir(string $directory, int $permissions = 0777, bool $recursive = false, $context): bool
+    {
+        return mkdir($directory, $permissions, $recursive, $context);
+    }
+
+    public function readdir($dir_handle): false|string
+    {
+        return readdir($dir_handle);
+    }
+
+    public function copy(string $from, string $to, $context ): bool
+    {
+        return copy($from, $to, $context);
+    }
+
+    public function closedir($dir_handle): void
+    {
+        closedir($dir_handle);
+    }
+
+    public function scandir(string $directory, int $sorting_order = 0, $context ): array|false
+    {
+        return scandir($directory, $sorting_order, $context);
+    }
+
+    public function unlink(string $filename, $context): bool
+    {
+        return unlink($filename, $context);
+    }
+
+    public function rmdir(string $directory, $context): bool
+    {
+        return rmdir($directory, $context);
+    }
+
+    public function realpath(string $path): false|string
+    {
+        return realpath($path);
+    }
+
+    public function is_readable(string $filename): bool
+    {
+        return is_readable($filename);
+    }
+
+    public function is_writable(string $filename): bool
+    {
+        return is_writable($filename);
+    }
+
+    public function is_executable(string $filename): bool
+    {
+        return is_executable($filename);
+    }
+
+    public function is_link(string $filename): bool
+    {
+        return is_link($filename);
+    }
+
+    public function fileatime(string $filename): false|int
+    {
+        return fileatime($filename);
+    }
+
+    public function filectime(string $filename): false|int
+    {
+        return filectime($filename);
+    }
+
+    public function filemtime(string $filename): false|int
+    {
+        return filemtime($filename);
+    }
+
+    public function chdir(string $directory): bool
+    {
+        return chdir($directory);
+    }
+
+    public function rename(string $from, string $to, $context): bool
+    {
+        return rename($from, $to, $context);
+    }
+
+    public function touch(string $filename, ?int $mtime, ?int $atime): bool
+    {
+        return touch($filename, $mtime, $atime);
+    }
+
+    public function filesize(string $filename): false|int
+    {
+        return filesize($filename);
+    }
+
+    public function dirname(string $path, int $levels = 1 ): string
+    {
+        return dirname($path, $levels);
+    }
+
+    public function file_get_contents(string $filename, bool $use_include_path = false, $context, int $offset = 0, ?int $length): false|string
+    {
+        return file_get_contents($filename, $use_include_path, $context, $offset, $length);
+    }
+
+    public function file_put_contents(string $filename, mixed $data, int $flags = 0, $context ): false|int
+    {
+        return file_put_contents($filename, $data, $flags, $context);
+    }
+
+    public function fileperms(string $filename): false|int
+    {
+        return fileperms($filename);
+    }
+
+    public function clearstatcache(bool $clear_realpath_cache = false, string $filename = '' ): void
+    {
+        clearstatcache($clear_realpath_cache, $filename);
+    }
+
+    public function chmod(string $filename, int $permissions): bool
+    {
+        return chmod($filename, $permissions);
+    }
+
+    public function chown(string $filename, int|string $user): bool
+    {
+        return chown($filename, $user);
+    }
+
+    public function chgrp(string $filename, int|string $group): bool
+    {
+        return chgrp($filename, $group);
+    }
+
+    public function chroot(string $directory): bool
+    {
+        return chroot($directory);
+    }
+
+    public function glob(string $pattern, int $flags = 0 ): array|false
+    {
+        return glob($pattern, $flags);
+    }
+
+    public function fopen(string $filename, string $mode, bool $use_include_path = false, $context ): false|resource
+    {
+        return fopen($filename, $mode, $use_include_path, $context);
+    }
+
+    public function fclose($stream): bool
+    {
+        return fclose($stream);
+    }
+
+    public function feof($stream): bool
+    {
+        return feof($stream);
+    }
+
+    public function fread($stream, int $length): false|string
+    {
+        return fread($stream, $length);
+    }
+
+    public function link(string $target, string $link): bool
+    {
+        return link($target, $link);
+    }
+
+    public function lstat(string $filename): array|false
+    {
+        return lstat($filename);
+    }
+}

+ 7 - 0
src/Path.php

@@ -84,6 +84,7 @@ class Path
      *
      * @throws FileNotFoundException
      * @throws FileExistsException
+     * @throws IOException
      */
     public static function copy_dir(string|self $src, string|self $dst): void
     {
@@ -989,6 +990,12 @@ class Path
         // TODO: implement https://path.readthedocs.io/en/latest/api.html#path.Path.ismount
     }
 
+    public function getDirectoryIterator()
+    {
+        // TODO: complete
+        return new \DirectoryIterator($this->path);
+    }
+
     /**
      * Iterate over the files in this directory.
      *