Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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