Filters
1 day ago
Scanning
1 day ago
AbstractFileObject.php
1 day ago
AbstractFilesystemScanner.php
1 day ago
DebugLogReader.php
1 day ago
DirectoryListing.php
1 day ago
DirectorySize.php
1 day ago
DiskWriteCheck.php
1 day ago
FileObject.php
1 day ago
Filesystem.php
1 day ago
FilesystemExceptions.php
5 years ago
FilesystemScanner.php
1 day ago
FilesystemScannerDto.php
1 day ago
FilterableDirectoryIterator.php
1 day ago
LegacyFileRulesTrait.php
1 day ago
LogCleanup.php
1 day ago
LogFiles.php
1 day ago
MissingFileException.php
3 years ago
OPcache.php
1 day ago
PartIdentifier.php
1 day ago
PathChecker.php
1 day ago
PathIdentifier.php
1 day ago
Permissions.php
1 day ago
WpUploadsFolderSymlinker.php
1 day ago
AbstractFileObject.php
336 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Filesystem; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | abstract class AbstractFileObject extends \SplFileObject |
| 13 | { |
| 14 | const MODE_READ = 'rb'; |
| 15 | const MODE_WRITE = 'wb'; |
| 16 | const MODE_APPEND = 'ab'; |
| 17 | const MODE_APPEND_AND_READ = 'ab+'; |
| 18 | const MODE_WRITE_SAFE = 'xb'; |
| 19 | const MODE_WRITE_UNSAFE = 'cb'; |
| 20 | |
| 21 | |
| 22 | protected $totalLines = null; |
| 23 | |
| 24 | |
| 25 | protected $fgetsUsedOnKey0 = false; |
| 26 | |
| 27 | |
| 28 | protected $fseekUsed = false; |
| 29 | |
| 30 | public function __construct(string $fullPath, string $openMode = self::MODE_READ) |
| 31 | { |
| 32 | try { |
| 33 | parent::__construct($fullPath, $openMode); |
| 34 | } catch (\Throwable $e) { |
| 35 | throw $e; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | public function setTotalLines(int $totalLines) |
| 48 | { |
| 49 | $this->totalLines = $totalLines; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | public function totalLines(bool $useParent = false): int |
| 54 | { |
| 55 | if ($this->totalLines !== null) { |
| 56 | return $this->totalLines; |
| 57 | } |
| 58 | |
| 59 | if ($useParent) { |
| 60 | $currentKey = $this->keyUseParent(); |
| 61 | $this->seekUseParent(PHP_INT_MAX); |
| 62 | $this->totalLines = $this->keyUseParent(); |
| 63 | |
| 64 | if ($currentKey < 0) { |
| 65 | $currentKey = 0; |
| 66 | } |
| 67 | |
| 68 | $this->seekUseParent($currentKey); |
| 69 | } else { |
| 70 | $currentKey = $this->key(); |
| 71 | if ($currentKey < 0) { |
| 72 | $currentKey = 0; |
| 73 | } |
| 74 | |
| 75 | $this->seek(PHP_INT_MAX); |
| 76 | $this->totalLines = $this->key(); |
| 77 | $this->seek($currentKey); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | if ($this->totalLines > 0) { |
| 91 | if (PHP_VERSION === '8.2.0RC3' || version_compare(PHP_VERSION, '8.2.0', '>=')) { |
| 92 | $this->totalLines += 1; |
| 93 | } |
| 94 | |
| 95 | if (version_compare(PHP_VERSION, '8.1', '>') && version_compare(PHP_VERSION, '8.1.11', '<=')) { |
| 96 | $this->totalLines += 1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $this->totalLines; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | #[\ReturnTypeWillChange] |
| 121 | public function seek($offset) |
| 122 | { |
| 123 | if ($offset < 0) { |
| 124 | throw new \Exception("Can't seek file: " . $this->getPathname() . " to negative offset: $offset"); |
| 125 | } |
| 126 | |
| 127 | $this->fseekUsed = false; |
| 128 | $this->fgetsUsedOnKey0 = false; |
| 129 | if ($offset === 0 || version_compare(PHP_VERSION, '8.0.1', '<')) { |
| 130 | parent::seek($offset); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | $offset -= 1; |
| 135 | |
| 136 | if ($this->totalLines !== null && $offset >= $this->totalLines) { |
| 137 | $offset += 1; |
| 138 | } |
| 139 | |
| 140 | $originalFlags = $this->getFlags(); |
| 141 | $newFlags = $originalFlags & ~self::READ_AHEAD; |
| 142 | $this->setFlags($newFlags); |
| 143 | |
| 144 | parent::seek($offset); |
| 145 | |
| 146 | if ($this->eof()) { |
| 147 | $this->current(); |
| 148 | $this->totalLines = $this->key(); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $this->current(); |
| 153 | $this->next(); |
| 154 | $this->current(); |
| 155 | |
| 156 | $this->setFlags($originalFlags); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | public function fgets(): string |
| 170 | { |
| 171 | if ($this->key() === 0 || version_compare(PHP_VERSION, '8.0.1', '<')) { |
| 172 | $this->fgetsUsedOnKey0 = true; |
| 173 | return parent::fgets(); |
| 174 | } |
| 175 | |
| 176 | $originalFlags = $this->getFlags(); |
| 177 | $newFlags = $originalFlags & ~self::READ_AHEAD; |
| 178 | $this->setFlags($newFlags); |
| 179 | |
| 180 | $line = $this->current(); |
| 181 | $this->next(); |
| 182 | |
| 183 | if (version_compare(PHP_VERSION, '8.0.19', '<')) { |
| 184 | $line = $this->current(); |
| 185 | } |
| 186 | |
| 187 | if (version_compare(PHP_VERSION, '8.1', '>') && version_compare(PHP_VERSION, '8.1.6', '<')) { |
| 188 | $line = $this->current(); |
| 189 | } |
| 190 | |
| 191 | if (!$this->fseekUsed) { |
| 192 | $line = $this->current(); |
| 193 | } |
| 194 | |
| 195 | $this->setFlags($originalFlags); |
| 196 | return $line; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | #[\ReturnTypeWillChange] |
| 201 | public function key(): int |
| 202 | { |
| 203 | if (!$this->fgetsUsedOnKey0 || version_compare(PHP_VERSION, '8.0.19', '<')) { |
| 204 | return parent::key(); |
| 205 | } |
| 206 | |
| 207 | if (version_compare(PHP_VERSION, '8.1', '>') && version_compare(PHP_VERSION, '8.1.6', '<')) { |
| 208 | return parent::key(); |
| 209 | } |
| 210 | |
| 211 | return parent::key() - 1; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | #[\ReturnTypeWillChange] |
| 225 | public function fseek($offset, $whence = SEEK_SET): int |
| 226 | { |
| 227 | if (version_compare(PHP_VERSION, '8.0.19', '<')) { |
| 228 | return parent::fseek($offset, $whence); |
| 229 | } |
| 230 | |
| 231 | if (version_compare(PHP_VERSION, '8.1', '>') && version_compare(PHP_VERSION, '8.1.6', '<')) { |
| 232 | return parent::fseek($offset, $whence); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | for ($i = 0; $i < 3; $i++) { |
| 238 | parent::fseek(0); |
| 239 | $this->fgets(); |
| 240 | } |
| 241 | |
| 242 | $this->fseekUsed = true; |
| 243 | return parent::fseek((int)$offset, $whence); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | public function readAndMoveNext(bool $useFgets = false): string |
| 256 | { |
| 257 | if ($useFgets && version_compare(PHP_VERSION, '8.0.1', '<')) { |
| 258 | return parent::fgets(); |
| 259 | } |
| 260 | |
| 261 | $originalFlags = $this->getFlags(); |
| 262 | $newFlags = $originalFlags & ~self::READ_AHEAD; |
| 263 | $this->setFlags($newFlags); |
| 264 | |
| 265 | $line = $this->current(); |
| 266 | $this->next(); |
| 267 | |
| 268 | $this->setFlags($originalFlags); |
| 269 | return $line; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | public function isSqlFile(): bool |
| 274 | { |
| 275 | return $this->getExtension() === 'sql'; |
| 276 | } |
| 277 | |
| 278 | public function fgetsUseParent(): string |
| 279 | { |
| 280 | return parent::fgets(); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | public function keyUseParent(): int |
| 285 | { |
| 286 | return parent::key(); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | public function seekUseParent(int $offset) |
| 291 | { |
| 292 | parent::seek($offset); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | #[\ReturnTypeWillChange] |
| 305 | public function flock($operation, &$wouldBlock = null): bool |
| 306 | { |
| 307 | if ($this->isWindowsOs()) { |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | $parentMethodFlock = 'parent::flock'; |
| 312 | if (version_compare(PHP_VERSION, '8.2', '>=')) { |
| 313 | // phpcs:ignore SlevomatCodingStandard.PHP.ForbiddenClasses.ForbiddenClass |
| 314 | $parentMethodFlock = \SplFileObject::class . '::flock'; |
| 315 | } |
| 316 | |
| 317 | if (!is_callable($parentMethodFlock)) { |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | return parent::flock($operation, $wouldBlock); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | protected function isWindowsOs(): bool |
| 328 | { |
| 329 | if (function_exists('wpstgIsWindowsOs')) { |
| 330 | return wpstgIsWindowsOs(); |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | } |
| 336 |