| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Fans out a match request to the registered matching engines in order, picks |
| 9 |
* the first non-null match that survives the exclusion policy, and emits a |
| 10 |
* trace step per engine outcome (skipped / no-match / excluded / matched / |
| 11 |
* error). |
| 12 |
* |
| 13 |
* The engine list itself comes from the `abj404_matching_engines` filter via |
| 14 |
* `apply_filters` (resolved upstream); this class only iterates whatever it |
| 15 |
* was given. Profile-based engine filtering (ABJ_404_Solution_EngineProfileResolver) |
| 16 |
* runs inside run(). |
| 17 |
*/ |
| 18 |
class ABJ_404_Solution_MatchingEngineOrchestrator { |
| 19 |
|
| 20 |
/** @var array<int, mixed> Engines may contain non-engine items (filter abuse-tolerant). */ |
| 21 |
private $matchingEngines; |
| 22 |
|
| 23 |
/** @var ABJ_404_Solution_Logging */ |
| 24 |
private $logger; |
| 25 |
|
| 26 |
/** @var ABJ_404_Solution_RedirectExclusionPolicy */ |
| 27 |
private $exclusionPolicy; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param array<int, mixed> $matchingEngines |
| 31 |
* @param ABJ_404_Solution_Logging $logger |
| 32 |
* @param ABJ_404_Solution_RedirectExclusionPolicy $exclusionPolicy |
| 33 |
*/ |
| 34 |
function __construct(array $matchingEngines, $logger, ABJ_404_Solution_RedirectExclusionPolicy $exclusionPolicy) { |
| 35 |
$this->matchingEngines = $matchingEngines; |
| 36 |
$this->logger = $logger; |
| 37 |
$this->exclusionPolicy = $exclusionPolicy; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param ABJ_404_Solution_MatchRequest $request |
| 42 |
* @param ABJ_404_Solution_FrontendPipelineTrace $trace |
| 43 |
* @return ABJ_404_Solution_MatchResult|null |
| 44 |
*/ |
| 45 |
function run(ABJ_404_Solution_MatchRequest $request, ABJ_404_Solution_FrontendPipelineTrace $trace): ?ABJ_404_Solution_MatchResult { |
| 46 |
$enginesToRun = ABJ_404_Solution_EngineProfileResolver::getInstance() |
| 47 |
->resolve($request->getRequestedURL(), $this->matchingEngines); |
| 48 |
|
| 49 |
foreach ($enginesToRun as $engine) { |
| 50 |
if (!($engine instanceof ABJ_404_Solution_MatchingEngine)) { |
| 51 |
$this->logger->warn('Matching engine is not an instance of ABJ_404_Solution_MatchingEngine: ' . |
| 52 |
(is_object($engine) ? get_class($engine) : gettype($engine))); |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
try { |
| 57 |
if (!$engine->shouldRun($request)) { |
| 58 |
$this->logger->debugMessage('Engine skipped: ' . $engine->getName()); |
| 59 |
$trace->add('Engine: ' . $engine->getName(), 'Skipped', 'not applicable'); |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$result = $engine->match($request); |
| 64 |
|
| 65 |
if ($result === null) { |
| 66 |
$this->logger->debugMessage('Engine returned no match: ' . $engine->getName()); |
| 67 |
$trace->add('Engine: ' . $engine->getName(), 'No match'); |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
if ($result->getLink() === '') { |
| 72 |
$this->logger->debugMessage('Engine returned empty link, skipping: ' . $engine->getName()); |
| 73 |
$trace->add('Engine: ' . $engine->getName(), 'No match', 'empty link'); |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
if ($this->exclusionPolicy->isExcluded($result, $request->getOptions())) { |
| 78 |
$this->logger->debugMessage('Match excluded: ' . $engine->getName() . ' id=' . $result->getId()); |
| 79 |
$trace->add('Engine: ' . $engine->getName(), 'Excluded', 'post #' . $result->getId()); |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
$this->logger->debugMessage('Engine matched: ' . $engine->getName()); |
| 84 |
$trace->add( |
| 85 |
'Engine: ' . $engine->getName(), |
| 86 |
'Matched', |
| 87 |
'score ' . $result->getScore() . ' -> ' . $result->getLink() |
| 88 |
); |
| 89 |
return $result; |
| 90 |
} catch (\Throwable $e) { |
| 91 |
$this->logger->warn('Matching engine error (' . $engine->getName() . '): ' . $e->getMessage()); |
| 92 |
$trace->add('Engine: ' . $engine->getName(), 'Error', $e->getMessage()); |
| 93 |
continue; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$trace->add('Suggestion engines', 'No match found'); |
| 98 |
return null; |
| 99 |
} |
| 100 |
} |
| 101 |
|