PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/sfPHPUnit2Plugin/lib/PHPUnit-3.4.9/PHPUnit/Util/Filesystem.php

https://github.com/nathanlon/twical
PHP | 412 lines | 262 code | 33 blank | 117 comment | 32 complexity | bea4d2e76fdcc0000acaee1dabc28fde MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2010, 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-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.0.0
  44. */
  45. require_once 'PHPUnit/Util/Filter.php';
  46. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  47. /**
  48. * Filesystem helpers.
  49. *
  50. * @category Testing
  51. * @package PHPUnit
  52. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: 3.4.9
  56. * @link http://www.phpunit.de/
  57. * @since Class available since Release 3.0.0
  58. * @abstract
  59. */
  60. class PHPUnit_Util_Filesystem
  61. {
  62. /**
  63. * @var array
  64. */
  65. protected static $buffer = array();
  66. /**
  67. * @var array
  68. */
  69. protected static $hasBinary = array();
  70. /**
  71. * Maps class names to source file names:
  72. * - PEAR CS: Foo_Bar_Baz -> Foo/Bar/Baz.php
  73. * - Namespace: Foo\Bar\Baz -> Foo/Bar/Baz.php
  74. *
  75. * @param string $className
  76. * @return string
  77. * @since Method available since Release 3.4.0
  78. */
  79. public static function classNameToFilename($className)
  80. {
  81. return str_replace(
  82. array('_', '\\'),
  83. DIRECTORY_SEPARATOR,
  84. $className
  85. ) . '.php';
  86. }
  87. /**
  88. * Starts the collection of loaded files.
  89. *
  90. * @since Method available since Release 3.3.0
  91. */
  92. public static function collectStart()
  93. {
  94. self::$buffer = get_included_files();
  95. }
  96. /**
  97. * Stops the collection of loaded files and
  98. * returns the names of the loaded files.
  99. *
  100. * @return array
  101. * @since Method available since Release 3.3.0
  102. */
  103. public static function collectEnd()
  104. {
  105. return array_values(
  106. array_diff(get_included_files(), self::$buffer)
  107. );
  108. }
  109. /**
  110. * Stops the collection of loaded files and adds
  111. * the names of the loaded files to the blacklist.
  112. *
  113. * @return array
  114. * @since Method available since Release 3.4.6
  115. */
  116. public static function collectEndAndAddToBlacklist()
  117. {
  118. foreach (self::collectEnd() as $blacklistedFile) {
  119. PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
  120. }
  121. }
  122. /**
  123. * Wrapper for file_exists() that searches the include_path.
  124. *
  125. * @param string $file
  126. * @return mixed
  127. * @author Mattis Stordalen Flister <mattis@xait.no>
  128. * @since Method available since Release 3.2.9
  129. */
  130. public static function fileExistsInIncludePath($file)
  131. {
  132. if (file_exists($file)) {
  133. return realpath($file);
  134. }
  135. $paths = explode(PATH_SEPARATOR, get_include_path());
  136. foreach ($paths as $path) {
  137. $fullpath = $path . DIRECTORY_SEPARATOR . $file;
  138. if (file_exists($fullpath)) {
  139. return realpath($fullpath);
  140. }
  141. }
  142. return FALSE;
  143. }
  144. /**
  145. * Returns the common path of a set of files.
  146. *
  147. * @param array $paths
  148. * @return string
  149. * @since Method available since Release 3.1.0
  150. */
  151. public static function getCommonPath(array $paths)
  152. {
  153. $count = count($paths);
  154. if ($count == 1) {
  155. return dirname($paths[0]) . DIRECTORY_SEPARATOR;
  156. }
  157. $_paths = array();
  158. for ($i = 0; $i < $count; $i++) {
  159. $_paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
  160. if (empty($_paths[$i][0])) {
  161. $_paths[$i][0] = DIRECTORY_SEPARATOR;
  162. }
  163. }
  164. $common = '';
  165. $done = FALSE;
  166. $j = 0;
  167. $count--;
  168. while (!$done) {
  169. for ($i = 0; $i < $count; $i++) {
  170. if ($_paths[$i][$j] != $_paths[$i+1][$j]) {
  171. $done = TRUE;
  172. break;
  173. }
  174. }
  175. if (!$done) {
  176. $common .= $_paths[0][$j];
  177. if ($j > 0) {
  178. $common .= DIRECTORY_SEPARATOR;
  179. }
  180. }
  181. $j++;
  182. }
  183. return $common;
  184. }
  185. /**
  186. * @param string $directory
  187. * @return string
  188. * @throws RuntimeException
  189. * @since Method available since Release 3.3.0
  190. */
  191. public static function getDirectory($directory)
  192. {
  193. if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
  194. $directory .= DIRECTORY_SEPARATOR;
  195. }
  196. if (is_dir($directory) || mkdir($directory, 0777, TRUE)) {
  197. return $directory;
  198. } else {
  199. throw new PHPUnit_Framework_Exception(
  200. sprintf(
  201. 'Directory "%s" does not exist.',
  202. $directory
  203. )
  204. );
  205. }
  206. }
  207. /**
  208. * Returns a filesystem safe version of the passed filename.
  209. * This function does not operate on full paths, just filenames.
  210. *
  211. * @param string $filename
  212. * @return string
  213. * @author Michael Lively Jr. <m@digitalsandwich.com>
  214. */
  215. public static function getSafeFilename($filename)
  216. {
  217. /* characters allowed: A-Z, a-z, 0-9, _ and . */
  218. return preg_replace('#[^\w.]#', '_', $filename);
  219. }
  220. /**
  221. * @param string $binary
  222. * @return boolean
  223. * @since Method available since Release 3.4.0
  224. */
  225. public static function hasBinary($binary)
  226. {
  227. if (!isset(self::$hasBinary[$binary])) {
  228. if (substr(php_uname('s'), 0, 7) == 'Windows') {
  229. $binary .= '.exe';
  230. }
  231. self::$hasBinary[$binary] = FALSE;
  232. $openBaseDir = ini_get('open_basedir');
  233. if (is_string($openBaseDir) && !empty($openBaseDir)) {
  234. $safeModeExecDir = ini_get('safe_mode_exec_dir');
  235. $var = $openBaseDir;
  236. if (is_string($safeModeExecDir) && !empty($safeModeExecDir)) {
  237. $var .= PATH_SEPARATOR . $safeModeExecDir;
  238. }
  239. } else {
  240. if (isset($_ENV['PATH'])) {
  241. $var = $_ENV['PATH'];
  242. }
  243. else if (isset($_ENV['Path'])) {
  244. $var = $_ENV['Path'];
  245. }
  246. else if (isset($_SERVER['PATH'])) {
  247. $var = $_SERVER['PATH'];
  248. }
  249. else if (isset($_SERVER['Path'])) {
  250. $var = $_SERVER['Path'];
  251. }
  252. }
  253. if (isset($var)) {
  254. $paths = explode(PATH_SEPARATOR, $var);
  255. } else {
  256. $paths = array();
  257. }
  258. foreach ($paths as $path) {
  259. $_path = $path . DIRECTORY_SEPARATOR . $binary;
  260. if (file_exists($_path) && is_executable($_path)) {
  261. self::$hasBinary[$binary] = TRUE;
  262. break;
  263. }
  264. }
  265. }
  266. return self::$hasBinary[$binary];
  267. }
  268. /**
  269. * Reduces the paths by cutting the longest common start path.
  270. *
  271. * For instance,
  272. *
  273. * <code>
  274. * Array
  275. * (
  276. * [/home/sb/PHPUnit/Samples/Money/Money.php] => Array
  277. * (
  278. * ...
  279. * )
  280. *
  281. * [/home/sb/PHPUnit/Samples/Money/MoneyBag.php] => Array
  282. * (
  283. * ...
  284. * )
  285. * )
  286. * </code>
  287. *
  288. * is reduced to
  289. *
  290. * <code>
  291. * Array
  292. * (
  293. * [Money.php] => Array
  294. * (
  295. * ...
  296. * )
  297. *
  298. * [MoneyBag.php] => Array
  299. * (
  300. * ...
  301. * )
  302. * )
  303. * </code>
  304. *
  305. * @param array $files
  306. * @return string
  307. * @since Method available since Release 3.3.0
  308. */
  309. public static function reducePaths(&$files)
  310. {
  311. if (empty($files)) {
  312. return '.';
  313. }
  314. $commonPath = '';
  315. $paths = array_keys($files);
  316. if (count($files) == 1) {
  317. $commonPath = dirname($paths[0]);
  318. $files[basename($paths[0])] = $files[$paths[0]];
  319. unset($files[$paths[0]]);
  320. return $commonPath;
  321. }
  322. $max = count($paths);
  323. for ($i = 0; $i < $max; $i++) {
  324. $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
  325. if (empty($paths[$i][0])) {
  326. $paths[$i][0] = DIRECTORY_SEPARATOR;
  327. }
  328. }
  329. $done = FALSE;
  330. $max = count($paths);
  331. while (!$done) {
  332. for ($i = 0; $i < $max - 1; $i++) {
  333. if (!isset($paths[$i][0]) ||
  334. !isset($paths[$i+1][0]) ||
  335. $paths[$i][0] != $paths[$i+1][0]) {
  336. $done = TRUE;
  337. break;
  338. }
  339. }
  340. if (!$done) {
  341. $commonPath .= $paths[0][0] . (($paths[0][0] != DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '');
  342. for ($i = 0; $i < $max; $i++) {
  343. array_shift($paths[$i]);
  344. }
  345. }
  346. }
  347. $original = array_keys($files);
  348. $max = count($original);
  349. for ($i = 0; $i < $max; $i++) {
  350. $files[join('/', $paths[$i])] = $files[$original[$i]];
  351. unset($files[$original[$i]]);
  352. }
  353. ksort($files);
  354. return $commonPath;
  355. }
  356. }
  357. ?>