phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Core / CodeGen / Util / File.php
CommitLineData
e39816b5
ARW
1<?php
2
b5c2afd0
EM
3/**
4 * Class CRM_Core_CodeGen_Util_File
5 */
e39816b5 6class CRM_Core_CodeGen_Util_File {
2558c2b0
EM
7 /**
8 * @param $dir
9 * @param int $perm
10 */
e39816b5
ARW
11 static function createDir($dir, $perm = 0755) {
12 if (!is_dir($dir)) {
13 mkdir($dir, $perm, TRUE);
14 }
15 }
16
2558c2b0
EM
17 /**
18 * @param $dir
19 */
0eed9ecc 20 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 */
e39816b5 37 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 /**
48 * Calculate a cumulative digest based on a collection of files
49 *
50 * @param array $files list of file names (strings)
77b97be7
EM
51 * @param callable|string $digest a one-way hash function (string => string)
52 *
3530751a
TO
53 * @return string
54 */
55 static function digestAll($files, $digest = 'md5') {
56 $buffer = '';
57 foreach ($files as $file) {
58 $buffer .= $digest(file_get_contents($file));
59 }
60 return $digest($buffer);
61 }
62
63 /**
2469a0dd 64 * Find the path to the main Civi source tree
3530751a 65 *
2469a0dd 66 * @return string
3530751a
TO
67 * @throws RuntimeException
68 */
69 static function findCoreSourceDir() {
70 $path = str_replace(DIRECTORY_SEPARATOR, '/', __DIR__);
71 if (!preg_match(':(.*)/CRM/Core/CodeGen/Util:', $path, $matches)) {
72 throw new RuntimeException("Failed to determine path of code-gen");
73 }
74
75 return $matches[1];
76 }
77
78 /**
79 * Find files in several directories using several filename patterns
80 *
3530751a
TO
81 * @param array $pairs each item is an array(0 => $searchBaseDir, 1 => $filePattern)
82 * @return array of file paths
83 */
84 static function findManyFiles($pairs) {
85 $files = array();
86 foreach ($pairs as $pair) {
87 list ($dir, $pattern) = $pair;
88 $files = array_merge($files, CRM_Utils_File::findFiles($dir, $pattern));
89 }
90 return $files;
91 }
e39816b5 92}