PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Filesystem / File.php
kirki / libraries / framework / Filesystem Last commit date
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