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
File.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SplFileInfo subclass representing a single filesystem file with move and put helpers. |
| 5 | * Validates existence on construction when path checking is enabled. |
| 6 | * Wraps PHP file operations with framework-level error handling. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Filesystem |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Filesystem; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Exception; |
| 16 | use InvalidArgumentException; |
| 17 | use SplFileInfo; |
| 18 | class File extends SplFileInfo |
| 19 | { |
| 20 | /** |
| 21 | * Create a new file instance. |
| 22 | * |
| 23 | * @param string $path The path. |
| 24 | * @param bool $check_path The check path. |
| 25 | * |
| 26 | * @return void |
| 27 | * |
| 28 | * @throws \InvalidArgumentException |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | public function __construct(string $path, bool $check_path = \true) |
| 33 | { |
| 34 | if ($check_path && !\file_exists($path)) { |
| 35 | throw new InvalidArgumentException("File does not exist at path: {$path}"); |
| 36 | } |
| 37 | parent::__construct($path); |
| 38 | } |
| 39 | /** |
| 40 | * Move the file to a new location. |
| 41 | * |
| 42 | * @param string $directory The directory. |
| 43 | * @param ?string $name The name. |
| 44 | * |
| 45 | * @return static |
| 46 | * |
| 47 | * @throws \Exception |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | public function move(string $directory, ?string $name = null) |
| 52 | { |
| 53 | $target = $this->get_target_file($directory, $name); |
| 54 | \set_error_handler(static function ($type, $msg) use(&$error) { |
| 55 | $error = $msg; |
| 56 | }); |
| 57 | try { |
| 58 | $renamed = \rename($this->getPathname(), $target); |
| 59 | } finally { |
| 60 | \restore_error_handler(); |
| 61 | } |
| 62 | if (!$renamed) { |
| 63 | throw new Exception(\sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, \strip_tags($error ?? ''))); |
| 64 | } |
| 65 | @\chmod($target, 0666 & ~\umask()); |
| 66 | return $target; |
| 67 | } |
| 68 | /** |
| 69 | * Get the contents of the file. |
| 70 | * |
| 71 | * @return string |
| 72 | * |
| 73 | * @throws \Exception |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function get_content() |
| 78 | { |
| 79 | $content = \file_get_contents($this->getPathname()); |
| 80 | if ($content === \false) { |
| 81 | throw new Exception(\sprintf('Unable to read the file "%s".', $this->getPathname())); |
| 82 | } |
| 83 | return $content; |
| 84 | } |
| 85 | /** |
| 86 | * Get the target file. |
| 87 | * |
| 88 | * @param string $directory The directory. |
| 89 | * @param ?string $name The name. |
| 90 | * |
| 91 | * @return static |
| 92 | * |
| 93 | * @throws \Exception |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | */ |
| 97 | protected function get_target_file(string $directory, ?string $name = null) |
| 98 | { |
| 99 | if (!\is_dir($directory) && !@\mkdir($directory, 0777, \true) && !\is_dir($directory)) { |
| 100 | if (\is_file($directory)) { |
| 101 | throw new Exception(\sprintf('Unable to create the "%s" directory. A similar named file exists.', $directory)); |
| 102 | } |
| 103 | throw new Exception(\sprintf('Unable to create the "%s" directory.', $directory)); |
| 104 | } elseif (!\is_writable($directory)) { |
| 105 | throw new Exception(\sprintf('Unable to write in the "%s" directory.', $directory)); |
| 106 | } |
| 107 | $target = \rtrim($directory, '/\\') . \DIRECTORY_SEPARATOR . ($name === null ? $this->getBasename() : $this->get_name($name)); |
| 108 | return new self($target, \false); |
| 109 | } |
| 110 | /** |
| 111 | * Get the name of the file. |
| 112 | * |
| 113 | * @param string $name The name. |
| 114 | * |
| 115 | * @return string |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | */ |
| 119 | protected function get_name(string $name) |
| 120 | { |
| 121 | $original_name = \str_replace('\\', '/', $name); |
| 122 | $position = \strrpos($original_name, '/'); |
| 123 | return $position === \false ? $original_name : \substr($original_name, $position + 1); |
| 124 | } |
| 125 | } |
| 126 |