avtonom/mathielen-import-engine-bundle
Installation
Add the bundle to your composer.json:
composer require avtonom/mathielen-import-engine-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Avtonom\MathielenImportEngineBundle\AvtonomMathielenImportEngineBundle::class => ['all' => true],
];
First Use Case: Basic CSV Import
Define a simple import class (e.g., src/Import/UserImport.php):
namespace App\Import;
use Avtonom\MathielenImportEngineBundle\Import\ImportInterface;
use Avtonom\MathielenImportEngineBundle\Import\ImportResult;
use App\Entity\User;
class UserImport implements ImportInterface
{
public function import(array $data): ImportResult
{
$user = new User();
$user->setEmail($data['email']);
$user->setName($data['name']);
// Save logic (e.g., EntityManager) here
return new ImportResult(true, 'User imported successfully');
}
}
Run the Import Use the Symfony console command:
php bin/console avtonom:import:run UserImport /path/to/file.csv
Key Files to Explore
src/Resources/config/services.xml: Bundle configuration.src/Import/ImportInterface.php: Core interface for imports.src/Command/ImportCommand.php: CLI entry point.Structured Import Pipeline
ImportInterface to map CSV rows to entities/arrays.validate() in your import class to reject malformed data early.$this->setBatchSize(100); // In your import class constructor
Export Workflow
Avtonom\MathielenImportEngineBundle\Export\ExportInterface for exports.namespace App\Export;
use Avtonom\MathielenImportEngineBundle\Export\ExportInterface;
use App\Entity\UserRepository;
class UserExport implements ExportInterface
{
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function export(): array
{
return $this->userRepository->findAll();
}
public function getHeaders(): array
{
return ['id', 'email', 'name'];
}
}
php bin/console avtonom:export:run UserExport /path/to/output.csv
Dependency Injection
EntityManager, Validator) into your import/export classes.config/services.yaml:
services:
App\Import\UserImport:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
Event-Driven Extensions
import.start, import.end, or row.processed events via Symfony’s event dispatcher:
$dispatcher->addListener('import.start', function () {
// Pre-import logic (e.g., log start time)
});
EntityManager to persist entities during import.Validator to validate rows before processing:
use Symfony\Component\Validator\Validator\ValidatorInterface;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function import(array $data): ImportResult
{
$errors = $this->validator->validate($data);
if (count($errors) > 0) {
return new ImportResult(false, (string) $errors);
}
// Proceed with import
}
use Psr\Log\LoggerInterface;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
PHP Version Compatibility
Symfony Version Mismatch
config/packages/avtonom_mathielen_import.yaml:
imports:
resources:
- "%kernel.project_dir%/vendor/avtonom/mathielen-import-engine-bundle/src/Resources/config/services.xml"
parameters:
avtonom_mathielen_import.engine.class: App\Service\CustomImportEngine
Memory Limits
$this->setChunkSize(500); // Process 500 rows at a time
Namespace Collisions
ImportInterface may conflict with Laravel’s Illuminate\Support\Collection or other Import classes.use Avtonom\MathielenImportEngineBundle\Import\ImportInterface as AvtonomImportInterface;
Missing Error Handling
ImportResult or wrap imports in try-catch blocks:
try {
$result = $this->import($data);
} catch (\Exception $e) {
return new ImportResult(false, $e->getMessage());
}
Enable Verbose Output
Run commands with -v or -vv for debugging:
php bin/console avtonom:import:run UserImport file.csv -vv
Check Event Listeners
Ensure your event listeners are registered in config/services.yaml:
services:
App\EventListener\ImportListener:
tags:
- { name: kernel.event_listener, event: import.start, method: onImportStart }
Validate CSV Format Use tools like CSVLint to pre-validate files before import.
Custom File Parsers
Extend Avtonom\MathielenImportEngineBundle\Parser\ParserInterface to support formats like JSON or XML:
class JsonParser implements ParserInterface
{
public function parse(string $filePath): array
{
return json_decode(file_get_contents($filePath), true);
}
}
Register the parser in services.yaml:
services:
App\Parser\JsonParser:
tags:
- { name: avtonom_mathielen_import.parser }
Database Transactions Wrap imports in transactions to ensure data consistency:
use Doctrine\DBAL\Connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function import(array $data): ImportResult
{
$this->connection->beginTransaction();
try {
// Import logic
$this->connection->commit();
return new ImportResult(true, 'Import successful');
} catch (\Exception $e) {
$this->connection->rollBack();
return new ImportResult(false, $e->getMessage());
}
}
Progress Tracking Add progress callbacks to monitor long-running imports:
public function import(array $data): ImportResult
{
$this->dispatcher->dispatch('row.processed', new RowProcessedEvent($data));
// ...
}
Listen for events in a controller or command:
$dispatcher->addListener('row.processed', function (RowProcessedEvent $event) {
echo "Processed row: " . $event->getRow() . "\n";
});
Custom Result Formats
Extend ImportResult to include additional metadata (e.g., row counts, stats):
class ExtendedImportResult extends ImportResult
{
private $rowCount;
private $errorsByRow;
public function __construct(bool $success, string $message, int $rowCount, array $errorsByRow)
{
parent::__construct($success, $message);
$this->rowCount = $
How can I help you explore Laravel packages today?