PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/task/sfFilesystem.class.php

http://opac-sbweb.googlecode.com/
PHP | 400 lines | 237 code | 44 blank | 119 comment | 43 complexity | e22c1620ab570152ea559603ddd3bc1a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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 14523 2009-01-07 10:33:54Z 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. * Creates a symbolic link using a relative path if possible.
  203. *
  204. * @param string $originDir The origin directory path
  205. * @param string $targetDir The symbolic link name
  206. * @param bool $copyOnWindows Whether to copy files if on windows
  207. */
  208. public function relativeSymlink($originDir, $targetDir, $copyOnWindows = false)
  209. {
  210. if (function_exists('symlink') || !$copyOnWindows)
  211. {
  212. $originDir = $this->calculateRelativeDir($targetDir, $originDir);
  213. }
  214. $this->symlink($originDir, $targetDir, $copyOnWindows);
  215. }
  216. /**
  217. * Mirrors a directory to another.
  218. *
  219. * @param string $originDir The origin directory
  220. * @param string $targetDir The target directory
  221. * @param sfFinder $finder An sfFinder instance
  222. * @param array $options An array of options (see copy())
  223. */
  224. public function mirror($originDir, $targetDir, $finder, $options = array())
  225. {
  226. foreach ($finder->relative()->in($originDir) as $file)
  227. {
  228. if (is_dir($originDir.DIRECTORY_SEPARATOR.$file))
  229. {
  230. $this->mkdirs($targetDir.DIRECTORY_SEPARATOR.$file);
  231. }
  232. else if (is_file($originDir.DIRECTORY_SEPARATOR.$file))
  233. {
  234. $this->copy($originDir.DIRECTORY_SEPARATOR.$file, $targetDir.DIRECTORY_SEPARATOR.$file, $options);
  235. }
  236. else if (is_link($originDir.DIRECTORY_SEPARATOR.$file))
  237. {
  238. $this->symlink($originDir.DIRECTORY_SEPARATOR.$file, $targetDir.DIRECTORY_SEPARATOR.$file);
  239. }
  240. else
  241. {
  242. throw new sfException(sprintf('Unable to guess "%s" file type.', $file));
  243. }
  244. }
  245. }
  246. /**
  247. * Executes a shell command.
  248. *
  249. * @param string $cmd The command to execute on the shell
  250. */
  251. public function sh($cmd)
  252. {
  253. $this->logSection('exec ', $cmd);
  254. ob_start();
  255. passthru($cmd.' 2>&1', $return);
  256. $content = ob_get_contents();
  257. ob_end_clean();
  258. if ($return > 0)
  259. {
  260. throw new sfException(sprintf('Problem executing command %s', "\n".$content));
  261. }
  262. return $content;
  263. }
  264. /**
  265. * Replaces tokens in an array of files.
  266. *
  267. * @param array $files An array of filenames
  268. * @param string $beginToken The begin token delimiter
  269. * @param string $endToken The end token delimiter
  270. * @param array $tokens An array of token/value pairs
  271. */
  272. public function replaceTokens($files, $beginToken, $endToken, $tokens)
  273. {
  274. if (!is_array($files))
  275. {
  276. $files = array($files);
  277. }
  278. foreach ($files as $file)
  279. {
  280. $content = file_get_contents($file);
  281. foreach ($tokens as $key => $value)
  282. {
  283. $content = str_replace($beginToken.$key.$endToken, $value, $content, $count);
  284. }
  285. $this->logSection('tokens', $file);
  286. file_put_contents($file, $content);
  287. }
  288. }
  289. /**
  290. * Logs a message in a section.
  291. *
  292. * @param string $section The section name
  293. * @param string $message The message
  294. * @param int $size The maximum size of a line
  295. */
  296. protected function logSection($section, $message, $size = null)
  297. {
  298. if (!$this->dispatcher)
  299. {
  300. return;
  301. }
  302. $message = $this->formatter ? $this->formatter->formatSection($section, $message, $size) : $section.' '.$message."\n";
  303. $this->dispatcher->notify(new sfEvent($this, 'command.log', array($message)));
  304. }
  305. /**
  306. * Calculates the relative path from one to another directory.
  307. * If they share no common path the absolute target dir is returned
  308. *
  309. * @param string $from directory from that the relative path shall be calculated
  310. * @param string $to target directory
  311. */
  312. protected function calculateRelativeDir($from, $to)
  313. {
  314. $from = $this->canonicalizePath($from);
  315. $to = $this->canonicalizePath($to);
  316. $commonLength = 0;
  317. $minPathLength = min(strlen($from), strlen($to));
  318. // count how many chars the strings have in common
  319. for ($i = 0; $i < $minPathLength; $i++)
  320. {
  321. if ($from[$i] != $to[$i]) break;
  322. if ($from[$i] == DIRECTORY_SEPARATOR) $commonLength = $i + 1;
  323. }
  324. if ($commonLength)
  325. {
  326. $levelUp = substr_count($from, DIRECTORY_SEPARATOR, $commonLength);
  327. // up that many level
  328. $relativeDir = str_repeat("..".DIRECTORY_SEPARATOR, $levelUp);
  329. // down the remaining $to path
  330. $relativeDir .= substr($to, $commonLength);
  331. return $relativeDir;
  332. }
  333. return $to;
  334. }
  335. protected function canonicalizePath($path)
  336. {
  337. if (empty($path)) return '';
  338. $out=array();
  339. foreach( explode(DIRECTORY_SEPARATOR, $path) as $i => $fold)
  340. {
  341. if ($fold=='' || $fold=='.') continue;
  342. if ($fold=='..' && $i>0 && end($out)!='..')
  343. {
  344. array_pop($out);
  345. }
  346. else
  347. {
  348. $out[]= $fold;
  349. }
  350. }
  351. $result = $path{0} == DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : '';
  352. $result .= join(DIRECTORY_SEPARATOR, $out);
  353. $result .= $path{strlen($path)-1} == DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : '';
  354. return $result;
  355. }
  356. }