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