Commit | Line | Data |
---|---|---|
e39816b5 ARW |
1 | <?php |
2 | ||
b5c2afd0 EM |
3 | /** |
4 | * Class CRM_Core_CodeGen_Util_File | |
5 | */ | |
e39816b5 | 6 | class CRM_Core_CodeGen_Util_File { |
518fa0ee | 7 | |
2558c2b0 EM |
8 | /** |
9 | * @param $dir | |
10 | * @param int $perm | |
11 | */ | |
00be9182 | 12 | public static function createDir($dir, $perm = 0755) { |
e39816b5 ARW |
13 | if (!is_dir($dir)) { |
14 | mkdir($dir, $perm, TRUE); | |
15 | } | |
16 | } | |
17 | ||
2558c2b0 EM |
18 | /** |
19 | * @param $dir | |
20 | */ | |
00be9182 | 21 | public static function cleanTempDir($dir) { |
e39816b5 ARW |
22 | foreach (glob("$dir/*") as $tempFile) { |
23 | unlink($tempFile); | |
24 | } | |
25 | rmdir($dir); | |
0eed9ecc TO |
26 | if (preg_match(':^(.*)\.d$:', $dir, $matches)) { |
27 | if (file_exists($matches[1])) { | |
28 | unlink($matches[1]); | |
29 | } | |
30 | } | |
e39816b5 ARW |
31 | } |
32 | ||
2558c2b0 EM |
33 | /** |
34 | * @param $prefix | |
35 | * | |
36 | * @return string | |
37 | */ | |
00be9182 | 38 | public static function createTempDir($prefix) { |
0eed9ecc | 39 | $newTempDir = tempnam(sys_get_temp_dir(), $prefix) . '.d'; |
e39816b5 ARW |
40 | if (file_exists($newTempDir)) { |
41 | self::removeDir($newTempDir); | |
42 | } | |
43 | self::createDir($newTempDir); | |
44 | ||
45 | return $newTempDir; | |
46 | } | |
3530751a TO |
47 | |
48 | /** | |
fe482240 | 49 | * Calculate a cumulative digest based on a collection of files. |
3530751a | 50 | * |
6a0b768e TO |
51 | * @param array $files |
52 | * List of file names (strings). | |
77b97be7 EM |
53 | * @param callable|string $digest a one-way hash function (string => string) |
54 | * | |
3530751a TO |
55 | * @return string |
56 | */ | |
00be9182 | 57 | public static function digestAll($files, $digest = 'md5') { |
3530751a TO |
58 | $buffer = ''; |
59 | foreach ($files as $file) { | |
60 | $buffer .= $digest(file_get_contents($file)); | |
61 | } | |
62 | return $digest($buffer); | |
63 | } | |
64 | ||
65 | /** | |
fe482240 | 66 | * Find the path to the main Civi source tree. |
3530751a | 67 | * |
2469a0dd | 68 | * @return string |
3530751a TO |
69 | * @throws RuntimeException |
70 | */ | |
00be9182 | 71 | public static function findCoreSourceDir() { |
3530751a TO |
72 | $path = str_replace(DIRECTORY_SEPARATOR, '/', __DIR__); |
73 | if (!preg_match(':(.*)/CRM/Core/CodeGen/Util:', $path, $matches)) { | |
74 | throw new RuntimeException("Failed to determine path of code-gen"); | |
75 | } | |
76 | ||
77 | return $matches[1]; | |
78 | } | |
79 | ||
80 | /** | |
fe482240 | 81 | * Find files in several directories using several filename patterns. |
3530751a | 82 | * |
6a0b768e TO |
83 | * @param array $pairs |
84 | * Each item is an array(0 => $searchBaseDir, 1 => $filePattern). | |
a6c01b45 | 85 | * @return array |
16b10e64 | 86 | * Array of file paths |
3530751a | 87 | */ |
00be9182 | 88 | public static function findManyFiles($pairs) { |
be2fb01f | 89 | $files = []; |
3530751a TO |
90 | foreach ($pairs as $pair) { |
91 | list ($dir, $pattern) = $pair; | |
92 | $files = array_merge($files, CRM_Utils_File::findFiles($dir, $pattern)); | |
93 | } | |
ce22c206 | 94 | sort($files); |
3530751a TO |
95 | return $files; |
96 | } | |
96025800 | 97 | |
e39816b5 | 98 | } |