-
Notifications
You must be signed in to change notification settings - Fork 35
Enhance AiClient::isConfigured() to support provider ID and class name #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
25d8ca1
d9a66f4
bcc57f1
e14a756
d718758
9f6a0d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| use WordPress\AiClient\ProviderImplementations\Google\GoogleProvider; | ||
| use WordPress\AiClient\ProviderImplementations\OpenAi\OpenAiProvider; | ||
| use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface; | ||
| use WordPress\AiClient\Providers\Contracts\ProviderInterface; | ||
| use WordPress\AiClient\Providers\Http\HttpTransporterFactory; | ||
| use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelConfig; | ||
|
|
@@ -114,14 +115,44 @@ public static function defaultRegistry(): ProviderRegistry | |
| /** | ||
| * Checks if a provider is configured and available for use. | ||
| * | ||
| * Supports multiple input formats for developer convenience: | ||
| * - ProviderAvailabilityInterface: Direct availability check (backward compatible) | ||
| * - string (provider ID): e.g., AiClient::isConfigured('openai') | ||
| * - string (class name): e.g., AiClient::isConfigured(OpenAiProvider::class) | ||
| * | ||
| * When using string input, this method leverages the ProviderRegistry's centralized | ||
| * dependency management, ensuring HttpTransporter and authentication are properly | ||
| * injected into availability instances. | ||
| * | ||
| * @since 0.1.0 | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @param ProviderAvailabilityInterface $availability The provider availability instance to check. | ||
| * @param ProviderAvailabilityInterface|string|class-string<ProviderInterface> $availabilityOrIdOrClassName | ||
| * The provider availability instance, provider ID, or provider class name. | ||
| * @param ProviderRegistry|null $registry Optional custom registry. If null, uses default. | ||
| * @return bool True if the provider is configured and available, false otherwise. | ||
| */ | ||
| public static function isConfigured(ProviderAvailabilityInterface $availability): bool | ||
| public static function isConfigured($availabilityOrIdOrClassName, ?ProviderRegistry $registry = null): bool | ||
|
||
| { | ||
| return $availability->isConfigured(); | ||
| // Handle direct ProviderAvailabilityInterface (backward compatibility) | ||
| if ($availabilityOrIdOrClassName instanceof ProviderAvailabilityInterface) { | ||
| return $availabilityOrIdOrClassName->isConfigured(); | ||
| } | ||
|
|
||
| // Handle string input (provider ID or class name) via registry | ||
| if (is_string($availabilityOrIdOrClassName)) { | ||
| $registry = $registry ?? self::defaultRegistry(); | ||
| return $registry->isProviderConfigured($availabilityOrIdOrClassName); | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException( | ||
| 'Parameter must be a ProviderAvailabilityInterface instance, provider ID string, or provider class name. ' . | ||
| sprintf( | ||
| 'Received: %s', | ||
| is_object($availabilityOrIdOrClassName) | ||
| ? get_class($availabilityOrIdOrClassName) | ||
| : gettype($availabilityOrIdOrClassName) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't purely for backward compatibility, we just want to support all 3 ways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved e14a756