minh7721/laravel-directory-cleanup

此包将从指定的目录中删除过期的文件。

1.10.0 2024-05-06 06:55 UTC

This package is auto-updated.

Last update: 2024-09-06 07:36:15 UTC


README

Latest Version on Packagist Software License Test Status PHP CS Fixer Status Total Downloads

此包将从目录中删除旧文件。您可以使用配置文件来指定目录中文件的最大存活时间。

支持我们

我们投入了大量资源来创建最佳开源包。您可以通过购买我们的付费产品之一来支持我们。

我们非常感激您从家乡寄给我们明信片,说明您正在使用我们的哪些包。您可以在我们的联系页面上找到我们的地址。我们将在我们的虚拟明信片墙上公布所有收到的明信片。

安装

您可以通过composer安装此包

composer require nguyenhiep/laravel-directory-cleanup

在Laravel 5.5中,服务提供程序将自动注册。在框架的较旧版本中,只需在config/app.php文件中添加服务提供程序即可

'providers' => [
    ...
    Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider::class,

];

接下来,您必须发布配置文件

php artisan vendor:publish --provider="Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider"

这是发布的配置文件laravel-directory-cleanup的内容

return [

    'directories' => [

        /*
         * Here you can specify which directories need to be cleanup. All files older than
         * the specified amount of minutes will be deleted.
         */

        /*
        'path/to/a/directory' => [
            'deleteAllOlderThanMinutes' => 60 * 24,
            'extensions' => "*" // "*"|"png,jpg"
        ],
        */
    ],

    /*
     * If a file is older than the amount of minutes specified, a cleanup policy will decide if that file
     * should be deleted. By default every file that is older than the specified amount of minutes
     * will be deleted.
     *
     * You can customize this behaviour by writing your own clean up policy.  A valid policy
     * is any class that implements `Spatie\DirectoryCleanup\Policies\CleanupPolicy`.
     */
    'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\DeleteEverything::class,
];

使用方法

在配置文件中指定需要清理的目录。

当运行控制台命令clean:directories时,所有指定目录中比deleteAllOlderThanMinutes早的文件将被删除。空子目录也将被删除。

此命令可以安排在Laravel的console kernel中。

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('clean:directories')->daily();
}

编写自定义清理策略

如果您想在删除文件之前应用额外的条件逻辑,您可以用自定义的替换默认的cleanup_policy。创建一个实现了Spatie\DirectoryCleanup\Policies\CleanupPolicy的类,并将您的逻辑添加到shouldDelete方法中。

// app/CleanupPolicies/MyPolicy.php

namespace App\CleanupPolicies;

use Symfony\Component\Finder\SplFileInfo;
use Spatie\DirectoryCleanup\Policies\CleanupPolicy;

class MyPolicy implements CleanupPolicy
{
    public function shouldDelete(SplFileInfo $file) : bool
    {
        $filesToKeep = ['robots.txt'];

        return ! in_array($file->getFilename(), $filesToKeep);
    }
}

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何安全问题,请通过电子邮件freek@spatie.be而不是使用问题跟踪器。

致谢

许可协议

MIT许可协议(MIT)。请参阅许可文件以获取更多信息。