1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,107 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-/* |
|
4 |
- * This file is part of the Symfony package. |
|
5 |
- * |
|
6 |
- * (c) Fabien Potencier <fabien@symfony.com> |
|
7 |
- * |
|
8 |
- * For the full copyright and license information, please view the LICENSE |
|
9 |
- * file that was distributed with this source code. |
|
10 |
- */ |
|
11 |
- |
|
12 |
-namespace Symfony\Component\Finder; |
|
13 |
- |
|
14 |
-/** |
|
15 |
- * Gitignore matches against text. |
|
16 |
- * |
|
17 |
- * @author Ahmed Abdou <mail@ahmd.io> |
|
18 |
- */ |
|
19 |
-class Gitignore |
|
20 |
-{ |
|
21 |
- /** |
|
22 |
- * Returns a regexp which is the equivalent of the gitignore pattern. |
|
23 |
- * |
|
24 |
- * @param string $gitignoreFileContent |
|
25 |
- * |
|
26 |
- * @return string The regexp |
|
27 |
- */ |
|
28 |
- public static function toRegex(string $gitignoreFileContent): string |
|
29 |
- { |
|
30 |
- $gitignoreFileContent = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent); |
|
31 |
- $gitignoreLines = preg_split('/\r\n|\r|\n/', $gitignoreFileContent); |
|
32 |
- $gitignoreLines = array_map('trim', $gitignoreLines); |
|
33 |
- $gitignoreLines = array_filter($gitignoreLines); |
|
34 |
- |
|
35 |
- $ignoreLinesPositive = array_filter($gitignoreLines, function (string $line) { |
|
36 |
- return !preg_match('/^!/', $line); |
|
37 |
- }); |
|
38 |
- |
|
39 |
- $ignoreLinesNegative = array_filter($gitignoreLines, function (string $line) { |
|
40 |
- return preg_match('/^!/', $line); |
|
41 |
- }); |
|
42 |
- |
|
43 |
- $ignoreLinesNegative = array_map(function (string $line) { |
|
44 |
- return preg_replace('/^!(.*)/', '${1}', $line); |
|
45 |
- }, $ignoreLinesNegative); |
|
46 |
- $ignoreLinesNegative = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesNegative); |
|
47 |
- |
|
48 |
- $ignoreLinesPositive = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesPositive); |
|
49 |
- if (empty($ignoreLinesPositive)) { |
|
50 |
- return '/^$/'; |
|
51 |
- } |
|
52 |
- |
|
53 |
- if (empty($ignoreLinesNegative)) { |
|
54 |
- return sprintf('/%s/', implode('|', $ignoreLinesPositive)); |
|
55 |
- } |
|
56 |
- |
|
57 |
- return sprintf('/(?=^(?:(?!(%s)).)*$)(%s)/', implode('|', $ignoreLinesNegative), implode('|', $ignoreLinesPositive)); |
|
58 |
- } |
|
59 |
- |
|
60 |
- private static function getRegexFromGitignore(string $gitignorePattern): string |
|
61 |
- { |
|
62 |
- $regex = '('; |
|
63 |
- if (0 === strpos($gitignorePattern, '/')) { |
|
64 |
- $gitignorePattern = substr($gitignorePattern, 1); |
|
65 |
- $regex .= '^'; |
|
66 |
- } else { |
|
67 |
- $regex .= '(^|\/)'; |
|
68 |
- } |
|
69 |
- |
|
70 |
- if ('/' === $gitignorePattern[\strlen($gitignorePattern) - 1]) { |
|
71 |
- $gitignorePattern = substr($gitignorePattern, 0, -1); |
|
72 |
- } |
|
73 |
- |
|
74 |
- $iMax = \strlen($gitignorePattern); |
|
75 |
- for ($i = 0; $i < $iMax; ++$i) { |
|
76 |
- $doubleChars = substr($gitignorePattern, $i, 2); |
|
77 |
- if ('**' === $doubleChars) { |
|
78 |
- $regex .= '.+'; |
|
79 |
- ++$i; |
|
80 |
- continue; |
|
81 |
- } |
|
82 |
- |
|
83 |
- $c = $gitignorePattern[$i]; |
|
84 |
- switch ($c) { |
|
85 |
- case '*': |
|
86 |
- $regex .= '[^\/]+'; |
|
87 |
- break; |
|
88 |
- case '/': |
|
89 |
- case '.': |
|
90 |
- case ':': |
|
91 |
- case '(': |
|
92 |
- case ')': |
|
93 |
- case '{': |
|
94 |
- case '}': |
|
95 |
- $regex .= '\\'.$c; |
|
96 |
- break; |
|
97 |
- default: |
|
98 |
- $regex .= $c; |
|
99 |
- } |
|
100 |
- } |
|
101 |
- |
|
102 |
- $regex .= '($|\/)'; |
|
103 |
- $regex .= ')'; |
|
104 |
- |
|
105 |
- return $regex; |
|
106 |
- } |
|
107 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,107 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+/* |
|
4 |
+ * This file is part of the Symfony package. |
|
5 |
+ * |
|
6 |
+ * (c) Fabien Potencier <fabien@symfony.com> |
|
7 |
+ * |
|
8 |
+ * For the full copyright and license information, please view the LICENSE |
|
9 |
+ * file that was distributed with this source code. |
|
10 |
+ */ |
|
11 |
+ |
|
12 |
+namespace Symfony\Component\Finder; |
|
13 |
+ |
|
14 |
+/** |
|
15 |
+ * Gitignore matches against text. |
|
16 |
+ * |
|
17 |
+ * @author Ahmed Abdou <mail@ahmd.io> |
|
18 |
+ */ |
|
19 |
+class Gitignore |
|
20 |
+{ |
|
21 |
+ /** |
|
22 |
+ * Returns a regexp which is the equivalent of the gitignore pattern. |
|
23 |
+ * |
|
24 |
+ * @param string $gitignoreFileContent |
|
25 |
+ * |
|
26 |
+ * @return string The regexp |
|
27 |
+ */ |
|
28 |
+ public static function toRegex(string $gitignoreFileContent): string |
|
29 |
+ { |
|
30 |
+ $gitignoreFileContent = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent); |
|
31 |
+ $gitignoreLines = preg_split('/\r\n|\r|\n/', $gitignoreFileContent); |
|
32 |
+ $gitignoreLines = array_map('trim', $gitignoreLines); |
|
33 |
+ $gitignoreLines = array_filter($gitignoreLines); |
|
34 |
+ |
|
35 |
+ $ignoreLinesPositive = array_filter($gitignoreLines, function (string $line) { |
|
36 |
+ return !preg_match('/^!/', $line); |
|
37 |
+ }); |
|
38 |
+ |
|
39 |
+ $ignoreLinesNegative = array_filter($gitignoreLines, function (string $line) { |
|
40 |
+ return preg_match('/^!/', $line); |
|
41 |
+ }); |
|
42 |
+ |
|
43 |
+ $ignoreLinesNegative = array_map(function (string $line) { |
|
44 |
+ return preg_replace('/^!(.*)/', '${1}', $line); |
|
45 |
+ }, $ignoreLinesNegative); |
|
46 |
+ $ignoreLinesNegative = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesNegative); |
|
47 |
+ |
|
48 |
+ $ignoreLinesPositive = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesPositive); |
|
49 |
+ if (empty($ignoreLinesPositive)) { |
|
50 |
+ return '/^$/'; |
|
51 |
+ } |
|
52 |
+ |
|
53 |
+ if (empty($ignoreLinesNegative)) { |
|
54 |
+ return sprintf('/%s/', implode('|', $ignoreLinesPositive)); |
|
55 |
+ } |
|
56 |
+ |
|
57 |
+ return sprintf('/(?=^(?:(?!(%s)).)*$)(%s)/', implode('|', $ignoreLinesNegative), implode('|', $ignoreLinesPositive)); |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+ private static function getRegexFromGitignore(string $gitignorePattern): string |
|
61 |
+ { |
|
62 |
+ $regex = '('; |
|
63 |
+ if (0 === strpos($gitignorePattern, '/')) { |
|
64 |
+ $gitignorePattern = substr($gitignorePattern, 1); |
|
65 |
+ $regex .= '^'; |
|
66 |
+ } else { |
|
67 |
+ $regex .= '(^|\/)'; |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ if ('/' === $gitignorePattern[\strlen($gitignorePattern) - 1]) { |
|
71 |
+ $gitignorePattern = substr($gitignorePattern, 0, -1); |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ $iMax = \strlen($gitignorePattern); |
|
75 |
+ for ($i = 0; $i < $iMax; ++$i) { |
|
76 |
+ $doubleChars = substr($gitignorePattern, $i, 2); |
|
77 |
+ if ('**' === $doubleChars) { |
|
78 |
+ $regex .= '.+'; |
|
79 |
+ ++$i; |
|
80 |
+ continue; |
|
81 |
+ } |
|
82 |
+ |
|
83 |
+ $c = $gitignorePattern[$i]; |
|
84 |
+ switch ($c) { |
|
85 |
+ case '*': |
|
86 |
+ $regex .= '[^\/]+'; |
|
87 |
+ break; |
|
88 |
+ case '/': |
|
89 |
+ case '.': |
|
90 |
+ case ':': |
|
91 |
+ case '(': |
|
92 |
+ case ')': |
|
93 |
+ case '{': |
|
94 |
+ case '}': |
|
95 |
+ $regex .= '\\'.$c; |
|
96 |
+ break; |
|
97 |
+ default: |
|
98 |
+ $regex .= $c; |
|
99 |
+ } |
|
100 |
+ } |
|
101 |
+ |
|
102 |
+ $regex .= '($|\/)'; |
|
103 |
+ $regex .= ')'; |
|
104 |
+ |
|
105 |
+ return $regex; |
|
106 |
+ } |
|
107 |
+} |