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