How phpcfdi/sat-catalogos-populate works
1. What this repository is
This is a PHP 8.2+ command-line tool that builds and refreshes a SQLite3 catalog database for SAT/CFDI reference data. Its architecture is a pipeline: discover source origins → fetch/normalize spreadsheet files → convert to CSV → import into SQLite.
2. CLI entrypoint and orchestration
The main executable is bin/sat-catalogos-update, which boots src/Commands/CliApplication.php. The command layer is small and centralized, with command classes such as DumpOrigins, UpdateOrigins, and UpdateDatabase, plus a terminal logger. This is the top-level boundary for all user-facing behavior.
3. Origin management subsystem
The src/Origins namespace manages where catalog source files come from and how they are described. It includes:
- origin types for different source styles (ConstantOrigin, ScrapingOrigin, HidroPetro10Origin, Nomina12eOrigin)
- translators that turn origin definitions into runtime objects
- reviewers that validate or extract information from source pages/files
- OriginsIO, Upgrader, and Reviews for reading, updating, and comparing origin metadata
This subsystem is about metadata and discovery, not parsing catalog rows.
4. Source acquisition boundary
WebResourcesGateway and ResourcesGatewayInterface form the I/O boundary for fetching remote resources. The repo expects SAT-originated spreadsheets or pages to be retrieved from the web, then processed locally. This is the point where the tool crosses from internal processing into external network access.
5. Conversion layer: spreadsheet to CSV
The src/Converters namespace normalizes Excel inputs into a filesystem of CSV files. It includes:
- XlsToXlsxConverter
- XlsToCsvFolderConverter
- XlsxToCsvFolderConverter
- CsvFolderJoinFiles
This layer exists because the input formats vary and some catalogs are split across multiple sheets/files. The README explicitly depends on external tools like LibreOffice Calc and xlsx2csv, so this boundary is operationally important.
6. Importer family organization
The src/Importers tree is organized by SAT catalog family and version: Cfdi, Cfdi40, Cce, Cce20, Ccp20, Ccp30, Ccp31, Nomina, Ret20, Rep, HidroPretro10, plus catalog aggregator classes like CfdiCatalogs.php and Ccp31Catalogs.php.
Each catalog family encapsulates the knowledge of which CSV files belong to that version and how they map into tables.
7. Injector pattern for per-catalog mapping
Under each importer family, the Injectors subdirectory contains one class per catalog file/table. These injector classes are the catalog-specific adapters that connect a CSV source to a database table definition. They hide the per-catalog schema details from the generic import machinery.
8. Generic import base classes
Shared importer abstractions such as AbstractExcelImporter, AbstractXlsImporter, AbstractXlsxImporter, and AbstractXlsOneSheetImporter provide the common mechanics for reading spreadsheet-derived data. SourcesImporter acts as a higher-level coordinator over catalog sources. This keeps the per-catalog classes thin and focused on mapping rather than file handling.
9. Database model and field transformation layer
The src/Database namespace is the SQLite mapping layer. It includes:
- DataTable and DataTableGateway for table-level persistence
- Repository for access patterns
- DataFields plus field types like TextDataField, IntegerDataField, DateDataField, BoolDataField, FloatDataField
- transformation helpers like PaddingDataField, UsePreviousIfEmptyDataField, PreprocessDataField, NumberFormatDataField, ToBeDefinedDataField
Architecturally, this is where raw CSV values become normalized SQLite rows.
10. Row/column preprocessing utilities
AbstractCsvInjector, AbstractCollection, and the src/Utils/ArrayProcessors classes provide reusable row-cleaning behavior such as trimming, ignoring columns, and handling nulls. These are small but important because the SAT catalog files are not perfectly uniform across versions.
11. Data flow from input to output
The end-to-end path is:
- a command reads origin definitions
- origins resolve remote or local source files
- converters normalize spreadsheet input into CSV folders
- importer/injector pairs map CSV files to catalog tables
- the database layer writes the final SQLite3 file
In other words, origins describe what to fetch, converters reshape the files, importers define catalog semantics, and the database layer persists the result.
12. Testing and fixtures mirror the architecture
The tests/_files directory contains representative spreadsheets, split CSV outputs, and origin XML fixtures for each catalog family/version. The test suite is split into unit and feature layers, which closely reflects the production boundaries: conversion, importing, origin handling, and database persistence. This makes the repo’s architecture very explicit and test-driven.
Main entry points: Commands Commands · Converters XLS/XLSX · SourcesImporter.php Sources importer · tests Tests
Components 21
CLI
User-facing commands and application bootstrap
- sat-catalogos-update · Main executable for the update workflow
bin/sat-catalogos-update - CLI app · Bootstraps and runs commands
src/Commands/CliApplication.php - Commands · Central command set for origin dumps and database updates
src/Commands - Logger · Console logging for command execution
src/Commands/TerminalLogger.php
Origins
Origin metadata, review, and remote resource access
- Origins IO · Reads and writes origin definitions
src/Origins/OriginsIO.php - Translators · Converts origin definitions into runtime origin objects
src/Origins/OriginsTranslators - Reviewers · Inspects or validates origin sources
src/Origins/Reviewers - Web gateway · Fetches remote resources from SAT-originated sources
src/Origins/WebResourcesGateway.php - Origin types · Constant, scraping, HidroPetro10, and Nomina12e origin definitions
src/Origins/Types
Conversion
Spreadsheet normalization into CSV folders
- XLS/XLSX · Converts spreadsheet inputs to normalized CSV folders
src/Converters - CSV joiner · Combines split CSV files when needed
src/Converters/CsvFolderJoinFiles.php
Importing
Catalog family importers and CSV injectors
- Sources importer · Coordinates source-level catalog imports
src/Importers/SourcesImporter.php - Catalog families · Versioned import families such as CFDI, Carta Porte, Nómina, RET and Hidro/Petro
src/Importers - Injectors · Per-catalog adapters that map CSV files to tables
src/Importers
Database
SQLite table mapping and value normalization
- Data fields · Typed and transformed values for SQLite rows
src/Database - Data table · Table-level storage and gateway layer
src/Database/DataTable.php - Repository · Database access patterns over catalog data
src/Database/Repository.php
Utils
Shared low-level helpers
- Array processors · Trims, ignores, and null-handles CSV rows
src/Utils/ArrayProcessors - CSV file · CSV reading and writing helper
src/Utils/CsvFile.php - Shell exec · External command execution support
src/Utils/ShellExec.php
Tests
Feature and unit coverage mirroring the architecture
- Tests · Coverage for origins, converters, importers, and database behavior
tests
Connections 26
- sat-catalogos-update → CLI app: boots · The executable starts the CLI application
- CLI app → Commands: dispatches · Application dispatches the available commands
- Commands → Origins IO: reads origins · Commands load origin definitions and updates
- Commands → Web gateway: fetches resources · Commands cross the network boundary through the resource gateway
- Commands → Sources importer: runs import · Update commands orchestrate the import pipeline
- Commands → Logger: logs · Commands emit progress and diagnostics
- Origins IO → Translators: translates · Stored origin metadata becomes runtime origin objects
- Translators → Origin types: instantiates · Translators produce concrete origin types
- Origin types → Reviewers: reviewed by · Origin types are validated or inspected by reviewers
- Reviewers → Web gateway: inspects remote · Reviewers may parse remote pages or resource links
- Web gateway → XLS/XLSX: downloads inputs · Remote spreadsheet sources are handed to conversion
- XLS/XLSX → CSV joiner: normalizes · Converted outputs can be merged into unified CSV folders
- XLS/XLSX → Sources importer: produces csv · Normalized CSV folders feed the import pipeline
- Sources importer → Catalog families: selects family · Import coordinator chooses the catalog family implementation
- Catalog families → Injectors: uses · Family-level importers delegate table/file mapping to injectors
- Injectors → Array processors: cleans rows · Injectors reuse row cleanup processors during CSV handling
- Injectors → Data fields: maps values · Injectors translate CSV columns into typed database fields
- Data fields → Data table: persists · Normalized field values are written to SQLite tables
- Data table → Repository: served by · Repository provides higher-level database access
- Sources importer → Repository: writes through · Import execution ultimately persists into the database repository
- CSV file → Array processors: supports · CSV helpers feed row-processing utilities
- Shell exec → XLS/XLSX: invokes tools · External conversion tools are used by spreadsheet conversion
- Tests → Origins IO: covers · Tests mirror origin metadata handling
- Tests → XLS/XLSX: covers · Tests mirror spreadsheet conversion
- Tests → Sources importer: covers · Tests exercise end-to-end importing
- Tests → Data table: covers · Tests exercise database persistence behavior