/dmCorePlugin/lib/os/dmFilesystem.php

https://github.com/h16bit/diem · PHP · 260 lines · 210 code · 33 blank · 17 comment · 29 complexity · 8af5e208ac0066876fc463d1298aeb4e MD5 · raw file

  1. <?php
  2. class dmFilesystem extends sfFilesystem
  3. {
  4. protected
  5. $lastExec; // array(command, output, return)
  6. public function mkdir($path, $mode = 0777)
  7. {
  8. if (!is_dir($path))
  9. {
  10. $oldUmask = umask(0);
  11. mkdir($path, $mode, true);
  12. umask($oldUmask);
  13. }
  14. return is_writable($path);
  15. }
  16. public function touch($file, $mode = 0777)
  17. {
  18. if (file_exists($file))
  19. {
  20. if (!is_writable($file))
  21. {
  22. chmod($file, $mode);
  23. return is_writable($file);
  24. }
  25. return true;
  26. }
  27. if (touch($file))
  28. {
  29. chmod($file, $mode);
  30. return is_writable($file);
  31. }
  32. return false;
  33. }
  34. public function find($type = "any")
  35. {
  36. return sfFinder::type($type);
  37. }
  38. public function findFilesInDir($dir)
  39. {
  40. $files = array();
  41. $resource = opendir($dir);
  42. while (false !== $entryname = readdir($resource))
  43. {
  44. if ($entryname == '.' || $entryname == '..') continue;
  45. $currentEntry = $dir.DIRECTORY_SEPARATOR.$entryname;
  46. if (is_file($currentEntry))
  47. {
  48. $files[] = $currentEntry;
  49. }
  50. }
  51. closedir($resource);
  52. return $files;
  53. }
  54. public function getFileInfos($file)
  55. {
  56. if (!file_exists($file))
  57. {
  58. return '[x]';
  59. }
  60. $username = function_exists('posix_getpwuid')
  61. ? dmArray::get(@posix_getpwuid(dmArray::get(stat($file), "uid")), "name")
  62. : '';
  63. $permissions = substr(decoct(fileperms($file)), 2);
  64. return $username.":".$permissions;
  65. }
  66. public function exec($command, $stdoutCallback = null, $stderrCallback = null)
  67. {
  68. try
  69. {
  70. list($out, $err) = $this->execute($command, $stdoutCallback, $stderrCallback);
  71. }
  72. catch(RuntimeException $e)
  73. {
  74. $this->lastExec = array(
  75. 'command' => $command,
  76. 'output' => $e->getMessage()
  77. );
  78. return false;
  79. }
  80. $this->lastExec = array(
  81. 'command' => $command,
  82. 'output' => $out,
  83. );
  84. return true;
  85. }
  86. public function sf($command)
  87. {
  88. try
  89. {
  90. $phpCli = sfToolkit::getPhpCli();
  91. }
  92. catch(sfException $e)
  93. {
  94. $this->lastExec = array(
  95. 'command' => $command,
  96. 'output' => $e->getMessage()
  97. );
  98. return false;
  99. }
  100. $sfCommand = sprintf(
  101. '%s "%s" %s',
  102. $phpCli,
  103. sfConfig::get('sf_root_dir').'/symfony',
  104. $command
  105. );
  106. return $this->exec($sfCommand);
  107. }
  108. public function getLastExec($key = null)
  109. {
  110. if (null === $key)
  111. {
  112. return $this->lastExec;
  113. }
  114. return dmArray::get($this->lastExec, $key);
  115. }
  116. // truncate folder
  117. public function deleteDirContent($dir, $throwExceptions = false)
  118. {
  119. if (!dmProject::isInProject($dir))
  120. {
  121. throw new dmException(sprintf('Try to delete %s, which is outside symfony project', $dir));
  122. }
  123. $success = true;
  124. if(!$dh = @opendir($dir))
  125. {
  126. if ($throwExceptions)
  127. {
  128. throw new dmException("Can not open $dir folder");
  129. }
  130. else
  131. {
  132. $success = false;
  133. }
  134. }
  135. while (false !== ($obj = @readdir($dh)))
  136. {
  137. if($obj == '.' || $obj == '..')
  138. {
  139. continue;
  140. }
  141. if (is_dir($dir . '/' . $obj))
  142. {
  143. $success &= $this->deleteDir($dir.'/'.$obj, $throwExceptions);
  144. }
  145. else
  146. {
  147. if (!@unlink($dir . '/' . $obj))
  148. {
  149. if ($throwExceptions)
  150. {
  151. throw new dmException("Can not delete file $dir/$obj");
  152. }
  153. else
  154. {
  155. $success = false;
  156. }
  157. }
  158. }
  159. }
  160. @closedir($dh);
  161. return $success;
  162. }
  163. // destroy folder
  164. public function deleteDir($dir, $throwExceptions = false)
  165. {
  166. if ($success = $this->deleteDirContent($dir, $throwExceptions))
  167. {
  168. if (!@rmdir($dir))
  169. {
  170. if ($throwExceptions)
  171. {
  172. throw new sfException("Can not delete folder $dir");
  173. }
  174. else
  175. {
  176. $success = false;
  177. }
  178. }
  179. }
  180. return $success;
  181. }
  182. public function unlink($files)
  183. {
  184. if (!is_array($files))
  185. {
  186. $files = array($files);
  187. }
  188. $success = true;
  189. $files = array_reverse($files);
  190. foreach ($files as $file)
  191. {
  192. if (is_dir($file) && !is_link($file))
  193. {
  194. $success &= $this->deleteDir($file);
  195. }
  196. elseif(is_file($file))
  197. {
  198. $success &= @unlink($file);
  199. }
  200. }
  201. return $success;
  202. }
  203. /**
  204. * Calculates the relative path from one to another directory.
  205. * If they share no common path the absolute target dir is returned
  206. *
  207. * @param string $from directory from that the relative path shall be calculated
  208. * @param string $to target directory
  209. */
  210. public function getRelativeDir($from, $to)
  211. {
  212. /*
  213. * $from must end with /
  214. */
  215. $from = '/'.trim($from, '/').'/';
  216. return $this->calculateRelativeDir($from, $to);
  217. }
  218. /**
  219. * Sets the formatter instance.
  220. *
  221. * @param sfFormatter The formatter instance
  222. */
  223. public function setFormatter(sfFormatter $formatter)
  224. {
  225. $this->formatter = $formatter;
  226. }
  227. }