cooolinho/symfony-file-importer-bundle
Installation Add the bundle to your Symfony/Laravel (via Symfony Bridge) project:
composer require cooolinho/symfony-file-importer-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel):
return [
// ...
Cooolinho\FileImporterBundle\FileImporterBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --tag=file-importer-config
Update config/file_importer.php with your storage paths (e.g., local, s3).
First Use Case: Import a CSV Create a controller method to handle file uploads and imports:
use Cooolinho\FileImporterBundle\Importer\CsvImporter;
public function import(Request $request)
{
$file = $request->file('csv_file');
$importer = new CsvImporter($file->getPathname());
$data = $importer->import(); // Returns associative array of rows
// Process $data (e.g., save to DB)
}
File Handling
UploadedFile or Laravel’s Illuminate\Http\UploadedFile to pass files to importers.local, s3, etc.) in config/file_importer.php:
'storage' => [
'default' => 'local',
'disks' => [
'local' => storage_path('app/imports'),
's3' => [
'key' => 'your-aws-key',
'secret' => 'your-aws-secret',
'bucket' => 'your-bucket',
],
],
];
Cooolino\FileImporterBundle\Validator\FileValidator to add custom rules (e.g., file size, MIME types).Importer Classes
CsvImporter: For CSV files (supports headers, delimiters).ExcelImporter: For .xlsx/.xls (requires phpoffice/phpspreadsheet).JsonImporter: For JSON files.$importer = new CsvImporter($filePath, [
'delimiter' => ';',
'enclosure' => '"',
'headers' => true,
]);
$data = $importer->import();
Data Processing
$importer->setChunkSize(100); // Process 100 rows at a time
while ($chunk = $importer->getNextChunk()) {
// Save $chunk to DB
}
$data = array_map(function ($row) {
return [
'name' => $row['full_name'],
'email' => strtolower($row['email']),
];
}, $data);
Integration with Laravel
FormRequest for validation:
use Illuminate\Foundation\Http\FormRequest;
class ImportRequest extends FormRequest
{
public function rules()
{
return [
'csv_file' => 'required|file|mimes:csv,txt|max:10240',
];
}
}
use Cooolinho\FileImporterBundle\Jobs\ImportFileJob;
ImportFileJob::dispatch($filePath, CsvImporter::class)->onQueue('imports');
Event Listeners
FileImportStarted, FileImportCompleted) via Symfony’s event dispatcher or Laravel’s event system:
// In a service provider (Symfony)
$dispatcher->addListener(FileImportEvent::IMPORT_STARTED, function (FileImportEvent $event) {
Log::info('Import started for file: ' . $event->getFilePath());
});
Memory Limits
memory_limit.$importer->setChunkSize()) or increase memory_limit in php.ini.Encoding Issues
ISO-8859-1) may corrupt data.$importer = new CsvImporter($filePath, ['encoding' => 'ISO-8859-1']);
Excel Importer Dependencies
ExcelImporter requires phpoffice/phpspreadsheet, which isn’t auto-installed.composer require phpoffice/phpspreadsheet
File Paths in Laravel
/tmp/) may not work in Laravel’s storage system.Storage facade to move files before importing:
use Illuminate\Support\Facades\Storage;
$filePath = Storage::disk('local')->putFile('imports', $request->file('csv_file'));
Case-Sensitive Headers
Name vs. name).$importer = new CsvImporter($filePath, [
'headers' => ['Name' => 'name', 'Email' => 'email'],
]);
Log Importer Output
config/file_importer.php:
'debug' => true,
Validate File Structure
getHeader() or getSample() methods to inspect file structure:
$importer = new CsvImporter($filePath);
$sample = $importer->getSample(5); // First 5 rows
Handle Malformed Data
try {
$data = $importer->import();
} catch (\Cooolino\FileImporterBundle\Exception\ImportException $e) {
Log::error('Import failed: ' . $e->getMessage());
return back()->withError($e->getMessage());
}
Custom Importers
Cooolino\FileImporterBundle\Importer\AbstractImporter to support new file types (e.g., XML):
class XmlImporter extends AbstractImporter
{
public function import()
{
$xml = simplexml_load_file($this->filePath);
// Transform XML to array
return json_decode(json_encode($xml), true);
}
}
Custom Validators
FileValidator to add business logic:
class CustomValidator extends FileValidator
{
public function validate($file)
{
if (!$this->isValidExtension($file, ['csv', 'txt'])) {
return false;
}
// Add custom checks (e.g., file hash)
return true;
}
}
Event Customization
// In your importer service
$dispatcher->dispatch(new FileImportEvent($filePath, 'custom.event'));
Storage Adapters
Cooolino\FileImporterBundle\Storage\StorageInterface.How can I help you explore Laravel packages today?