salesrender/plugin-component-directory-cleaner 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

salesrender/plugin-component-directory-cleaner

Composer 安装命令:

composer require salesrender/plugin-component-directory-cleaner

包简介

SalesRender plugin directory cleaner command

README 文档

README

Symfony Console command that recursively deletes files and directories older than a specified number of hours.

Overview

plugin-component-directory-cleaner provides a single Symfony Console command (cleaner:run) that cleans up stale files and empty directories from a given path. It traverses the target directory recursively using PHP's RecursiveDirectoryIterator and RecursiveIteratorIterator, comparing each item's modification time (mtime) against a configurable timeout threshold in hours.

In the SalesRender plugin ecosystem, plugins frequently generate temporary files -- uploaded data, export results, intermediate processing artifacts, and similar ephemeral content. Without periodic cleanup, these files accumulate and consume disk space. This component solves that problem by providing a ready-to-use console command that can be invoked manually, via cron, or programmatically.

The command is automatically registered in every SalesRender plugin that uses plugin-core, since ConsoleAppFactory::createBaseApp() adds DirectoryCleanerCommand to the Symfony Console application by default. No additional configuration is required on the plugin developer's side.

Installation

composer require salesrender/plugin-component-directory-cleaner

Note: If your plugin depends on salesrender/plugin-core, this component is already included as a transitive dependency and the command is auto-registered.

Requirements

  • PHP >= 7.1.0
  • symfony/console ^5.0

Key Classes

DirectoryCleanerCommand

Namespace: SalesRender\Plugin\Components\DirectoryCleaner

Extends: Symfony\Component\Console\Command\Command

A Symfony Console command registered under the name cleaner:run.

Command Configuration

Property Value
Name cleaner:run
Description Remove files, older than 24 hours (or another timeout)

Arguments

Argument Mode Description Default
directory REQUIRED Path to directory --
hours OPTIONAL Timeout in hours 24

Behavior

  1. Resolves the directory argument to a real path via realpath().
  2. Creates a RecursiveDirectoryIterator (with SKIP_DOTS flag) wrapped in a RecursiveIteratorIterator using CHILD_FIRST order -- meaning deepest items are processed first, allowing empty subdirectories to be removed after their contents.
  3. Iterates over every item:
    • Excluded files (hardcoded list: .gitignore) are skipped. Output: Skip [exclude]: /path/to/file.
    • Items newer than the threshold are skipped. Output: Skip [by timeout]: /path/to/file.
    • Stale directories are removed with rmdir(). Output: Remove [directory]: /path [Success|Failed].
    • Stale files are removed with unlink(). Output: Remove [file]: /path [Success|Failed].
  4. Returns exit code 0.

Important: The CHILD_FIRST iteration order ensures that a directory's contents are processed before the directory itself. If all files inside a directory are stale and removed, rmdir() can then successfully remove the now-empty directory. However, if the directory contains even one non-stale file (or an excluded file like .gitignore), rmdir() will fail because the directory is not empty.

Usage

Standalone Usage

You can use DirectoryCleanerCommand in any standalone Symfony Console application:

<?php

use Symfony\Component\Console\Application;
use SalesRender\Plugin\Components\DirectoryCleaner\DirectoryCleanerCommand;

require __DIR__ . '/vendor/autoload.php';

$app = new Application();
// Register the directory cleaner command
$app->add(new DirectoryCleanerCommand());
$app->run();

With Plugin Core

When using salesrender/plugin-core (or any of its specialized variants like plugin-core-macros, plugin-core-logistic, plugin-core-chat, plugin-core-pbx, plugin-core-geocoder, plugin-core-integration), the DirectoryCleanerCommand is automatically registered in ConsoleAppFactory::createBaseApp():

// Inside plugin-core ConsoleAppFactory::createBaseApp()
$app = new Application();
$app->add(new DirectoryCleanerCommand());
// ... other commands registered here ...

A typical plugin's console.php looks like this:

#!/usr/bin/env php
<?php

use SalesRender\Plugin\Core\Macros\Factories\ConsoleAppFactory;

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/bootstrap.php';

$factory = new ConsoleAppFactory();
$application = $factory->build();
$application->run();

Since the factory handles registration, you can immediately use cleaner:run without any additional setup.

CLI Usage

Clean all files older than 24 hours (default):

php console.php cleaner:run /path/to/temp/directory

Clean all files older than 6 hours:

php console.php cleaner:run /path/to/temp/directory 6

Clean all files older than 72 hours (3 days):

php console.php cleaner:run /path/to/uploads 72

Example Output

Skip [exclude]: /var/html/app/temp/.gitignore
Remove [file]: /var/html/app/temp/export_20250101.csv [Success]
Remove [file]: /var/html/app/temp/upload_20250102.xlsx [Success]
Skip [by timeout]: /var/html/app/temp/recent_file.txt
Remove [directory]: /var/html/app/temp/old_batch [Success]

Scheduling with Cron

In a SalesRender plugin, you can schedule periodic cleanup using addCronTask() in your custom ConsoleAppFactory:

use SalesRender\Plugin\Core\Factories\ConsoleAppFactory as BaseConsoleAppFactory;
use Symfony\Component\Console\Application;

class MyConsoleAppFactory extends BaseConsoleAppFactory
{
    public function build(): Application
    {
        // Run cleanup every hour for the uploads directory
        $this->addCronTask('0 * * * *', 'cleaner:run /path/to/uploads 24');
        return parent::build();
    }
}

Configuration

This component has no external configuration files. All parameters are passed as command arguments at runtime:

  • directory -- the absolute or relative path to the directory to clean. The path is resolved via realpath().
  • hours -- the age threshold in hours. Files and directories with mtime older than now - hours will be deleted. Defaults to 24.

The exclusion list (currently only .gitignore) is hardcoded in the cleanUp() method and cannot be changed via arguments.

API Reference

DirectoryCleanerCommand

Method Visibility Description
configure(): void protected Sets command name (cleaner:run), description, and arguments (directory, hours)
execute(InputInterface $input, OutputInterface $output): int protected Entry point; reads arguments and delegates to cleanUp(), returns 0
cleanUp(string $directory, int $hoursTimeout, array $exclude, OutputInterface $output): void private Core logic: iterates the directory tree in child-first order, removes stale items, skips excluded files

See Also

salesrender/plugin-component-directory-cleaner 适用场景与选型建议

salesrender/plugin-component-directory-cleaner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 salesrender/plugin-component-directory-cleaner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 salesrender/plugin-component-directory-cleaner 我们能提供哪些服务?
定制开发 / 二次开发

基于 salesrender/plugin-component-directory-cleaner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2023-11-09