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

@@ -3,20 +3,47 @@
namespace App\Http\Controllers\Company\General;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use InvoiceShelf\Modules\Registry;
class ConfigController extends Controller
{
/**
* Handle the incoming request.
*
* @return Response
*/
public function __invoke(Request $request)
public function __invoke(Request $request): JsonResponse
{
if ($request->key === 'exchange_rate_drivers') {
return response()->json([
'exchange_rate_drivers' => $this->exchangeRateDrivers(),
]);
}
return response()->json([
$request->key => config('invoiceshelf.'.$request->key),
]);
}
/**
* Build the exchange rate driver list from the module Registry.
*
* Returns enriched objects (with label, website, and config_fields) so the
* frontend can render driver-specific configuration forms without hardcoding
* any per-driver UI.
*
* @return array<int, array<string, mixed>>
*/
protected function exchangeRateDrivers(): array
{
return collect(Registry::allDrivers('exchange_rate'))
->map(fn (array $meta, string $name) => [
'value' => $name,
'label' => $meta['label'] ?? $name,
'website' => $meta['website'] ?? '',
'config_fields' => $meta['config_fields'] ?? [],
])
->values()
->all();
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Providers;
use App\Services\ExchangeRate\CurrencyConverterDriver;
use App\Services\ExchangeRate\CurrencyFreakDriver;
use App\Services\ExchangeRate\CurrencyLayerDriver;
use App\Services\ExchangeRate\OpenExchangeRateDriver;
use Illuminate\Support\ServiceProvider;
use InvoiceShelf\Modules\Registry;
class DriverRegistryProvider extends ServiceProvider
{
public function boot(): void
{
$this->registerExchangeRateDrivers();
}
protected function registerExchangeRateDrivers(): void
{
Registry::registerExchangeRateDriver('currency_converter', [
'class' => CurrencyConverterDriver::class,
'label' => 'settings.exchange_rate.currency_converter',
'website' => 'https://www.currencyconverterapi.com',
'config_fields' => [
[
'key' => 'type',
'type' => 'select',
'label' => 'settings.exchange_rate.server',
'options' => [
['label' => 'settings.preferences.premium', 'value' => 'PREMIUM'],
['label' => 'settings.preferences.prepaid', 'value' => 'PREPAID'],
['label' => 'settings.preferences.free', 'value' => 'FREE'],
['label' => 'settings.preferences.dedicated', 'value' => 'DEDICATED'],
],
'default' => 'FREE',
],
[
'key' => 'url',
'type' => 'text',
'label' => 'settings.exchange_rate.url',
'visible_when' => ['type' => 'DEDICATED'],
],
],
]);
Registry::registerExchangeRateDriver('currency_freak', [
'class' => CurrencyFreakDriver::class,
'label' => 'settings.exchange_rate.currency_freak',
'website' => 'https://currencyfreaks.com',
]);
Registry::registerExchangeRateDriver('currency_layer', [
'class' => CurrencyLayerDriver::class,
'label' => 'settings.exchange_rate.currency_layer',
'website' => 'https://currencylayer.com',
]);
Registry::registerExchangeRateDriver('open_exchange_rate', [
'class' => OpenExchangeRateDriver::class,
'label' => 'settings.exchange_rate.open_exchange_rate',
'website' => 'https://openexchangerates.org',
]);
}
}

View File

@@ -3,10 +3,20 @@
namespace App\Services\ExchangeRate;
use InvalidArgumentException;
use InvoiceShelf\Modules\Registry;
class ExchangeRateDriverFactory
{
/** @var array<string, class-string<ExchangeRateDriver>> */
/**
* Built-in driver fallback map.
*
* Kept as a backstop so that direct calls to register() (without going through
* the module Registry) continue to work. Built-in drivers are also registered
* via the Registry by DriverRegistryProvider — that registration is the
* canonical source for driver metadata (label, website, config_fields).
*
* @var array<string, class-string<ExchangeRateDriver>>
*/
protected static array $drivers = [
'currency_freak' => CurrencyFreakDriver::class,
'currency_layer' => CurrencyLayerDriver::class,
@@ -15,7 +25,11 @@ class ExchangeRateDriverFactory
];
/**
* Register a custom exchange rate driver (for module extensibility).
* Register a custom exchange rate driver directly with the factory.
*
* Modules should prefer Registry::registerExchangeRateDriver() instead, which
* carries metadata (label, website, config_fields) the frontend needs to render
* a configuration form for the driver.
*/
public static function register(string $name, string $driverClass): void
{
@@ -24,7 +38,7 @@ class ExchangeRateDriverFactory
public static function make(string $driver, string $apiKey, array $config = []): ExchangeRateDriver
{
$class = static::$drivers[$driver] ?? null;
$class = static::resolveDriverClass($driver);
if (! $class) {
throw new InvalidArgumentException("Unknown exchange rate driver: {$driver}");
@@ -34,10 +48,32 @@ class ExchangeRateDriverFactory
}
/**
* Get all registered driver names.
* Get all known driver names — both built-in/factory-registered and Registry-registered.
*
* @return array<int, string>
*/
public static function availableDrivers(): array
{
return array_keys(static::$drivers);
$local = array_keys(static::$drivers);
$registry = array_keys(Registry::allDrivers('exchange_rate'));
return array_values(array_unique(array_merge($local, $registry)));
}
/**
* Resolve a driver name to its concrete class.
*
* Checks the local $drivers map first (built-ins and factory::register() calls),
* then falls back to the module Registry.
*/
protected static function resolveDriverClass(string $driver): ?string
{
if (isset(static::$drivers[$driver])) {
return static::$drivers[$driver];
}
$meta = Registry::driverMeta('exchange_rate', $driver);
return $meta['class'] ?? null;
}
}