PHP 8 compatibility (curly brace string access)
[pharext/pharext] / src / pharext / License.php
1 <?php
2
3 namespace pharext;
4
5 trait License
6 {
7 function findLicense($dir, $file = null) {
8 if (isset($file)) {
9 return realpath("$dir/$file");
10 }
11
12 $names = [];
13 foreach (["{,UN}LICEN{S,C}{E,ING}", "COPY{,ING,RIGHT}"] as $name) {
14 $names[] = $this->mergeLicensePattern($name, strtolower($name));
15 }
16 $exts = [];
17 foreach (["t{,e}xt", "rst", "asc{,i,ii}", "m{,ark}d{,own}", "htm{,l}"] as $ext) {
18 $exts[] = $this->mergeLicensePattern(strtoupper($ext), $ext);
19 }
20
21 $pattern = "{". implode(",", $names) ."}{,.{". implode(",", $exts) ."}}";
22
23 if (($glob = glob("$dir/$pattern", GLOB_BRACE))) {
24 return current($glob);
25 }
26 }
27
28 private function mergeLicensePattern($upper, $lower) {
29 $pattern = "";
30 $length = strlen($upper);
31 for ($i = 0; $i < $length; ++$i) {
32 if ($lower[$i] === $upper[$i]) {
33 $pattern .= $upper[$i];
34 } else {
35 $pattern .= "[" . $upper[$i] . $lower[$i] . "]";
36 }
37 }
38 return $pattern;
39 }
40
41 public function readLicense($file) {
42 $text = file_get_contents($file);
43 switch (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
44 case "htm":
45 case "html":
46 $text = strip_tags($text);
47 break;
48 }
49 return $text;
50 }
51 }