mikamatto/file-cleanup-bundle
最新稳定版本:v1.0.0
Composer 安装命令:
composer require mikamatto/file-cleanup-bundle
包简介
Automatically cleans up files when their associated Doctrine entities are deleted
README 文档
README
Provides an infrastructure to streamline automatic file cleanup when Doctrine entities are deleted. The bundle abstracts the file management logic through a registry pattern, allowing applications to implement type-specific file managers while maintaining a clean separation of concerns.
Installation
composer require mikamatto/file-cleanup-bundle
Usage
1. Implement FileBoundInterface on your entity
use Mikamatto\FileCleanupBundle\Contracts\FileBoundInterface; class Video implements FileBoundInterface { public function getHandledFileTypes(): array { return ['image']; // Types of files this entity handles } public function getBoundFilesByType(string $type): array { if ($type !== 'image') { return []; } $files = []; if ($this->preview) { $files[] = [ 'path' => $this->preview, 'type' => 'preview' ]; } if ($this->thumbnail) { $files[] = [ 'path' => $this->thumbnail, 'type' => 'thumb' ]; } return $files; } public function getFileOwnerId(): int { return $this->id; } }
2. Create a FileManager service
use Mikamatto\FileCleanupBundle\Contracts\FileManagerInterface; use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; #[AutoconfigureTag('mikamatto_file_cleanup.manager')] class ImageManager implements FileManagerInterface { public function getHandledType(): string { return 'image'; } public function deleteFiles(array $files, int $ownerId): void { foreach ($files as $file) { // Each file has 'path' and 'type' $this->deleteFile($file['path'], $ownerId, $file['type']); } } }
How it works
- When an entity implementing
FileBoundInterfaceis deleted:- The bundle's event subscriber is triggered
- It gets the file types from
getHandledFileTypes() - For each type, it finds the appropriate manager via
getHandledType() - Gets the files to delete via
getBoundFilesByType() - Passes the files to the manager's
deleteFiles()method
The format of the files array returned by getBoundFilesByType() should match what your FileManager implementation expects. The bundle itself doesn't impose any specific structure. For example:
return [ 'preview' => [ 'path' => 'path/to/preview.jpg', 'type' => 'preview' ], 'thumb' => [ 'path' => 'path/to/thumb.jpg', 'type' => 'thumb' ] ];
Requirements
- PHP 8.2 or higher
- Symfony 6.4 or higher
License
MIT
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-05-06