File.php
3 weeks ago
FileSystemServiceProvider.php
3 weeks ago
Fileable.php
3 weeks ago
Filesystem.php
3 weeks ago
Path.php
3 weeks ago
UploadedFile.php
3 weeks ago
Fileable.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Fileable |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Filesystem |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Filesystem; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | /** |
| 14 | * Fileable interface for filesystem operations. |
| 15 | * |
| 16 | * @method bool is_file() |
| 17 | * @method array glob($pattern, int $flags = 0) |
| 18 | * @method string basename() |
| 19 | * @method string name() |
| 20 | * @method string extension() |
| 21 | * @method bool copy(string $target) |
| 22 | * @method bool move(string $target) |
| 23 | * @method bool delete(string|array $paths) |
| 24 | * @method mixed chmod(int|null $mode = null) |
| 25 | * @method bool append(string $data) |
| 26 | * @method bool prepend(string $data) |
| 27 | * @method bool put(string $data) |
| 28 | * @method string get() |
| 29 | * @method array json(int $flags = 0) |
| 30 | * @method string hash(string $algorithm = 'md5') |
| 31 | * @method bool exists() |
| 32 | * @method bool missing() |
| 33 | * @method string dirname() |
| 34 | * @method string type() |
| 35 | * @method string mime_type() |
| 36 | * @method int|false size() |
| 37 | * @method bool is_directory() |
| 38 | * @method bool is_readable() |
| 39 | * @method bool is_writable() |
| 40 | * @method int|false last_modified() |
| 41 | */ |
| 42 | class Fileable |
| 43 | { |
| 44 | /** |
| 45 | * The path to the file. |
| 46 | * |
| 47 | * @var string |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | protected $path; |
| 52 | /** |
| 53 | * Create a new file instance. |
| 54 | * |
| 55 | * @param string $path The path. |
| 56 | * |
| 57 | * @return void |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public function __construct(string $path) |
| 62 | { |
| 63 | $this->path = $path; |
| 64 | } |
| 65 | /** |
| 66 | * Create a new file instance. |
| 67 | * |
| 68 | * @param string $path The path. |
| 69 | * |
| 70 | * @return static |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | */ |
| 74 | public static function make(string $path) |
| 75 | { |
| 76 | return new static($path); |
| 77 | } |
| 78 | /** |
| 79 | * Get the parameters for the given method. |
| 80 | * |
| 81 | * @param mixed $method The method name. |
| 82 | * @param mixed $parameters The parameters array. |
| 83 | * |
| 84 | * @return array |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | protected function parameters($method, $parameters) |
| 89 | { |
| 90 | $exception_methods = ['delete', 'glob']; |
| 91 | if (\in_array($method, $exception_methods)) { |
| 92 | return $parameters; |
| 93 | } |
| 94 | return \array_merge([$this->path], $parameters); |
| 95 | } |
| 96 | /** |
| 97 | * Handle dynamic method calls into the filesystem. |
| 98 | * |
| 99 | * @param mixed $method The method name. |
| 100 | * @param mixed $parameters The parameters array. |
| 101 | * |
| 102 | * @return mixed |
| 103 | * |
| 104 | * @throws \BadMethodCallException |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public function __call($method, $parameters) |
| 109 | { |
| 110 | $filesystem = new Filesystem(); |
| 111 | if (!\method_exists($filesystem, $method)) { |
| 112 | throw new \BadMethodCallException("Method [{$method}] does not exist on [Filesystem]."); |
| 113 | } |
| 114 | return $filesystem->{$method}(...$this->parameters($method, $parameters)); |
| 115 | } |
| 116 | } |
| 117 |