Skip to main content
LibreDTE Core organizes functionality into packages and components, providing a clear hierarchical structure that promotes modularity and separation of concerns.

Package Registry

The PackageRegistry serves as the central hub for accessing all packages:
PackageRegistry.php
namespace libredte\lib\Core;

use Derafu\Backbone\Contract\PackageRegistryInterface;

class PackageRegistry implements PackageRegistryInterface
{
    public function getBillingPackage(): BillingPackageInterface
    {
        $package = $this->getPackage('billing');
        return $package;
    }
}

Accessing the Registry

use libredte\lib\Core\Application;

$app = Application::getInstance();
$registry = $app->getPackageRegistry();

// Get the billing package
$billing = $registry->getBillingPackage();

The Billing Package

Currently, LibreDTE Core contains one main package focused on its core mission:
LibreDTE’s Mission: Proveer facturación electrónica libre, accesible y bien documentada para Chile.Due to this focused mission, the library centers exclusively on electronic invoicing functionality for Chile.

Package Structure

BillingPackage.php
namespace libredte\lib\Core\Package\Billing;

use Derafu\Backbone\Abstract\AbstractPackage;
use Derafu\Backbone\Attribute\Package;

#[Package(name: 'billing')]
class BillingPackage extends AbstractPackage implements BillingPackageInterface
{
    public function __construct(
        private BookComponentInterface $bookComponent,
        private DocumentComponentInterface $documentComponent,
        private ExchangeComponentInterface $exchangeComponent,
        private IdentifierComponentInterface $identifierComponent,
        private IntegrationComponentInterface $integrationComponent,
        private OwnershipTransferComponentInterface $ownershipTransferComponent,
        private TradingPartiesComponentInterface $tradingPartiesComponent
    ) {}

    public function getComponents(): array
    {
        return [
            'book' => $this->bookComponent,
            'document' => $this->documentComponent,
            'exchange' => $this->exchangeComponent,
            'identifier' => $this->identifierComponent,
            'integration' => $this->integrationComponent,
            'ownership_transfer' => $this->ownershipTransferComponent,
            'trading_parties' => $this->tradingPartiesComponent,
        ];
    }
}

The 7 Components

The Billing package contains seven specialized components:

1. Document Component

billing.document

Handles creation, manipulation, and rendering of tax documents (DTEs).
namespace libredte\lib\Core\Package\Billing\Component\Document;

#[Component(name: 'document', package: 'billing')]
class DocumentComponent extends AbstractComponent
{
    public function __construct(
        private BatchProcessorWorkerInterface $batchProcessorWorker,
        private BuilderWorkerInterface $builderWorker,
        private DispatcherWorkerInterface $dispatcherWorker,
        private DocumentBagManagerWorkerInterface $documentBagManagerWorker,
        private LoaderWorkerInterface $loaderWorker,
        private NormalizerWorkerInterface $normalizerWorker,
        private ParserWorkerInterface $parserWorker,
        private RendererWorkerInterface $rendererWorker,
        private SanitizerWorkerInterface $sanitizerWorker,
        private ValidatorWorkerInterface $validatorWorker,
        private CafLoaderWorkerInterface $cafLoader
    ) {}
}
  • BatchProcessor: Process multiple documents from spreadsheets
  • Builder: Construct DTE XML structure
  • Dispatcher: Coordinate document processing workflow
  • DocumentBagManager: Manage document containers
  • Loader: Load documents from various sources
  • Normalizer: Normalize document data to standard format
  • Parser: Parse input data (JSON, YAML, XML)
  • Renderer: Render documents to PDF or other formats
  • Sanitizer: Clean and validate document data
  • Validator: Validate documents against SII rules

2. Exchange Component

billing.exchange

Manages sending and receiving tax documents via email, SII, and other channels.
ExchangeComponent.php
namespace libredte\lib\Core\Package\Billing\Component\Exchange;

#[Component(name: 'exchange', package: 'billing')]
class ExchangeComponent extends AbstractComponent
{
    public function __construct(
        private ReceiverWorkerInterface $receiverWorker,
        private SenderWorkerInterface $senderWorker
    ) {}

    public function send(ExchangeBagInterface $bag): array
    {
        return $this->senderWorker->send($bag);
    }

    public function receive(ExchangeBagInterface $bag): array
    {
        return $this->receiverWorker->receive($bag);
    }
}

3. Identifier Component

billing.identifier

Manages CAF (Código de Autorización de Folios) for document numbering.
IdentifierComponent.php
namespace libredte\lib\Core\Package\Billing\Component\Identifier;

#[Component(name: 'identifier', package: 'billing')]
class IdentifierComponent extends AbstractComponent
{
    public function __construct(
        private CafFakerWorkerInterface $cafFaker,
        private CafLoaderWorkerInterface $cafLoader,
        private CafProviderWorkerInterface $cafProvider,
        private CafValidatorWorkerInterface $cafValidator
    ) {}
}

4. Integration Component

billing.integration

Handles integration with SII (Servicio de Impuestos Internos) web services.
IntegrationComponent.php
namespace libredte\lib\Core\Package\Billing\Component\Integration;

#[Component(name: 'integration', package: 'billing')]
class IntegrationComponent extends AbstractComponent
{
    public function __construct(
        private SiiLazyWorkerInterface $siiLazyWorker
    ) {}
}
The SiiLazy worker supports:
  • Authenticate
  • CheckXmlDocumentSentStatus
  • ConsumeWebservice
  • RequestXmlDocumentSentStatusByEmail
  • SendXmlDocument
  • ValidateDocument
  • ValidateDocumentSignature

5. Trading Parties Component

billing.trading_parties

Manages commercial parties: issuers (emisor), receivers (receptor), and agents (mandatario).
TradingPartiesComponent.php
namespace libredte\lib\Core\Package\Billing\Component\TradingParties;

#[Component(name: 'trading_parties', package: 'billing')]
class TradingPartiesComponent extends AbstractComponent
{
    public function __construct(
        private MandatarioManagerWorkerInterface $mandatarioManager
    ) {}
}

6. Book Component

billing.book

Manages sales and purchase books (Libros de Ventas y Compras).
BookComponent.php
namespace libredte\lib\Core\Package\Billing\Component\Book;

#[Component(name: 'book', package: 'billing')]
class BookComponent extends AbstractComponent
{
    // Currently in development
}
The Book component is currently under development. Workers will be added in future releases.

7. Ownership Transfer Component

billing.ownership_transfer

Handles ownership transfer of tax documents.
OwnershipTransferComponent.php
namespace libredte\lib\Core\Package\Billing\Component\OwnershipTransfer;

#[Component(name: 'ownership_transfer', package: 'billing')]
class OwnershipTransferComponent extends AbstractComponent
{
    // Currently in development
}
The Ownership Transfer component is currently under development. Workers will be added in future releases.

Component Access Patterns

$billing = $registry->getBillingPackage();
$document = $billing->getDocumentComponent();

Worker Pattern

Each component contains workers that perform specific tasks:
// Get a specific worker
$builder = $document->getBuilderWorker();

// Or get all workers
$workers = $document->getWorkers();
// Returns: ['batch_processor' => ..., 'builder' => ..., ...]
Always access workers through component methods rather than instantiating them directly. This ensures proper dependency injection and configuration.

Next Steps

Architecture

Understand the overall architecture

Dependency Injection

Learn how to use the DI container

Build docs developers (and LLMs) love