This post was updated 756 days ago and some of the ideas may be out of date.

为了方便打包源码部署到线上,写了个提取最近修改了哪些文件

<?php

/**
 * Created by PhpStorm.
 * User: LinFei
 * Created time 2022/09/15 15:47:45
 * E-mail: fly@eyabc.cn
 */
declare (strict_types=1);

// 过滤的目录或文件
$packageFilter = [
    '.env',
    '.git',
    '.idea',
    '.vscode',
    'runtime',
    '__package',
    'package.php',
];

// 查询的目录
$root = dirname(__FILE__);

// 指定开始的时间
$checkTime = strtotime('2022-09-10 22:06:13');

find($root, function (SplFileInfo $item, bool $isDir) use ($packageFilter, $root, $checkTime) {
    $filename = $item->getPathname();

    foreach ($packageFilter as $value) {
        // 分隔符替换
        $value = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $value);
        $value = __DIR__ . DIRECTORY_SEPARATOR . ltrim($value, DIRECTORY_SEPARATOR);
        if (strpos($filename, $value) !== false) {
            return;
        }
    }

    if ($checkTime > max($item->getMTime(), $item->getCTime())) {
        return;
    }

    $savePath = str_replace($root, $root . DIRECTORY_SEPARATOR . '__package', $filename);
    $saveDir = $isDir ? $savePath : dirname($savePath);

    if (!is_dir($saveDir)) {
        $result = mkdir($saveDir, 0777, true);
        echo 'mkdir: ', $saveDir, $result ? ' successfully' : ' failure', PHP_EOL;
    }

    if (!$isDir) {
        $result = copy($filename, $savePath);
        echo 'copy: ', $filename, $result ? ' successfully' : ' failure', PHP_EOL;
    }
});


function find(string $root, Closure $filter): void
{
    $items = new FilesystemIterator($root);

    /** @var SplFileInfo $item */
    foreach ($items as $item) {
        if ($item->isDir() && !$item->isLink()) {
            $filter($item, true);
            find($item->getPathname(), $filter);
            continue;
        }

        $filter($item, false);
    }
}