|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Krak\PhpInc; |
| 4 | + |
| 5 | +use function iter\reduce, |
| 6 | + iter\filter; |
| 7 | + |
| 8 | +/** only match lower case filenames */ |
| 9 | +function lowerCaseMatch() { |
| 10 | + return function($finfo) { |
| 11 | + $first = $finfo->getFilename()[0]; |
| 12 | + return ctype_lower($first); |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function extMatch($ext) { |
| 17 | + if (!is_array($ext)) { |
| 18 | + $ext = [$ext]; |
| 19 | + } |
| 20 | + |
| 21 | + return function($finfo) use ($ext) { |
| 22 | + return in_array($finfo->getExtension(), $ext); |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +/** exclude certain paths, if the path matches the re, it will be excluded */ |
| 27 | +function excludePathMatch($path_re) { |
| 28 | + return function($finfo) use ($path_re) { |
| 29 | + $res = preg_match($path_re, $finfo->getPathname()); |
| 30 | + return !boolval($res); |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function orMatch($matches) { |
| 35 | + return function($finfo) use ($matches) { |
| 36 | + foreach ($matches as $match) { |
| 37 | + if ($match($finfo)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return false; |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function andMatch($matches) { |
| 47 | + return function($finfo) use ($matches) { |
| 48 | + foreach ($matches as $match) { |
| 49 | + if (!$match($finfo)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function scanSrc($match) { |
| 59 | + return function($path) use ($match) { |
| 60 | + $files = new \RecursiveDirectoryIterator($path); |
| 61 | + $files = new \RecursiveIteratorIterator($files); |
| 62 | + $files = filter(function($finfo) { |
| 63 | + return $finfo->isFile(); |
| 64 | + }, $files); |
| 65 | + $files = filter(function($file) use ($match) { |
| 66 | + return $match($file); |
| 67 | + }, $files); |
| 68 | + return $files; |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +function genIncFile() { |
| 73 | + return function($base, $files) { |
| 74 | + $include_php = reduce(function($acc, $file) use ($base) { |
| 75 | + return $acc . sprintf( |
| 76 | + "require_once __DIR__ . '%s';\n", |
| 77 | + str_replace($base, '', $file->getPathname()) |
| 78 | + ); |
| 79 | + }, $files, ''); |
| 80 | + |
| 81 | + return sprintf("<?php\n\n%s", $include_php); |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function phpInc($scan, $gen) { |
| 86 | + return function($path) use ($scan, $gen) { |
| 87 | + $path = realpath($path); |
| 88 | + return $gen($path, $scan($path)); |
| 89 | + }; |
| 90 | +} |
0 commit comments