tourze/symfony-temp-file-bundle 问题修复 & 功能扩展

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

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

tourze/symfony-temp-file-bundle

最新稳定版本:1.1.0

Composer 安装命令:

composer require tourze/symfony-temp-file-bundle

包简介

Symfony临时文件集中管理与自动清理bundle

README 文档

README

English | 中文

Latest Stable Version License PHP Version Symfony Version Code Coverage

Introduction

symfony-temp-file-bundle is a Symfony bundle for centralized management and automatic cleanup of temporary files. It ensures that temporary files generated during HTTP requests or console commands are automatically deleted at the end of the process, reducing the risk of leftover temp files.

Features

  • Unified temporary file management: Centralized creation and deletion of temporary files
  • Automatic cleanup: Files are automatically deleted at the end of HTTP requests and console commands
  • Multiple event listeners: Supports KernelEvents::TERMINATE, KernelEvents::EXCEPTION, ConsoleEvents::TERMINATE, ConsoleEvents::ERROR
  • Simple API: Easy-to-use methods for generating and managing temporary files
  • Zero configuration: Works out of the box with minimal setup
  • Memory efficient: Uses array shifting to manage file cleanup

Installation

Requirements

  • PHP >= 8.1
  • Symfony >= 6.4

Composer

composer require tourze/symfony-temp-file-bundle

Manual Registration (Symfony Flex will register automatically in most cases)

Add to config/bundles.php:

Tourze\TempFileBundle\TempFileBundle::class => ['all' => true],

Quick Start

Generate a Temporary File

use Tourze\TempFileBundle\Service\TemporaryFileService;

// Inject TemporaryFileService
$tempFile = $temporaryFileService->generateTemporaryFileName('prefix_', 'txt');
file_put_contents($tempFile, 'hello world');
// The temp file will be automatically deleted at the end of the request or command

Register an Existing Temp File for Auto-Cleanup

// Register an existing file for automatic cleanup
$temporaryFileService->addTemporaryFile($filePath);

Generate Temporary File Without Extension

// Generate temporary file without extension
$tempFile = $temporaryFileService->generateTemporaryFileName('data_');
// Will create a file like: /tmp/data_abc123

API Documentation

TemporaryFileService

Methods

  • generateTemporaryFileName(string $prefix, ?string $ext = null): string

    • Generate a temporary file name with optional extension
    • The file is automatically registered for cleanup
    • Returns the full path to the temporary file
  • addTemporaryFile(string $file): void

    • Register an existing file for automatic cleanup
    • Useful for files created outside the service
  • reset(): void

    • Manually clear the temporary file list
    • Usually called automatically by event listeners

Event Listeners

The service automatically cleans up temporary files on these events:

  • KernelEvents::TERMINATE - Normal HTTP request termination
  • KernelEvents::EXCEPTION - HTTP request exception
  • ConsoleEvents::TERMINATE - Console command termination
  • ConsoleEvents::ERROR - Console command error

Configuration

No configuration is required. The service is automatically registered and configured when the bundle is installed.

Contributing

  • Issues and PRs are welcome
  • Follow PSR-12 coding style
  • Please ensure all PHPUnit tests pass before submitting

License

MIT License © tourze

Changelog

See [CHANGELOG.md] or Git commit history for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-23