sfFilesystem.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfFilesystem provides basic utility to manipulate the file system.
  11. *
  12. * @package symfony
  13. * @subpackage util
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFilesystem.class.php 9097 2008-05-20 07:40:01Z FabianLange $
  16. */
  17. class sfFilesystem
  18. {
  19. protected
  20. $dispatcher = null,
  21. $formatter = null;
  22. /**
  23. * Constructor.
  24. *
  25. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  26. * @param sfFormatter $formatter An sfFormatter instance
  27. */
  28. public function __construct(sfEventDispatcher $dispatcher = null, sfFormatter $formatter = null)
  29. {
  30. $this->dispatcher = $dispatcher;
  31. $this->formatter = $formatter;
  32. }
  33. /**
  34. * Copies a file.
  35. *
  36. * This method only copies the file if the origin file is newer than the target file.
  37. *
  38. * By default, if the target already exists, it is not overriden.
  39. *
  40. * To override existing files, pass the "override" option.
  41. *
  42. * @param string $originFile The original filename
  43. * @param string $targetFile The target filename
  44. * @param array $options An array of options
  45. */
  46. public function copy($originFile, $targetFile, $options = array())
  47. {
  48. if (!array_key_exists('override', $options))
  49. {
  50. $options['override'] = false;
  51. }
  52. // we create target_dir if needed
  53. if (!is_dir(dirname($targetFile)))
  54. {
  55. $this->mkdirs(dirname($targetFile));
  56. }
  57. $mostRecent = false;
  58. if (file_exists($targetFile))
  59. {
  60. $statTarget = stat($targetFile);
  61. $stat_origin = stat($originFile);
  62. $mostRecent = ($stat_origin['mtime'] > $statTarget['mtime']) ? true : false;
  63. }
  64. if ($options['override'] || !file_exists($targetFile) || $mostRecent)
  65. {
  66. $this->logSection('file+', $targetFile);
  67. copy($originFile, $targetFile);
  68. }
  69. }
  70. /**
  71. * Creates a directory recursively.
  72. *
  73. * @param string $path The directory path
  74. * @param int $mode The directory mode
  75. *
  76. * @return bool true if the directory has been created, false otherwise
  77. */
  78. public function mkdirs($path, $mode = 0777)
  79. {
  80. if (is_dir($path))
  81. {
  82. return true;
  83. }
  84. $this->logSection('dir+', $path);
  85. return @mkdir($path, $mode, true);
  86. }
  87. /**
  88. * Creates empty files.
  89. *
  90. * @param mixed $files The filename, or an array of filenames
  91. */
  92. public function touch($files)
  93. {
  94. if (!is_array($files))
  95. {
  96. $files = array($files);
  97. }
  98. foreach ($files as $file)
  99. {
  100. $this->logSection('file+', $file);
  101. touch($file);
  102. }
  103. }
  104. /**
  105. * Removes files or directories.
  106. *
  107. * @param mixed $files A filename or an array of files to remove
  108. */
  109. public function remove($files)
  110. {
  111. if (!is_array($files))
  112. {
  113. $files = array($files);
  114. }
  115. $files = array_reverse($files);
  116. foreach ($files as $file)
  117. {
  118. if (is_dir($file) && !is_link($file))
  119. {
  120. $this->logSection('dir-', $file);
  121. rmdir($file);
  122. }
  123. else
  124. {
  125. $this->logSection(is_link($file) ? 'link-' : 'file-', $file);
  126. unlink($file);
  127. }
  128. }
  129. }
  130. /**
  131. * Change mode for an array of files or directories.
  132. *
  133. * @param array $files An array of files or directories
  134. * @param integer $mode The new mode
  135. * @param integer $umask The mode mask (octal)
  136. */
  137. public function chmod($files, $mode, $umask = 0000)
  138. {
  139. $currentUmask = umask();
  140. umask($umask);
  141. if (!is_array($files))
  142. {
  143. $files = array($files);
  144. }
  145. foreach ($files as $file)
  146. {
  147. $this->logSection(sprintf('chmod %o', $mode), $file);
  148. chmod($file, $mode);
  149. }
  150. umask($currentUmask);
  151. }
  152. /**
  153. * Renames a file.
  154. *
  155. * @param string $origin The origin filename
  156. * @param string $target The new filename
  157. */
  158. public function rename($origin, $target)
  159. {
  160. // we check that target does not exist
  161. if (is_readable($target))
  162. {
  163. throw new sfException(sprintf('Cannot rename because the target "%" already exist.', $target));
  164. }
  165. $this->logSection('rename', $origin.' > '.$target);
  166. rename($origin, $target);
  167. }
  168. /**
  169. * Creates a symbolic link or copy a directory.
  170. *
  171. * @param string $originDir The origin directory path
  172. * @param string $targetDir The symbolic link name
  173. * @param bool $copyOnWindows Whether to copy files if on windows
  174. */
  175. public function symlink($originDir, $targetDir, $copyOnWindows = false)
  176. {
  177. if (!function_exists('symlink') && $copyOnWindows)
  178. {
  179. $finder = sfFinder::type('any');
  180. $this->mirror($originDir, $targetDir, $finder);
  181. return;
  182. }
  183. $ok = false;
  184. if (is_link($targetDir))
  185. {
  186. if (readlink($targetDir) != $originDir)
  187. {
  188. unlink($targetDir);
  189. }
  190. else
  191. {
  192. $ok = true;
  193. }
  194. }
  195. if (!$ok)
  196. {
  197. $this->logSection('link+', $targetDir);
  198. symlink($originDir, $targetDir);
  199. }
  200. }
  201. /**
  202. * Mirrors a directory to another.
  203. *
  204. * @param string $originDir The origin directory
  205. * @param string $targetDir The target directory
  206. * @param sfFinder $finder An sfFinder instance
  207. * @param array $options An array of options (see copy())
  208. */
  209. public function mirror($originDir, $targetDir, $finder, $options = array())
  210. {
  211. foreach ($finder->relative()->in($originDir) as $file)
  212. {
  213. if (is_dir($originDir.DIRECTORY_SEPARATOR.$file))
  214. {
  215. $this->mkdirs($targetDir.DIRECTORY_SEPARATOR.$file);
  216. }
  217. else if (is_file($originDir.DIRECTORY_SEPARATOR.$file))
  218. {
  219. $this->copy($originDir.DIRECTORY_SEPARATOR.$file, $targetDir.DIRECTORY_SEPARATOR.$file, $options);
  220. }
  221. else if (is_link($originDir.DIRECTORY_SEPARATOR.$file))
  222. {
  223. $this->symlink($originDir.DIRECTORY_SEPARATOR.$file, $targetDir.DIRECTORY_SEPARATOR.$file);
  224. }
  225. else
  226. {
  227. throw new sfException(sprintf('Unable to guess "%s" file type.', $file));
  228. }
  229. }
  230. }
  231. /**
  232. * Executes a shell command.
  233. *
  234. * @param string $cmd The command to execute on the shell
  235. */
  236. public function sh($cmd)
  237. {
  238. $this->logSection('exec ', $cmd);
  239. ob_start();
  240. passthru($cmd.' 2>&1', $return);
  241. $content = ob_get_contents();
  242. ob_end_clean();
  243. if ($return > 0)
  244. {
  245. throw new sfException(sprintf('Problem executing command %s', "\n".$content));
  246. }
  247. return $content;
  248. }
  249. /**
  250. * Replaces tokens in an array of files.
  251. *
  252. * @param array $files An array of filenames
  253. * @param string $beginToken The begin token delimiter
  254. * @param string $endToken The end token delimiter
  255. * @param array $tokens An array of token/value pairs
  256. */
  257. public function replaceTokens($files, $beginToken, $endToken, $tokens)
  258. {
  259. if (!is_array($files))
  260. {
  261. $files = array($files);
  262. }
  263. foreach ($files as $file)
  264. {
  265. $content = file_get_contents($file);
  266. foreach ($tokens as $key => $value)
  267. {
  268. $content = str_replace($beginToken.$key.$endToken, $value, $content, $count);
  269. }
  270. $this->logSection('tokens', $file);
  271. file_put_contents($file, $content);
  272. }
  273. }
  274. /**
  275. * Logs a message in a section.
  276. *
  277. * @param string $section The section name
  278. * @param string $message The message
  279. * @param int $size The maximum size of a line
  280. */
  281. protected function logSection($section, $message, $size = null)
  282. {
  283. if (!$this->dispatcher)
  284. {
  285. return;
  286. }
  287. $message = $this->formatter ? $this->formatter->formatSection($section, $message, $size) : $section.' '.$message."\n";
  288. $this->dispatcher->notify(new sfEvent($this, 'command.log', array($message)));
  289. }
  290. }