comparison vendor/phpunit/php-file-iterator/src/Facade.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the File_Iterator package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Façade implementation that uses File_Iterator_Factory to create a
13 * File_Iterator that operates on an AppendIterator that contains an
14 * RecursiveDirectoryIterator for each given path. The list of unique
15 * files is returned as an array.
16 *
17 * @since Class available since Release 1.3.0
18 */
19 class File_Iterator_Facade
20 {
21 /**
22 * @param array|string $paths
23 * @param array|string $suffixes
24 * @param array|string $prefixes
25 * @param array $exclude
26 * @param bool $commonPath
27 * @return array
28 */
29 public function getFilesAsArray($paths, $suffixes = '', $prefixes = '', array $exclude = array(), $commonPath = FALSE)
30 {
31 if (is_string($paths)) {
32 $paths = array($paths);
33 }
34
35 $factory = new File_Iterator_Factory;
36 $iterator = $factory->getFileIterator(
37 $paths, $suffixes, $prefixes, $exclude
38 );
39
40 $files = array();
41
42 foreach ($iterator as $file) {
43 $file = $file->getRealPath();
44
45 if ($file) {
46 $files[] = $file;
47 }
48 }
49
50 foreach ($paths as $path) {
51 if (is_file($path)) {
52 $files[] = realpath($path);
53 }
54 }
55
56 $files = array_unique($files);
57 sort($files);
58
59 if ($commonPath) {
60 return array(
61 'commonPath' => $this->getCommonPath($files),
62 'files' => $files
63 );
64 } else {
65 return $files;
66 }
67 }
68
69 /**
70 * Returns the common path of a set of files.
71 *
72 * @param array $files
73 * @return string
74 */
75 protected function getCommonPath(array $files)
76 {
77 $count = count($files);
78
79 if ($count == 0) {
80 return '';
81 }
82
83 if ($count == 1) {
84 return dirname($files[0]) . DIRECTORY_SEPARATOR;
85 }
86
87 $_files = array();
88
89 foreach ($files as $file) {
90 $_files[] = $_fileParts = explode(DIRECTORY_SEPARATOR, $file);
91
92 if (empty($_fileParts[0])) {
93 $_fileParts[0] = DIRECTORY_SEPARATOR;
94 }
95 }
96
97 $common = '';
98 $done = FALSE;
99 $j = 0;
100 $count--;
101
102 while (!$done) {
103 for ($i = 0; $i < $count; $i++) {
104 if ($_files[$i][$j] != $_files[$i+1][$j]) {
105 $done = TRUE;
106 break;
107 }
108 }
109
110 if (!$done) {
111 $common .= $_files[0][$j];
112
113 if ($j > 0) {
114 $common .= DIRECTORY_SEPARATOR;
115 }
116 }
117
118 $j++;
119 }
120
121 return DIRECTORY_SEPARATOR . $common;
122 }
123 }