| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@author will@imarc.net |
|---|
| 8 |
@license http://flourishlib.com/license |
|---|
| 9 |
|
|---|
| 10 |
@package |
|---|
| 11 |
@link http://flourishlib.com/fDirectory |
|---|
| 12 |
|
|---|
| 13 |
@version |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
@changes |
|---|
| 24 |
|
|---|
| 25 |
class fDirectory |
|---|
| 26 |
{ |
|---|
| 27 |
|
|---|
| 28 |
const create = 'fDirectory::create'; |
|---|
| 29 |
const makeCanonical = 'fDirectory::makeCanonical'; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
@throws |
|---|
| 41 |
|
|---|
| 42 |
@param |
|---|
| 43 |
@param |
|---|
| 44 |
@return |
|---|
| 45 |
|
|---|
| 46 |
static public function create($directory, $mode=0777) |
|---|
| 47 |
{ |
|---|
| 48 |
if (empty($directory)) { |
|---|
| 49 |
throw new fValidationException('No directory name was specified'); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
if (file_exists($directory)) { |
|---|
| 53 |
throw new fValidationException( |
|---|
| 54 |
'The directory specified, %s, already exists', |
|---|
| 55 |
$directory |
|---|
| 56 |
); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
$parent_directory = fFilesystem::getPathInfo($directory, 'dirname'); |
|---|
| 60 |
if (!file_exists($parent_directory)) { |
|---|
| 61 |
fDirectory::create($parent_directory, $mode); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
if (!is_writable($parent_directory)) { |
|---|
| 65 |
throw new fEnvironmentException( |
|---|
| 66 |
'The directory specified, %s, is inside of a directory that is not writable', |
|---|
| 67 |
$directory |
|---|
| 68 |
); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
mkdir($directory, $mode); |
|---|
| 72 |
|
|---|
| 73 |
$directory = new fDirectory($directory); |
|---|
| 74 |
|
|---|
| 75 |
fFilesystem::recordCreate($directory); |
|---|
| 76 |
|
|---|
| 77 |
return $directory; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
@param |
|---|
| 85 |
@return |
|---|
| 86 |
|
|---|
| 87 |
static public function makeCanonical($directory) |
|---|
| 88 |
{ |
|---|
| 89 |
if (substr($directory, -1) != '/' && substr($directory, -1) != '\\') { |
|---|
| 90 |
$directory .= DIRECTORY_SEPARATOR; |
|---|
| 91 |
} |
|---|
| 92 |
return $directory; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
@var |
|---|
| 100 |
|
|---|
| 101 |
protected $deleted = NULL; |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
@var |
|---|
| 107 |
|
|---|
| 108 |
protected $directory; |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
@throws |
|---|
| 119 |
|
|---|
| 120 |
@param |
|---|
| 121 |
@param |
|---|
| 122 |
@return |
|---|
| 123 |
|
|---|
| 124 |
public function __construct($directory, $skip_checks=FALSE) |
|---|
| 125 |
{ |
|---|
| 126 |
if (!$skip_checks) { |
|---|
| 127 |
if (empty($directory)) { |
|---|
| 128 |
throw new fValidationException('No directory was specified'); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
if (!is_readable($directory)) { |
|---|
| 132 |
throw new fValidationException( |
|---|
| 133 |
'The directory specified, %s, does not exist or is not readable', |
|---|
| 134 |
$directory |
|---|
| 135 |
); |
|---|
| 136 |
} |
|---|
| 137 |
if (!is_dir($directory)) { |
|---|
| 138 |
throw new fValidationException( |
|---|
| 139 |
'The directory specified, %s, is not a directory', |
|---|
| 140 |
$directory |
|---|
| 141 |
); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
$directory = self::makeCanonical(realpath($directory)); |
|---|
| 146 |
|
|---|
| 147 |
$this->directory =& fFilesystem::hookFilenameMap($directory); |
|---|
| 148 |
$this->deleted =& fFilesystem::hookDeletedMap($directory); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
if ($this->deleted !== NULL && !fFilesystem::isInsideTransaction()) { |
|---|
| 153 |
fFilesystem::updateDeletedMap($directory, NULL); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
@internal |
|---|
| 162 |
|
|---|
| 163 |
@param |
|---|
| 164 |
@return |
|---|
| 165 |
|
|---|
| 166 |
public function __get($method) |
|---|
| 167 |
{ |
|---|
| 168 |
return array($this, $method); |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
@return |
|---|
| 176 |
|
|---|
| 177 |
public function __toString() |
|---|
| 178 |
{ |
|---|
| 179 |
return $this->getPath(); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
@return |
|---|
| 192 |
|
|---|
| 193 |
public function delete() |
|---|
| 194 |
{ |
|---|
| 195 |
if ($this->deleted) { |
|---|
| 196 |
return; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
$files = $this->scan(); |
|---|
| 200 |
|
|---|
| 201 |
foreach ($files as $file) { |
|---|
| 202 |
$file->delete(); |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 207 |
return fFilesystem::delete($this); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
rmdir($this->directory); |
|---|
| 211 |
|
|---|
| 212 |
fFilesystem::updateDeletedMap($this->directory, debug_backtrace()); |
|---|
| 213 |
fFilesystem::updateFilenameMapForDirectory($this->directory, '*DELETED at ' . time() . ' with token ' . uniqid('', TRUE) . '* ' . $this->directory); |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
@return |
|---|
| 221 |
|
|---|
| 222 |
public function getName() |
|---|
| 223 |
{ |
|---|
| 224 |
return fFilesystem::getPathInfo($this->directory, 'basename'); |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
@return |
|---|
| 232 |
|
|---|
| 233 |
public function getParent() |
|---|
| 234 |
{ |
|---|
| 235 |
$this->tossIfDeleted(); |
|---|
| 236 |
|
|---|
| 237 |
$dirname = fFilesystem::getPathInfo($this->directory, 'dirname'); |
|---|
| 238 |
|
|---|
| 239 |
if ($dirname == $this->directory) { |
|---|
| 240 |
throw new fEnvironmentException( |
|---|
| 241 |
'The current directory does not have a parent directory' |
|---|
| 242 |
); |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
return new fDirectory($dirname); |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
@param |
|---|
| 256 |
@return |
|---|
| 257 |
|
|---|
| 258 |
public function getPath($translate_to_web_path=FALSE) |
|---|
| 259 |
{ |
|---|
| 260 |
$this->tossIfDeleted(); |
|---|
| 261 |
|
|---|
| 262 |
if ($translate_to_web_path) { |
|---|
| 263 |
return fFilesystem::translateToWebPath($this->directory); |
|---|
| 264 |
} |
|---|
| 265 |
return $this->directory; |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
@param |
|---|
| 276 |
@param |
|---|
| 277 |
@return |
|---|
| 278 |
|
|---|
| 279 |
public function getSize($format=FALSE, $decimal_places=1) |
|---|
| 280 |
{ |
|---|
| 281 |
$this->tossIfDeleted(); |
|---|
| 282 |
|
|---|
| 283 |
$size = 0; |
|---|
| 284 |
|
|---|
| 285 |
$children = $this->scan(); |
|---|
| 286 |
foreach ($children as $child) { |
|---|
| 287 |
$size += $child->getSize(); |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
if (!$format) { |
|---|
| 291 |
return $size; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
return fFilesystem::formatFilesize($size, $decimal_places); |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
@return |
|---|
| 302 |
|
|---|
| 303 |
public function isWritable() |
|---|
| 304 |
{ |
|---|
| 305 |
$this->tossIfDeleted(); |
|---|
| 306 |
|
|---|
| 307 |
return is_writable($this->directory); |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
@throws |
|---|
| 325 |
|
|---|
| 326 |
@param |
|---|
| 327 |
@param |
|---|
| 328 |
@return |
|---|
| 329 |
|
|---|
| 330 |
public function move($new_parent_directory, $overwrite) |
|---|
| 331 |
{ |
|---|
| 332 |
if (!$new_parent_directory instanceof fDirectory) { |
|---|
| 333 |
$new_parent_directory = new fDirectory($new_parent_directory); |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
if (strpos($new_parent_directory->getPath(), $this->getPath()) === 0) { |
|---|
| 337 |
throw new fValidationException('It is not possible to move a directory into one of its sub-directories'); |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
return $this->rename($new_parent_directory->getPath() . $this->getName(), $overwrite); |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
@param |
|---|
| 353 |
@param |
|---|
| 354 |
@return |
|---|
| 355 |
|
|---|
| 356 |
public function rename($new_dirname, $overwrite) |
|---|
| 357 |
{ |
|---|
| 358 |
$this->tossIfDeleted(); |
|---|
| 359 |
|
|---|
| 360 |
if (!$this->getParent()->isWritable()) { |
|---|
| 361 |
throw new fEnvironmentException( |
|---|
| 362 |
'The directory, %s, can not be renamed because the directory containing it is not writable', |
|---|
| 363 |
$this->directory |
|---|
| 364 |
); |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
if (preg_match('#^[^/\\\\]+$#D', $new_dirname)) { |
|---|
| 369 |
$new_dirname = $this->getParent()->getPath() . $new_dirname; |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|
| 372 |
$info = fFilesystem::getPathInfo($new_dirname); |
|---|
| 373 |
|
|---|
| 374 |
if (!file_exists($info['dirname'])) { |
|---|
| 375 |
throw new fProgrammerException( |
|---|
| 376 |
'The new directory name specified, %s, is inside of a directory that does not exist', |
|---|
| 377 |
$new_dirname |
|---|
| 378 |
); |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
if (file_exists($new_dirname)) { |
|---|
| 382 |
if (!is_writable($new_dirname)) { |
|---|
| 383 |
throw new fEnvironmentException( |
|---|
| 384 |
'The new directory name specified, %s, already exists, but is not writable', |
|---|
| 385 |
$new_dirname |
|---|
| 386 |
); |
|---|
| 387 |
} |
|---|
| 388 |
if (!$overwrite) { |
|---|
| 389 |
$new_dirname = fFilesystem::makeUniqueName($new_dirname); |
|---|
| 390 |
} |
|---|
| 391 |
} else { |
|---|
| 392 |
$parent_dir = new fDirectory($info['dirname']); |
|---|
| 393 |
if (!$parent_dir->isWritable()) { |
|---|
| 394 |
throw new fEnvironmentException( |
|---|
| 395 |
'The new directory name specified, %s, is inside of a directory that is not writable', |
|---|
| 396 |
$new_dirname |
|---|
| 397 |
); |
|---|
| 398 |
} |
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
rename($this->directory, $new_dirname); |
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
$new_dirname = fDirectory::makeCanonical(realpath($new_dirname)); |
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 408 |
fFilesystem::rename($this->directory, $new_dirname); |
|---|
| 409 |
} |
|---|
| 410 |
|
|---|
| 411 |
fFilesystem::updateFilenameMapForDirectory($this->directory, $new_dirname); |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
http://php.net/scandir |
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
http://php.net/preg_match |
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
@param |
|---|
| 431 |
@return |
|---|
| 432 |
|
|---|
| 433 |
public function scan($filter=NULL) |
|---|
| 434 |
{ |
|---|
| 435 |
$this->tossIfDeleted(); |
|---|
| 436 |
|
|---|
| 437 |
$files = array_diff(scandir($this->directory), array('.', '..')); |
|---|
| 438 |
$objects = array(); |
|---|
| 439 |
|
|---|
| 440 |
if ($filter && !preg_match('#^([^a-zA-Z0-9\\\\\s]).*\1[imsxeADSUXJu]*$#D', $filter)) { |
|---|
| 441 |
$filter = '#^' . strtr( |
|---|
| 442 |
preg_quote($filter, '#'), |
|---|
| 443 |
array( |
|---|
| 444 |
'\\*' => '.*', |
|---|
| 445 |
'\\?' => '.' |
|---|
| 446 |
) |
|---|
| 447 |
) . '$#D'; |
|---|
| 448 |
} |
|---|
| 449 |
|
|---|
| 450 |
natcasesort($files); |
|---|
| 451 |
|
|---|
| 452 |
foreach ($files as $file) { |
|---|
| 453 |
if ($filter) { |
|---|
| 454 |
$test_path = (is_dir($file)) ? $file . '/' : $file; |
|---|
| 455 |
if (!preg_match($filter, $test_path)) { |
|---|
| 456 |
continue; |
|---|
| 457 |
} |
|---|
| 458 |
} |
|---|
| 459 |
|
|---|
| 460 |
$objects[] = fFilesystem::createObject($this->directory . $file); |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
return $objects; |
|---|
| 464 |
} |
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
http://php.net/scandir |
|---|
| 469 |
|
|---|
| 470 |
@param |
|---|
| 471 |
@return |
|---|
| 472 |
|
|---|
| 473 |
public function scanRecursive($filter=NULL) |
|---|
| 474 |
{ |
|---|
| 475 |
$this->tossIfDeleted(); |
|---|
| 476 |
|
|---|
| 477 |
$objects = $this->scan(); |
|---|
| 478 |
|
|---|
| 479 |
$total_files = sizeof($objects); |
|---|
| 480 |
for ($i=0; $i < $total_files; $i++) { |
|---|
| 481 |
if ($objects[$i] instanceof fDirectory) { |
|---|
| 482 |
array_splice($objects, $i+1, 0, $objects[$i]->scanRecursive()); |
|---|
| 483 |
} |
|---|
| 484 |
} |
|---|
| 485 |
|
|---|
| 486 |
if ($filter) { |
|---|
| 487 |
if (!preg_match('#^([^a-zA-Z0-9\\\\\s*?^$]).*\1[imsxeADSUXJu]*$#D', $filter)) { |
|---|
| 488 |
$filter = '#^' . strtr( |
|---|
| 489 |
preg_quote($filter, '#'), |
|---|
| 490 |
array( |
|---|
| 491 |
'\\*' => '.*', |
|---|
| 492 |
'\\?' => '.' |
|---|
| 493 |
) |
|---|
| 494 |
) . '$#D'; |
|---|
| 495 |
} |
|---|
| 496 |
|
|---|
| 497 |
$new_objects = array(); |
|---|
| 498 |
$strip_length = strlen($this->getPath()); |
|---|
| 499 |
foreach ($objects as $object) { |
|---|
| 500 |
$test_path = substr($object->getPath(), $strip_length); |
|---|
| 501 |
$test_path = str_replace(DIRECTORY_SEPARATOR, '/', $test_path); |
|---|
| 502 |
if (!preg_match($filter, $test_path)) { |
|---|
| 503 |
continue; |
|---|
| 504 |
} |
|---|
| 505 |
$new_objects[] = $object; |
|---|
| 506 |
} |
|---|
| 507 |
$objects = $new_objects; |
|---|
| 508 |
} |
|---|
| 509 |
|
|---|
| 510 |
return $objects; |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
@return |
|---|
| 518 |
|
|---|
| 519 |
protected function tossIfDeleted() |
|---|
| 520 |
{ |
|---|
| 521 |
if ($this->deleted) { |
|---|
| 522 |
throw new fProgrammerException( |
|---|
| 523 |
"The action requested can not be performed because the directory has been deleted\n\nBacktrace for fDirectory::delete() call:\n%s", |
|---|
| 524 |
fCore::backtrace(0, $this->deleted) |
|---|
| 525 |
); |
|---|
| 526 |
} |
|---|
| 527 |
} |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 533 |
will@flourishlib.com |
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|