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

/phpunit/3.4.0/PHPUnit/Util/Filesystem.php

https://github.com/vivid-planet/library
PHP | 399 lines | 248 code | 33 blank | 118 comment | 32 complexity | 88e2b89199866aa1b0d3627addb18bb4 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Filesystem.php 5135 2009-08-27 08:37:36Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.0.0
  45. */
  46. require_once 'PHPUnit/Util/Filter.php';
  47. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  48. /**
  49. * Filesystem helpers.
  50. *
  51. * @category Testing
  52. * @package PHPUnit
  53. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpunit.de/
  58. * @since Class available since Release 3.0.0
  59. * @abstract
  60. */
  61. class PHPUnit_Util_Filesystem
  62. {
  63. /**
  64. * @var array
  65. */
  66. protected static $buffer = array();
  67. /**
  68. * @var array
  69. */
  70. protected static $hasBinary = array();
  71. /**
  72. * Maps class names to source file names:
  73. * - PEAR CS: Foo_Bar_Baz -> Foo/Bar/Baz.php
  74. * - Namespace: Foo\Bar\Baz -> Foo/Bar/Baz.php
  75. *
  76. * @param string $className
  77. * @return string
  78. * @since Method available since Release 3.4.0
  79. */
  80. public static function classNameToFilename($className)
  81. {
  82. return str_replace(
  83. array('_', '\\'),
  84. DIRECTORY_SEPARATOR,
  85. $className
  86. ) . '.php';
  87. }
  88. /**
  89. * Starts the collection of loaded files.
  90. *
  91. * @since Method available since Release 3.3.0
  92. */
  93. public static function collectStart()
  94. {
  95. self::$buffer = get_included_files();
  96. }
  97. /**
  98. * Stops the collection of loaded files and
  99. * returns the names of the loaded files.
  100. *
  101. * @return array
  102. * @since Method available since Release 3.3.0
  103. */
  104. public static function collectEnd()
  105. {
  106. return array_values(
  107. array_diff(get_included_files(), self::$buffer)
  108. );
  109. }
  110. /**
  111. * Wrapper for file_exists() that searches the include_path.
  112. *
  113. * @param string $file
  114. * @return mixed
  115. * @author Mattis Stordalen Flister <mattis@xait.no>
  116. * @since Method available since Release 3.2.9
  117. */
  118. public static function fileExistsInIncludePath($file)
  119. {
  120. if (file_exists($file)) {
  121. return realpath($file);
  122. }
  123. $paths = explode(PATH_SEPARATOR, get_include_path());
  124. foreach ($paths as $path) {
  125. $fullpath = $path . DIRECTORY_SEPARATOR . $file;
  126. if (file_exists($fullpath)) {
  127. return realpath($fullpath);
  128. }
  129. }
  130. return FALSE;
  131. }
  132. /**
  133. * Returns the common path of a set of files.
  134. *
  135. * @param array $paths
  136. * @return string
  137. * @since Method available since Release 3.1.0
  138. */
  139. public static function getCommonPath(array $paths)
  140. {
  141. $count = count($paths);
  142. if ($count == 1) {
  143. return dirname($paths[0]) . DIRECTORY_SEPARATOR;
  144. }
  145. $_paths = array();
  146. for ($i = 0; $i < $count; $i++) {
  147. $_paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
  148. if (empty($_paths[$i][0])) {
  149. $_paths[$i][0] = DIRECTORY_SEPARATOR;
  150. }
  151. }
  152. $common = '';
  153. $done = FALSE;
  154. $j = 0;
  155. $count--;
  156. while (!$done) {
  157. for ($i = 0; $i < $count; $i++) {
  158. if ($_paths[$i][$j] != $_paths[$i+1][$j]) {
  159. $done = TRUE;
  160. break;
  161. }
  162. }
  163. if (!$done) {
  164. $common .= $_paths[0][$j];
  165. if ($j > 0) {
  166. $common .= DIRECTORY_SEPARATOR;
  167. }
  168. }
  169. $j++;
  170. }
  171. return $common;
  172. }
  173. /**
  174. * @param string $directory
  175. * @return string
  176. * @throws RuntimeException
  177. * @since Method available since Release 3.3.0
  178. */
  179. public static function getDirectory($directory)
  180. {
  181. if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
  182. $directory .= DIRECTORY_SEPARATOR;
  183. }
  184. if (is_dir($directory) || mkdir($directory, 0777, TRUE)) {
  185. return $directory;
  186. } else {
  187. throw new PHPUnit_Framework_Exception(
  188. sprintf(
  189. 'Directory "%s" does not exist.',
  190. $directory
  191. )
  192. );
  193. }
  194. }
  195. /**
  196. * Returns a filesystem safe version of the passed filename.
  197. * This function does not operate on full paths, just filenames.
  198. *
  199. * @param string $filename
  200. * @return string
  201. * @author Michael Lively Jr. <m@digitalsandwich.com>
  202. */
  203. public static function getSafeFilename($filename)
  204. {
  205. /* characters allowed: A-Z, a-z, 0-9, _ and . */
  206. return preg_replace('#[^\w.]#', '_', $filename);
  207. }
  208. /**
  209. * @param string $binary
  210. * @return boolean
  211. * @since Method available since Release 3.4.0
  212. */
  213. public static function hasBinary($binary)
  214. {
  215. if (!isset(self::$hasBinary[$binary])) {
  216. if (substr(php_uname('s'), 0, 7) == 'Windows') {
  217. $binary .= '.exe';
  218. }
  219. self::$hasBinary[$binary] = FALSE;
  220. $openBaseDir = ini_get('open_basedir');
  221. if (is_string($openBaseDir) && !empty($openBaseDir)) {
  222. $safeModeExecDir = ini_get('safe_mode_exec_dir');
  223. $var = $openBaseDir;
  224. if (is_string($safeModeExecDir) && !empty($safeModeExecDir)) {
  225. $var .= PATH_SEPARATOR . $safeModeExecDir;
  226. }
  227. } else {
  228. if (isset($_ENV['PATH'])) {
  229. $var = $_ENV['PATH'];
  230. }
  231. else if (isset($_ENV['Path'])) {
  232. $var = $_ENV['Path'];
  233. }
  234. else if (isset($_SERVER['PATH'])) {
  235. $var = $_SERVER['PATH'];
  236. }
  237. else if (isset($_SERVER['Path'])) {
  238. $var = $_SERVER['Path'];
  239. }
  240. }
  241. if (isset($var)) {
  242. $paths = explode(PATH_SEPARATOR, $var);
  243. } else {
  244. $paths = array();
  245. }
  246. foreach ($paths as $path) {
  247. $_path = $path . DIRECTORY_SEPARATOR . $binary;
  248. if (file_exists($_path) && is_executable($_path)) {
  249. self::$hasBinary[$binary] = TRUE;
  250. break;
  251. }
  252. }
  253. }
  254. return self::$hasBinary[$binary];
  255. }
  256. /**
  257. * Reduces the paths by cutting the longest common start path.
  258. *
  259. * For instance,
  260. *
  261. * <code>
  262. * Array
  263. * (
  264. * [/home/sb/PHPUnit/Samples/Money/Money.php] => Array
  265. * (
  266. * ...
  267. * )
  268. *
  269. * [/home/sb/PHPUnit/Samples/Money/MoneyBag.php] => Array
  270. * (
  271. * ...
  272. * )
  273. * )
  274. * </code>
  275. *
  276. * is reduced to
  277. *
  278. * <code>
  279. * Array
  280. * (
  281. * [Money.php] => Array
  282. * (
  283. * ...
  284. * )
  285. *
  286. * [MoneyBag.php] => Array
  287. * (
  288. * ...
  289. * )
  290. * )
  291. * </code>
  292. *
  293. * @param array $files
  294. * @return string
  295. * @since Method available since Release 3.3.0
  296. */
  297. public static function reducePaths(&$files)
  298. {
  299. if (empty($files)) {
  300. return '.';
  301. }
  302. $commonPath = '';
  303. $paths = array_keys($files);
  304. if (count($files) == 1) {
  305. $commonPath = dirname($paths[0]);
  306. $files[basename($paths[0])] = $files[$paths[0]];
  307. unset($files[$paths[0]]);
  308. return $commonPath;
  309. }
  310. $max = count($paths);
  311. for ($i = 0; $i < $max; $i++) {
  312. $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
  313. if (empty($paths[$i][0])) {
  314. $paths[$i][0] = DIRECTORY_SEPARATOR;
  315. }
  316. }
  317. $done = FALSE;
  318. $max = count($paths);
  319. while (!$done) {
  320. for ($i = 0; $i < $max - 1; $i++) {
  321. if (!isset($paths[$i][0]) ||
  322. !isset($paths[$i+1][0]) ||
  323. $paths[$i][0] != $paths[$i+1][0]) {
  324. $done = TRUE;
  325. break;
  326. }
  327. }
  328. if (!$done) {
  329. $commonPath .= $paths[0][0] . (($paths[0][0] != DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '');
  330. for ($i = 0; $i < $max; $i++) {
  331. array_shift($paths[$i]);
  332. }
  333. }
  334. }
  335. $original = array_keys($files);
  336. $max = count($original);
  337. for ($i = 0; $i < $max; $i++) {
  338. $files[join('/', $paths[$i])] = $files[$original[$i]];
  339. unset($files[$original[$i]]);
  340. }
  341. ksort($files);
  342. return $commonPath;
  343. }
  344. }
  345. ?>