bengor-file/gaufrette-filesystem-bridge-bundle
Install the Bundle
composer require bengor-file/gaufrette-filesystem-bridge-bundle
Ensure FileBundle and GaufretteFilesystemBridge are also installed (this bundle bridges them).
Enable in config/bundles.php
return [
// ...
BenGorFile\GaufretteFilesystemBridgeBundle\BenGorFileGaufretteFilesystemBridgeBundle::class => ['all' => true],
];
Configure in config/packages/bengor_file_gaufrette_filesystem_bridge.yaml
bengor_file_gaufrette_filesystem_bridge:
filesystem: 'my_gaufrette_filesystem' # Matches your Gaufrette config
adapter: 'file' # or 's3', 'rackspace', etc.
First Use Case: File Upload Inject the bridged filesystem into a service/controller:
use BenGorFile\GaufretteFilesystemBridgeBundle\Filesystem\FilesystemBridge;
class UploadController extends AbstractController
{
public function upload(FilesystemBridge $filesystemBridge)
{
$file = $this->get('request')->files->get('file');
$filesystem = $filesystemBridge->getFilesystem('my_gaufrette_filesystem');
$filesystem->write('path/to/file.txt', fopen($file->getPathname(), 'r'));
}
}
Bridge Filesystem Access
Use FilesystemBridge to abstract Gaufrette operations:
$filesystem = $filesystemBridge->getFilesystem('my_gaufrette_filesystem');
$filesystem->write('file.txt', $content); // Write
$content = $filesystem->read('file.txt'); // Read
$filesystem->delete('file.txt'); // Delete
Leverage Gaufrette Adapters
Configure Gaufrette adapters (e.g., local, s3, ftp) in config/packages/gaufrette.yaml:
gaufrette:
filesystems:
my_gaufrette_filesystem:
adapter: gaufrette\adapter\local
options:
directory: '%kernel.project_dir%/var/files'
Integration with FileBundle
Use File entities from FileBundle and map them to Gaufrette:
$fileEntity = new File();
$fileEntity->setPath('path/to/file.txt');
$filesystemBridge->saveFile($fileEntity); // Persists to Gaufrette
Streaming Large Files Use Gaufrette’s streaming capabilities for efficiency:
$filesystem->write('large_file.zip', fopen($file->getPathname(), 'r'), true);
Symfony Events
Hook into file.pre_upload/file.post_upload events to validate/process files before/after Gaufrette operations.
Custom Metadata Handling
Extend File entity to store Gaufrette-specific metadata (e.g., size, mime_type):
$fileEntity->setMetadata([
'gaufrette_size' => $filesystem->size('file.txt'),
'gaufrette_mtime' => $filesystem->mtime('file.txt'),
]);
Dynamic Filesystem Routing Route filesystems dynamically based on request context:
$filesystemName = $request->get('env') === 'prod' ? 's3_prod' : 'local_dev';
$filesystem = $filesystemBridge->getFilesystem($filesystemName);
Flysystem Compatibility
Use league/flysystem adapters via Gaufrette’s FlysystemAdapter for broader storage support.
Deprecated Dependencies
symfony/flex).symfony/flex to install Symfony components and manually bridge Gaufrette with Laravel’s Storage facade.Configuration Overrides
gaufrette.yaml must match the filesystem key in bengor_file_gaufrette_filesystem_bridge.yaml.dump($filesystemBridge->getAvailableFilesystems()).File Permissions
local) require proper filesystem permissions.chmod or ACLs to ensure Laravel’s storage directory is writable:
chmod -R 775 %kernel.project_dir%/var/files
Circular Dependencies
FilesystemBridge into FileBundle services directly. Use Symfony’s container.get() or Laravel’s app() for lazy loading.No Laravel-Specific Features
// app/Providers/GaufretteServiceProvider.php
public function register()
{
$this->app->singleton('gaufrette.filesystem', function ($app) {
return new FilesystemBridge($app['gaufrette.filesystem']);
});
}
Check Filesystem Availability
dd($filesystemBridge->getAvailableFilesystems()); // Verify configured namespaces.
Log Gaufrette Errors Enable Gaufrette’s debug mode:
gaufrette:
debug: true
Validate File Writes
Use filesystem->exists() and filesystem->size() to confirm writes:
if (!$filesystem->exists('file.txt')) {
throw new \RuntimeException('File not written to Gaufrette.');
}
Custom Adapters
Extend Gaufrette’s AdapterInterface and register via gaufrette.yaml:
gaufrette:
adapters:
custom_adapter:
class: App\Adapter\CustomAdapter
Event Listeners
Subscribe to file.pre_upload to validate files before Gaufrette operations:
$event->setAborted(true); // Abort upload if invalid.
Laravel Integration Publish the bundle’s configs and override defaults:
php artisan vendor:publish --provider="BenGorFile\GaufretteFilesystemBridgeBundle\BenGorFileGaufretteFilesystemBridgeBundle"
How can I help you explore Laravel packages today?