PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/path.php

http://gutuma.googlecode.com/
PHP | 160 lines | 99 code | 12 blank | 49 comment | 10 complexity | cd123975aa8fcc769b1dfa75712e36ae MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /************************************************************************
  3. * @project Gutuma Newsletter Managment
  4. * @author Rowan Seymour
  5. * @copyright This source is distributed under the GPL
  6. * @file The path class
  7. */
  8. /**
  9. * The path class
  10. */
  11. class gu_path
  12. {
  13. private $path;
  14. /**
  15. * Constructor
  16. */
  17. public function __construct($path)
  18. {
  19. $this->path = $path;
  20. }
  21. /**
  22. * Converts to a string, only called automatically in PHP 5.2.0+
  23. */
  24. public function __toString()
  25. {
  26. return $this->path;
  27. }
  28. /**
  29. * Checks if this path exists
  30. * @return TRUE if this path exists, else FALSE
  31. */
  32. public function exists()
  33. {
  34. return file_exists($this->path);
  35. }
  36. /**
  37. * Checks if this path exists and is a directory or a file
  38. * @return bool TRUE is this path is a directory, else FALSE
  39. */
  40. public function is_directory()
  41. {
  42. return is_dir($this->path);
  43. }
  44. /**
  45. * Checks if this exists and is a file or a directory
  46. * @return bool TRUE is this path is a file, else FALSE
  47. */
  48. public function is_file()
  49. {
  50. return is_file($this->path);
  51. }
  52. /**
  53. * Gets the extension assuming this path is a file
  54. * @return string The extension
  55. */
  56. public function get_extension()
  57. {
  58. $ext = strrchr($this->path, '.');
  59. return ($ext === FALSE || strlen($ext) <= 1) ? NULL : substr($ext, 1);
  60. }
  61. /**
  62. * Gets the filename assuming this path is a file
  63. * @param $inc_extension TRUE if extension should be included, else FALSE
  64. * @return string The name component
  65. */
  66. public function get_name($inc_extension = TRUE)
  67. {
  68. return $inc_extension ? basename($this->path) : basename($this->path, strrchr($this->path, '.'));
  69. }
  70. /**
  71. * Gets the path of the parent directory
  72. * @return gu_path The parent directory path
  73. */
  74. public function get_parent()
  75. {
  76. return new gu_path(dirname($this->path));
  77. }
  78. /**
  79. * Gets the children of this path, i.e. subdirectories and files assuming its a directory
  80. * @param $inc_hidden TRUE if hidden files should be included, else FALSE
  81. * @return array The children
  82. */
  83. public function get_children($inc_hidden = FALSE)
  84. {
  85. $children = array();
  86. if ($dh = @opendir($this->path)) {
  87. while (($item = readdir($dh)) !== FALSE) {
  88. if ($item != '.' && $item != '..' && ($inc_hidden || $item[0] != '.'))
  89. $children[] = new gu_path($this->path.DIRECTORY_SEPARATOR.$item);
  90. }
  91. closedir($dh);
  92. return $children;
  93. }
  94. else
  95. return NULL;
  96. }
  97. /**
  98. * Canonizes the path, even if it doesn't exist. From http://uk2.php.net/realpath
  99. * @return string The canonized path
  100. */
  101. public function get_absolute()
  102. {
  103. $p = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->path);
  104. $parts = array_filter(explode(DIRECTORY_SEPARATOR, $p), 'strlen');
  105. $absolutes = array();
  106. foreach ($parts as $part) {
  107. if ('.' == $part)
  108. continue;
  109. if ('..' == $part)
  110. array_pop($absolutes);
  111. else
  112. $absolutes[] = $part;
  113. }
  114. return implode(DIRECTORY_SEPARATOR, $absolutes);
  115. }
  116. /**
  117. * Renames the file or directory pointed to by this path
  118. * @param $new_path The new path
  119. * @return TRUE if successful, else FALSE
  120. */
  121. public function rename($new_path)
  122. {
  123. gu_debug('rename('.$this->path.', '.$new_path.')');
  124. if (($res = rename($this->path, $new_path)) === TRUE)
  125. $this->path = $new_path;
  126. return $res;
  127. }
  128. /**
  129. * Moves the file or directory pointed to by this path. If new path is an existing
  130. * file it will be overwritten, and if its an existing directory, then this file or
  131. * directory will be moved inside it.
  132. * @param $new_path The new path
  133. * @return TRUE if successful, else FALSE
  134. */
  135. public function move($new_path)
  136. {
  137. if (is_dir($new_path))
  138. $new_path = $new_path.DIRECTORY_SEPARATOR.$this->get_name();
  139. elseif (is_file($new_path))
  140. unlink($new_path);
  141. return $this->rename($new_path);
  142. }
  143. }
  144. ?>