PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/task/sfFilesystem.class.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 487 lines | 304 code | 57 blank | 126 comment | 51 complexity | 219fa2c3bba04ab691e0ee7faf2722b2 MD5 | raw file
  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 31247 2010-10-26 12:26:15Z fabien $
  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 "%s" 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 ('\\' == DIRECTORY_SEPARATOR && $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 ('\\' != DIRECTORY_SEPARATOR || !$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. * @param array $stdoutCallback A callback for stdout output
  251. * @param array $stderrCallback A callback for stderr output
  252. *
  253. * @return array An array composed of the content output and the error output
  254. */
  255. public function execute($cmd, $stdoutCallback = null, $stderrCallback = null)
  256. {
  257. $this->logSection('exec ', $cmd);
  258. $descriptorspec = array(
  259. 1 => array('pipe', 'w'), // stdout
  260. 2 => array('pipe', 'w'), // stderr
  261. );
  262. $process = proc_open($cmd, $descriptorspec, $pipes);
  263. if (!is_resource($process))
  264. {
  265. throw new RuntimeException('Unable to execute the command.');
  266. }
  267. stream_set_blocking($pipes[1], false);
  268. stream_set_blocking($pipes[2], false);
  269. $output = '';
  270. $err = '';
  271. while (!feof($pipes[1]) || !feof($pipes[2]))
  272. {
  273. foreach ($pipes as $key => $pipe)
  274. {
  275. if (!$line = fread($pipe, 128))
  276. {
  277. continue;
  278. }
  279. if (1 == $key)
  280. {
  281. // stdout
  282. $output .= $line;
  283. if ($stdoutCallback)
  284. {
  285. call_user_func($stdoutCallback, $line);
  286. }
  287. }
  288. else
  289. {
  290. // stderr
  291. $err .= $line;
  292. if ($stderrCallback)
  293. {
  294. call_user_func($stderrCallback, $line);
  295. }
  296. }
  297. }
  298. usleep(100000);
  299. }
  300. fclose($pipes[1]);
  301. fclose($pipes[2]);
  302. if (($return = proc_close($process)) > 0)
  303. {
  304. throw new RuntimeException('Problem executing command.', $return);
  305. }
  306. return array($output, $err);
  307. }
  308. /**
  309. * Replaces tokens in an array of files.
  310. *
  311. * @param array $files An array of filenames
  312. * @param string $beginToken The begin token delimiter
  313. * @param string $endToken The end token delimiter
  314. * @param array $tokens An array of token/value pairs
  315. */
  316. public function replaceTokens($files, $beginToken, $endToken, $tokens)
  317. {
  318. if (!is_array($files))
  319. {
  320. $files = array($files);
  321. }
  322. foreach ($files as $file)
  323. {
  324. $content = file_get_contents($file);
  325. foreach ($tokens as $key => $value)
  326. {
  327. $content = str_replace($beginToken.$key.$endToken, $value, $content, $count);
  328. }
  329. $this->logSection('tokens', $file);
  330. file_put_contents($file, $content);
  331. }
  332. }
  333. /**
  334. * Logs a message in a section.
  335. *
  336. * @param string $section The section name
  337. * @param string $message The message
  338. * @param int $size The maximum size of a line
  339. */
  340. protected function logSection($section, $message, $size = null)
  341. {
  342. if (!$this->dispatcher)
  343. {
  344. return;
  345. }
  346. $message = $this->formatter ? $this->formatter->formatSection($section, $message, $size) : $section.' '.$message."\n";
  347. $this->dispatcher->notify(new sfEvent($this, 'command.log', array($message)));
  348. }
  349. /**
  350. * Calculates the relative path from one to another directory.
  351. *
  352. * If the paths share no common path the absolute target dir is returned.
  353. *
  354. * @param string $from The directory from which to calculate the relative path
  355. * @param string $to The target directory
  356. *
  357. * @return string
  358. */
  359. protected function calculateRelativeDir($from, $to)
  360. {
  361. $from = $this->canonicalizePath($from);
  362. $to = $this->canonicalizePath($to);
  363. $commonLength = 0;
  364. $minPathLength = min(strlen($from), strlen($to));
  365. // count how many chars the strings have in common
  366. for ($i = 0; $i < $minPathLength; $i++)
  367. {
  368. if ($from[$i] != $to[$i])
  369. {
  370. break;
  371. }
  372. if (DIRECTORY_SEPARATOR == $from[$i])
  373. {
  374. $commonLength = $i + 1;
  375. }
  376. }
  377. if ($commonLength)
  378. {
  379. if (extension_loaded('mbstring'))
  380. {
  381. $levelUp = mb_substr_count(mb_strcut($from, $commonLength), DIRECTORY_SEPARATOR);
  382. }
  383. else
  384. {
  385. $levelUp = substr_count($from, DIRECTORY_SEPARATOR, $commonLength);
  386. }
  387. // up that many level
  388. $relativeDir = str_repeat('..'.DIRECTORY_SEPARATOR, $levelUp);
  389. // down the remaining $to path
  390. $relativeDir .= substr($to, $commonLength);
  391. return $relativeDir;
  392. }
  393. return $to;
  394. }
  395. /**
  396. * @param string A filesystem path
  397. *
  398. * @return string
  399. */
  400. protected function canonicalizePath($path)
  401. {
  402. if (empty($path))
  403. {
  404. return '';
  405. }
  406. $out = array();
  407. foreach (explode(DIRECTORY_SEPARATOR, $path) as $i => $fold)
  408. {
  409. if ('' == $fold || '.' == $fold)
  410. {
  411. continue;
  412. }
  413. if ('..' == $fold && $i > 0 && '..' != end($out))
  414. {
  415. array_pop($out);
  416. }
  417. else
  418. {
  419. $out[] = $fold;
  420. }
  421. }
  422. $result = DIRECTORY_SEPARATOR == $path[0] ? DIRECTORY_SEPARATOR : '';
  423. $result .= implode(DIRECTORY_SEPARATOR, $out);
  424. $result .= DIRECTORY_SEPARATOR == $path[strlen($path) - 1] ? DIRECTORY_SEPARATOR : '';
  425. return $result;
  426. }
  427. }