1 Star 0 Fork 0

laoha0201/php-crud-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
build.php 3.25 KB
一键复制 编辑 原始数据 按行查看 历史
Maurits van der Schee 提交于 2020-11-28 21:40 . fix for #734
<?php
// combine src and vendor directories into a single file
function removeIgnored(string $dir, array &$entries, array $ignore)
{
foreach ($entries as $i => $entry) {
if (isset($ignore[$dir . '/' . $entry])) {
unset($entries[$i]);
}
}
}
function runDir(string $base, string $dir, array &$lines, array $ignore): int
{
$count = 0;
$entries = scandir($dir);
removeIgnored($dir, $entries, $ignore);
sort($entries);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$filename = "$base/$dir/$entry";
if (is_dir($filename)) {
$count += runDir($base, "$dir/$entry", $lines, $ignore);
}
}
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*<\?php\s+/s', '', $data, 1);
$data = preg_replace('/^.*?(vendor\/autoload|declare\s*\(\s*strict_types\s*=\s*1).*?$/m', '', $data);
array_push($lines, "// file: $dir/$entry");
if (!preg_match('/^\s*(namespace[^;]*);/', $data)) {
$data = "namespace;\n" . $data;
}
foreach (explode("\n", trim($data)) as $line) {
if ($line) {
$line = ' ' . $line;
}
$line = preg_replace('/^\s*(namespace[^;]*);/', '\1 {', $line);
array_push($lines, $line);
}
array_push($lines, '}');
array_push($lines, '');
$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
*
* Dependencies:
* - vendor/psr/*: PHP-FIG
* https://github.com/php-fig
* - vendor/nyholm/*: Tobias Nyholm
* https://github.com/Nyholm
**/
EOF;
foreach (explode("\n", $head) as $line) {
array_push($lines, $line);
}
}
function run(string $base, array $dirs, string $filename, array $ignore)
{
$lines = [];
$start = microtime(true);
addHeader($lines);
$ignore = array_flip($ignore);
$count = 0;
foreach ($dirs as $dir) {
$count += runDir($base, $dir, $lines, $ignore);
}
$data = implode("\n", $lines);
$data = preg_replace('/\n({)?\s*\n\s*\n/', "\n$1\n", $data);
file_put_contents('tmp_' . $filename, $data);
ob_start();
include 'tmp_' . $filename;
ob_end_clean();
rename('tmp_' . $filename, $filename);
$data = substr($data, 0, strrpos($data, "\n// file: src/index.php"));
file_put_contents(str_replace('.php', '.include.php', $filename), $data);
$end = microtime(true);
$time = ($end - $start) * 1000;
echo sprintf("%d files combined in %d ms into '%s'\n", $count, $time, $filename);
}
$ignore = [
'vendor/nyholm/psr7/src/Factory/HttplugFactory.php',
];
$directories = ['vendor/nyholm', 'src'];
if (!extension_loaded('psr')) {
array_unshift($directories, 'vendor/psr');
}
run(__DIR__, $directories, 'api.php', $ignore);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laoha0201/php-crud-api.git
git@gitee.com:laoha0201/php-crud-api.git
laoha0201
php-crud-api
php-crud-api
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385