tourze / symfony-temp-file-bundle
Symfony临时文件集中管理与自动清理bundle
Installs: 1 737
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-temp-file-bundle
Requires
- php: ^8.1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/service-contracts: ^3.5
- symfony/yaml: ^6.4 || ^7.1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:27:21 UTC
README
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 terminationKernelEvents::EXCEPTION- HTTP request exceptionConsoleEvents::TERMINATE- Console command terminationConsoleEvents::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.