spatie/laravel-directory-cleanup

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

1.10.0 2024-03-05 17:31 UTC

This package is auto-updated.

Last update: 2024-09-05 18:36:15 UTC


README

Latest Version on Packagist Software License run-tests Total Downloads

此包将删除目录中的旧文件。您可以使用配置文件指定特定目录中文件的最大年龄。

支持我们

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

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

安装

您可以通过composer安装此包

composer require spatie/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,
        ],
        */
    ],

    /*
     * 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的控制台内核中计划。

// 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);
    }
}

更新日志

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

测试

$ composer test

贡献

请参阅贡献指南以获取详细信息。

安全

如果您发现了有关安全的错误,请通过security@spatie.be发送邮件,而不是使用问题跟踪器。

鸣谢

许可证

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