12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
-
- function prioritySort(string $dir, array $entries, array $priority): array
- {
- $result = array();
- foreach ($priority as $file) {
- if (dirname($file) == $dir) {
- if (in_array(basename($file), $entries)) {
- array_push($result, basename($file));
- }
- }
- }
- $entries = array_diff($entries, $result);
- sort($entries);
- foreach ($entries as $entry) {
- array_push($result, $entry);
- }
- return $result;
- }
-
- function runDir(string $base, string $dir, array &$lines, array $priority): int
- {
- $count = 0;
- $entries = scandir($dir);
- $entries = prioritySort($dir, $entries, $priority);
- foreach ($entries as $entry) {
- if ($entry === '.' || $entry === '..') {
- continue;
- }
- $filename = "$base/$dir/$entry";
- if (is_dir($filename)) {
- $count += runDir($base, "$dir/$entry", $lines, $priority);
- }
- }
- foreach ($entries as $entry) {
- $filename = "$base/$dir/$entry";
- if (is_file($filename)) {
- if (substr($entry, -4) != '.php') {
- continue;
- }
- $data = file_get_contents($filename);
- $data = preg_replace('|/\*\*.*?\*/|s', '', $data);
- array_push($lines, "// file: $dir/$entry");
- foreach (explode("\n", $data) as $line) {
- if (!preg_match('/^<\?php|^namespace |^use |spl_autoload_register|declare\s*\(\s*strict_types\s*=\s*1|^\s*\/\//', $line)) {
- array_push($lines, $line);
- }
- }
- $count++;
- }
- }
- return $count;
- }
-
- function addHeader(array &$lines)
- {
- $head = <<<EOF
- <?php
- /**
- * PHP-CRUD-API v2 License: MIT
- * Maurits van der Schee: maurits@vdschee.nl
- * https://github.com/mevdschee/php-crud-api
- **/
-
- namespace Tqdev\PhpCrudApi;
-
- EOF;
- foreach (explode("\n", $head) as $line) {
- array_push($lines, $line);
- }
- }
-
- function run(string $base, string $dir, string $filename, array $priority)
- {
- $lines = [];
- $start = microtime(true);
- addHeader($lines);
- $count = runDir($base, 'src', $lines, $priority);
- $data = implode("\n", $lines);
- $data = preg_replace('/\n\s*\n\s*\n/', "\n\n", $data);
- file_put_contents('tmp_' . $filename, $data);
- ob_start();
- include 'tmp_' . $filename;
- ob_end_clean();
- rename('tmp_' . $filename, $filename);
- $end = microtime(true);
- $time = ($end - $start) * 1000;
- echo sprintf("%d files combined in %d ms into '%s'\n", $count, $time, $filename);
- }
-
- run(__DIR__, 'src', 'api.php', ['src/Psr']);
|