Files
InvoiceShelf/app/Platform/Ai/Drivers/AiDriverFactory.php
T
Darko Gjorgjijoski 5ef7804e60 refactor: adopt modular domain architecture (#747)
* refactor: stabilize model identities for domain migration

* refactor: extract module platform context

* refactor: assign models to domain contexts

* refactor: extract ai platform context

* refactor: extract storage platform context

* refactor: extract mail platform context

* refactor: extract pdf platform context

* refactor: extract operations platform context

* refactor: move installation into operations platform

* refactor: extract money domain context

* refactor: extract taxation domain context

* refactor: extract catalog domain context

* refactor: extract metadata domain context

* refactor: extract reporting domain context

* refactor: extract purchases domain context

* refactor: extract receivables domain context

* refactor: extract accounts domain context

* refactor: complete reporting statement boundary

* refactor: extract contacts domain context

* refactor: extract sales domain context

* refactor: remove legacy application layers

* fix: migrate legacy bouncer role identities
2026-08-05 17:40:03 +02:00

88 lines
2.7 KiB
PHP

<?php
namespace App\Platform\Ai\Drivers;
use App\Platform\Ai\Contracts\AiDriver;
use InvalidArgumentException;
use InvoiceShelf\Modules\Registry;
/**
* Instantiates AiDriver implementations by name.
*
* Mirrors the shape of ExchangeRateDriverFactory: a static $drivers fallback
* map for built-ins registered directly against the factory, plus a fallback
* to the module Registry so module-contributed drivers (via
* Registry::registerAiDriver()) are also resolvable. Canonical registration
* path is the Registry — the local fallback map exists so the factory keeps
* working even in tests or contexts where the Registry happens to be flushed.
*/
class AiDriverFactory
{
/**
* @var array<string, class-string<AiDriver>>
*/
protected static array $drivers = [
'openrouter' => OpenRouterDriver::class,
];
/**
* Register a custom AI driver directly with the factory.
*
* Modules should prefer Registry::registerAiDriver() which carries
* the metadata (label, website, supported_roles, suggested_models,
* config_fields) that the frontend UI needs to render a configuration
* form. This method exists for tests and programmatic registration.
*
* @param class-string<AiDriver> $driverClass
*/
public static function register(string $name, string $driverClass): void
{
static::$drivers[$name] = $driverClass;
}
/**
* Instantiate a driver by name.
*
* @param array<string, mixed> $config Driver-specific config (base_url, timeouts, etc.)
*
* @throws InvalidArgumentException When the driver name isn't known.
*/
public static function make(string $driver, string $apiKey, array $config = []): AiDriver
{
$class = static::resolveDriverClass($driver);
if (! $class) {
throw new InvalidArgumentException("Unknown AI driver: {$driver}");
}
return new $class($apiKey, $config);
}
/**
* Get all known driver names — both factory-registered built-ins and Registry-contributed.
*
* @return array<int, string>
*/
public static function availableDrivers(): array
{
$local = array_keys(static::$drivers);
$registry = array_keys(Registry::allDrivers('ai'));
return array_values(array_unique(array_merge($local, $registry)));
}
/**
* Resolve a driver name to its concrete class via the local map then the Registry.
*/
protected static function resolveDriverClass(string $driver): ?string
{
if (isset(static::$drivers[$driver])) {
return static::$drivers[$driver];
}
$meta = Registry::driverMeta('ai', $driver);
return $meta['class'] ?? null;
}
}