bengor-file/symfony-filesystem-bridge-bundle
Installation:
composer require bengor-file/symfony-filesystem-bridge-bundle
Add the bundle to config/bundles.php:
return [
// ...
BenGorFile\SymfonyFilesystemBridgeBundle\BenGorFileSymfonyFilesystemBridgeBundle::class => ['all' => true],
];
First Use Case:
Inject the bridge service into a controller/service to leverage Symfony's Filesystem component with FileBundle's abstractions:
use BenGorFile\SymfonyFilesystemBridgeBundle\Service\FilesystemBridge;
class MyController extends AbstractController
{
public function __construct(private FilesystemBridge $filesystemBridge) {}
public function uploadFile()
{
$filesystem = $this->filesystemBridge->getFilesystem();
$filesystem->mkdir('/path/to/dir'); // Uses Symfony's Filesystem
}
}
Key Entry Point:
The FilesystemBridge service bridges FileBundle's FileManager with Symfony's Filesystem component. Check services.yaml for autowiring configurations.
File Operations:
Use the bridge to abstract filesystem operations (e.g., mkdir, copy, remove) while maintaining FileBundle's file management logic:
$filesystem = $this->filesystemBridge->getFilesystem();
$filesystem->dumpFile('/path/to/file.txt', 'Content');
Integration with FileBundle:
Leverage FileBundle's FileManager for metadata/validation, then delegate filesystem tasks to Symfony's Filesystem:
$fileManager = $this->filesystemBridge->getFileManager();
$file = $fileManager->find('/path/to/file'); // FileBundle logic
$filesystem = $this->filesystemBridge->getFilesystem();
$filesystem->chmod($file->getPath(), 0755); // Symfony Filesystem
Dependency Injection:
Prefer constructor injection for the FilesystemBridge service to ensure testability:
class FileService {
public function __construct(private FilesystemBridge $bridge) {}
}
Configuration-Driven Paths:
Use Symfony's parameter bag (%kernel.project_dir%) or FileBundle's config to resolve paths dynamically:
# config/packages/bengor_file.yaml
bengor_file:
filesystem:
root_dir: '%kernel.project_dir%/uploads'
Event Listeners:
Attach listeners to FileBundle's events (e.g., FileUploadEvent) and use the bridge to handle filesystem operations:
use BenGorFile\SymfonyFilesystemBridgeBundle\Event\FileUploadEvent;
class FileOptimizerListener {
public function __construct(private FilesystemBridge $bridge) {}
public function onUpload(FileUploadEvent $event) {
$filesystem = $this->bridge->getFilesystem();
$filesystem->chmod($event->getFile()->getPath(), 0644);
}
}
Custom Filesystem Adapters:
Extend the bridge to support cloud storage (e.g., S3) by implementing Symfony's Filesystem interface:
class S3FilesystemBridge extends FilesystemBridge {
public function getFilesystem() {
return new S3Filesystem(); // Custom implementation
}
}
Deprecated Dependencies:
FileBundle (v1.x) may have outdated dependencies. Audit for compatibility if using newer Symfony/Laravel versions.Missing Documentation:
FileBundle integration. Refer to FileBundle's docs for file management logic.php bin/console debug:container BenGorFile\SymfonyFilesystemBridgeBundle to inspect available services.Stateful Filesystem:
Symfony's Filesystem is not thread-safe. Avoid sharing instances across requests or long-running processes.
Path Resolution:
FileBundle's root directory. Hardcoded paths (e.g., /tmp) may fail.FileBundle's PathResolver or Symfony's %kernel.project_dir% for consistency.Filesystem Operations: Enable Symfony's debug toolbar to log filesystem operations:
# config/packages/dev/debug.yaml
framework:
profiler: { only_exceptions: false }
Permission Issues:
Filesystem throws RuntimeException on failures. Wrap operations in try-catch:try {
$filesystem->mkdir($path);
} catch (\RuntimeException $e) {
$this->addFlash('error', 'Failed to create directory: '.$e->getMessage());
}
Custom Filesystem:
Override the bridge's getFilesystem() method to inject a custom adapter (e.g., for testing):
class TestFilesystemBridge extends FilesystemBridge {
public function getFilesystem() {
return new InMemoryFilesystem(); // Use a mock for tests
}
}
Event Subscribers:
Extend FileBundle's events to trigger filesystem actions:
use BenGorFile\FileBundle\Event\FileDeleteEvent;
class CleanupSubscriber implements EventSubscriberInterface {
public function onDelete(FileDeleteEvent $event) {
$this->bridge->getFilesystem()->remove($event->getFile()->getPath());
}
}
Configuration Overrides:
Customize the bridge's behavior via config/packages/bengor_file.yaml:
bengor_file:
filesystem_bridge:
adapter: 'local' # or 's3', 'ftp', etc.
options:
base_path: '%kernel.project_dir%/custom_path'
Laravel Integration:
Use the bridge in Laravel via Symfony's HttpKernel or a custom facade:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton('filesystem.bridge', function ($app) {
return new FilesystemBridge(
$app->make('file.manager'),
new SymfonyFilesystem() // Laravel's Filesystem
);
});
}
Performance:
Batch filesystem operations (e.g., mkdir + chmod) to reduce I/O calls:
$filesystem = $this->bridge->getFilesystem();
$filesystem->mkdir($path);
$filesystem->chmod($path, 0755);
Testing:
Mock the FilesystemBridge in PHPUnit:
$mockFilesystem = $this->createMock(\Symfony\Component\Filesystem\Filesystem::class);
$mockFilesystem->method('mkdir')->willReturn(true);
$bridge = $this->createPartialMock(FilesystemBridge::class, ['getFilesystem']);
$bridge->method('getFilesystem')->willReturn($mockFilesystem);
How can I help you explore Laravel packages today?