feat(exchange-rate): make providers extendible via module Registry

Exchange rate providers are now pluggable via the module Registry. The four built-in drivers (currency_converter, currency_freak, currency_layer, open_exchange_rate) move from a static config array into App\\Providers\\DriverRegistryProvider, which calls Registry::registerExchangeRateDriver() for each during app boot with metadata the frontend needs: label (i18n key), website (help-text URL), and config_fields (schema for driver-specific driver_config JSON).

The Currency Converter's server-type selector and dedicated URL field — previously hardcoded in ExchangeRateProviderModal.vue — are now just another config_fields entry with a visible_when rule that shows the URL input only when type=DEDICATED. Any module that wants to ship a custom driver gets the same treatment for free: declare config_fields in the registration, and the host app's modal renders them automatically.

ExchangeRateDriverFactory::make() falls back to Registry::driverMeta() when a name isn't in the local built-in map, and availableDrivers() merges both sources. ConfigController handles the exchange_rate_drivers key specially by mapping Registry::allDrivers('exchange_rate') to enriched option objects, so the config-file route still works for every other key. The static exchange_rate_drivers + currency_converter_servers arrays in config/invoiceshelf.php are deleted.

Unit tests cover the new Registry::register/flushDrivers, the factory merging built-ins with Registry-contributed drivers, and the factory rejecting unknown names. A feature test exercises the end-to-end /api/v1/config?key=exchange_rate_drivers response shape.

NOTE: this commit depends on invoiceshelf/modules package commit e44d951 which adds the Registry driver API. The package needs to be released and pinned in composer.json before a fresh composer install on this commit will work.
This commit is contained in:
Darko Gjorgjijoski
2026-04-11 04:00:00 +02:00
parent 112cc56922
commit e44657bf7e
10 changed files with 511 additions and 167 deletions

View File

@@ -0,0 +1,68 @@
<?php
use InvoiceShelf\Modules\Registry;
afterEach(function () {
Registry::flushDrivers();
});
test('registerDriver stores driver metadata under its type', function () {
Registry::flushDrivers();
Registry::registerDriver('exchange_rate', 'fake_provider', [
'class' => 'FakeDriver',
'label' => 'fake.label',
]);
expect(Registry::driverMeta('exchange_rate', 'fake_provider'))
->toEqual([
'class' => 'FakeDriver',
'label' => 'fake.label',
]);
});
test('registerExchangeRateDriver delegates to registerDriver with the exchange_rate type', function () {
Registry::flushDrivers();
Registry::registerExchangeRateDriver('fake_provider', [
'class' => 'FakeDriver',
'label' => 'fake.label',
]);
expect(Registry::driverMeta('exchange_rate', 'fake_provider'))
->not->toBeNull()
->and(Registry::allDrivers('exchange_rate'))
->toHaveKey('fake_provider');
});
test('allDrivers returns an empty array for unknown types', function () {
Registry::flushDrivers();
expect(Registry::allDrivers('nonexistent'))->toBe([]);
});
test('driverMeta returns null for unknown drivers', function () {
Registry::flushDrivers();
expect(Registry::driverMeta('exchange_rate', 'nonexistent'))->toBeNull();
});
test('flushDrivers clears all driver registrations', function () {
Registry::registerExchangeRateDriver('a', ['class' => 'A', 'label' => 'a']);
Registry::registerExchangeRateDriver('b', ['class' => 'B', 'label' => 'b']);
Registry::flushDrivers();
expect(Registry::allDrivers('exchange_rate'))->toBe([]);
});
test('flush does not clear driver registrations', function () {
Registry::registerExchangeRateDriver('persists', [
'class' => 'PersistDriver',
'label' => 'persist.label',
]);
Registry::flush();
expect(Registry::driverMeta('exchange_rate', 'persists'))->not->toBeNull();
});