searchEngine = $searchEngine; $this->identifier = $identifier; $this->domainEngine = $domainEngine; $this->providerOrchestrator = $providerOrchestrator; $this->debugMode = $debugMode ?? $this->isDebugEnabled(); $this->diagnosticsSink = $diagnosticsSink; $this->providerPlanContext = $providerPlanContext; } public function search(SearchRequest $request): SearchResult { $startedAt = microtime(true); $identifier = null; $domain = null; $providerPlan = null; $error = null; $planningTimeMs = 0.0; try { $identifier = $this->identifier->resolve($request->getQuery()); $domain = $this->domainEngine->resolve(new DomainContext( $identifier->getNormalizedValue(), $identifier->getIdentifierType(), $this->domainHint($request->getMediaType()) )); $providerPlan = $this->providerOrchestrator->plan($identifier, $domain, 'Missing'); } catch (\Throwable $exception) { $error = $exception->getMessage(); } $planningTimeMs = (microtime(true) - $startedAt) * 1000; // AUTO/UNKNOWN is a fan-out request owned by UniversalSearchService. // Activating a plan inferred from the identifier here would narrow the // registry to a single guessed domain before AUTO has a chance to // collect corroborating/enrichment providers from every domain. if ( $providerPlan !== null && $this->providerPlanContext !== null && ! in_array($request->getMediaType(), [MediaType::AUTO, MediaType::UNKNOWN], true) ) { $this->providerPlanContext->activate($providerPlan); } $result = null; $executionError = null; try { $result = $this->searchEngine->search($request); return $result; } catch (\Throwable $exception) { $executionError = $exception->getMessage(); throw $exception; } finally { if ($this->providerPlanContext !== null) { $this->providerPlanContext->clear(); } if ($this->debugMode) { $this->lastDiagnostics = new RuntimePlanningDiagnostics( $identifier, $domain, $providerPlan, $planningTimeMs, $error, $result?->getProviderTrace() ?? [], (microtime(true) - $startedAt) * 1000, $executionError ); if (is_callable($this->diagnosticsSink)) { try { ($this->diagnosticsSink)($this->lastDiagnostics); } catch (\Throwable $exception) { // Diagnostics must never affect the legacy search flow. } } } } } public function getLastDiagnostics(): ?RuntimePlanningDiagnostics { return $this->debugMode ? $this->lastDiagnostics : null; } private function domainHint(string $mediaType): ?string { return match ($mediaType) { MediaType::BOOK => DomainRegistry::BOOKS, MediaType::MOVIE => DomainRegistry::MOVIES, MediaType::GAME => DomainRegistry::GAMES, MediaType::MUSIC => DomainRegistry::MUSIC, MediaType::FOOD => DomainRegistry::FOOD, MediaType::MEDICINE => DomainRegistry::MEDICAL, MediaType::BEAUTY, MediaType::JEWELRY, MediaType::GENERIC => DomainRegistry::GENERIC_PRODUCTS, default => null, }; } private function isDebugEnabled(): bool { return (defined('WP_DEBUG') && WP_DEBUG) || (defined('TF_SMART_IMPORT_DEBUG') && TF_SMART_IMPORT_DEBUG); } }