nguyenhiep/laravel-directory-cleanup

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

1.8.0 2021-01-15 15:52 UTC

This package is auto-updated.

Last update: 2024-09-09 12:02:07 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的控制台内核中计划。

// 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)。请参阅许可证文件以获取更多信息。