5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.0.0
47 require_once 'PHPUnit/Util/Filter.php';
49 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
56 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
57 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
59 * @version Release: 3.3.17
60 * @link http://www.phpunit.de/
61 * @since Class available since Release 3.0.0
64 class PHPUnit_Util_Filesystem
66 protected static $buffer = array();
69 * Starts the collection of loaded files.
71 * @since Method available since Release 3.3.0
73 public static function collectStart()
75 self::$buffer = get_included_files();
79 * Stops the collection of loaded files and
80 * returns the names of the loaded files.
83 * @since Method available since Release 3.3.0
85 public static function collectEnd()
88 array_diff(get_included_files(), self::$buffer)
93 * Wrapper for file_exists() that searches the include_path.
97 * @author Mattis Stordalen Flister <mattis@xait.no>
98 * @since Method available since Release 3.2.9
100 public static function fileExistsInIncludePath($file)
102 if (file_exists($file)) {
103 return realpath($file);
106 $paths = explode(PATH_SEPARATOR, get_include_path());
108 foreach ($paths as $path) {
109 $fullpath = $path . DIRECTORY_SEPARATOR . $file;
111 if (file_exists($fullpath)) {
112 return realpath($fullpath);
120 * Returns the common path of a set of files.
122 * @param array $paths
124 * @since Method available since Release 3.1.0
126 public static function getCommonPath(array $paths)
128 $count = count($paths);
131 return dirname($paths[0]) . DIRECTORY_SEPARATOR;
136 for ($i = 0; $i < $count; $i++) {
137 $_paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
139 if (empty($_paths[$i][0])) {
140 $_paths[$i][0] = DIRECTORY_SEPARATOR;
150 for ($i = 0; $i < $count; $i++) {
151 if ($_paths[$i][$j] != $_paths[$i+1][$j]) {
158 $common .= $_paths[0][$j];
161 $common .= DIRECTORY_SEPARATOR;
172 * @param string $directory
174 * @throws RuntimeException
175 * @since Method available since Release 3.3.0
177 public static function getDirectory($directory)
179 if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
180 $directory .= DIRECTORY_SEPARATOR;
183 if (is_dir($directory) || mkdir($directory, 0777, TRUE)) {
186 throw new RuntimeException(
188 'Directory "%s" does not exist.',
196 * Returns a filesystem safe version of the passed filename.
197 * This function does not operate on full paths, just filenames.
199 * @param string $filename
201 * @author Michael Lively Jr. <m@digitalsandwich.com>
203 public static function getSafeFilename($filename)
205 /* characters allowed: A-Z, a-z, 0-9, _ and . */
206 return preg_replace('#[^\w.]#', '_', $filename);
210 * Reduces the paths by cutting the longest common start path.
217 * [/home/sb/PHPUnit/Samples/Money/Money.php] => Array
222 * [/home/sb/PHPUnit/Samples/Money/MoneyBag.php] => Array
234 * [Money.php] => Array
239 * [MoneyBag.php] => Array
246 * @param array $files
248 * @since Method available since Release 3.3.0
250 public static function reducePaths(&$files)
257 $paths = array_keys($files);
259 if (count($files) == 1) {
260 $commonPath = dirname($paths[0]);
261 $files[basename($paths[0])] = $files[$paths[0]];
263 unset($files[$paths[0]]);
268 $max = count($paths);
270 for ($i = 0; $i < $max; $i++) {
271 $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
273 if (empty($paths[$i][0])) {
274 $paths[$i][0] = DIRECTORY_SEPARATOR;
279 $max = count($paths);
282 for ($i = 0; $i < $max - 1; $i++) {
283 if (!isset($paths[$i][0]) ||
284 !isset($paths[$i+1][0]) ||
285 $paths[$i][0] != $paths[$i+1][0]) {
292 $commonPath .= $paths[0][0] . (($paths[0][0] != DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '');
294 for ($i = 0; $i < $max; $i++) {
295 array_shift($paths[$i]);
300 $original = array_keys($files);
301 $max = count($original);
303 for ($i = 0; $i < $max; $i++) {
304 $files[join('/', $paths[$i])] = $files[$original[$i]];
305 unset($files[$original[$i]]);