| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
require_once __DIR__ . '/DatabaseErrorClassifier.php'; |
| 8 |
require_once __DIR__ . '/DatabaseRepairPolicy.php'; |
| 9 |
require_once __DIR__ . '/DatabaseSqlErrorReporter.php'; |
| 10 |
require_once __DIR__ . '/DatabaseCollationHelper.php'; |
| 11 |
require_once __DIR__ . '/DatabaseTableRepairer.php'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Sub-composition-root for the cohesive "recover and repair from DB failures" |
| 15 |
* cluster: classify a query error, decide whether to repair, run the repair, |
| 16 |
* recover from collation mismatches, and report SQL errors to the debug log. |
| 17 |
* |
| 18 |
* Owns five collaborators that previously lived as separate fields on |
| 19 |
* DatabaseCore. They are grouped here because they share one cohesive |
| 20 |
* concern (what to do when a query fails) and because each one's wiring |
| 21 |
* needs the same back-references (queryAndGetResults, harvest result, |
| 22 |
* runtime flags, plugin notices). Concentrating that wiring in one place |
| 23 |
* shrinks DatabaseCore's field count and centralizes recovery wiring. |
| 24 |
* |
| 25 |
* DatabaseCore exposes each of the five components via its existing |
| 26 |
* per-component accessor methods (errorClassifier(), repairPolicy(), |
| 27 |
* sqlErrorReporter(), collationHelper(), tableRepairer()); those |
| 28 |
* accessors now delegate here. The public contract from |
| 29 |
* DatabaseCoreInterface is unchanged. |
| 30 |
*/ |
| 31 |
class ABJ_404_Solution_DatabaseRecoveryServices { |
| 32 |
|
| 33 |
/** @var ABJ_404_Solution_DatabaseErrorClassifier */ |
| 34 |
private $errorClassifier; |
| 35 |
|
| 36 |
/** @var ABJ_404_Solution_DatabaseRepairPolicy */ |
| 37 |
private $repairPolicy; |
| 38 |
|
| 39 |
/** @var ABJ_404_Solution_DatabaseSqlErrorReporter */ |
| 40 |
private $sqlErrorReporter; |
| 41 |
|
| 42 |
/** @var ABJ_404_Solution_DatabaseCollationHelper */ |
| 43 |
private $collationHelper; |
| 44 |
|
| 45 |
/** @var ABJ_404_Solution_DatabaseTableRepairer */ |
| 46 |
private $tableRepairer; |
| 47 |
|
| 48 |
/** |
| 49 |
* @param ABJ_404_Solution_DatabaseCore $core |
| 50 |
* @param ABJ_404_Solution_Functions $functions |
| 51 |
* @param ABJ_404_Solution_Logging $logger |
| 52 |
* @param ABJ_404_Solution_DatabaseWpdbResultHarvester $resultHarvester |
| 53 |
* @param ABJ_404_Solution_DatabaseNoticeStateHolder $noticeState |
| 54 |
* @param ABJ_404_Solution_DatabaseQueryExecutor $queryExecutor |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
ABJ_404_Solution_DatabaseCore $core, |
| 58 |
$functions, |
| 59 |
$logger, |
| 60 |
ABJ_404_Solution_DatabaseWpdbResultHarvester $resultHarvester, |
| 61 |
ABJ_404_Solution_DatabaseNoticeStateHolder $noticeState, |
| 62 |
ABJ_404_Solution_DatabaseQueryExecutor $queryExecutor |
| 63 |
) { |
| 64 |
$this->errorClassifier = new ABJ_404_Solution_DatabaseErrorClassifier($core, $functions, $logger); |
| 65 |
$this->repairPolicy = new ABJ_404_Solution_DatabaseRepairPolicy($core, $this->errorClassifier, $functions, $logger); |
| 66 |
$this->sqlErrorReporter = new ABJ_404_Solution_DatabaseSqlErrorReporter($core, $logger); |
| 67 |
$this->collationHelper = new ABJ_404_Solution_DatabaseCollationHelper( |
| 68 |
function (string $query, array $options) use ($core): array { |
| 69 |
return $core->queryAndGetResults($query, $options); |
| 70 |
}, |
| 71 |
function (string $tableName) use ($core): string { |
| 72 |
// Call through $core so test-stub subclass overrides of |
| 73 |
// getCreateTableDDL are honored, matching pre-extraction behavior. |
| 74 |
return $core->tableNameResolver()->getCreateTableDDL($tableName); |
| 75 |
}, |
| 76 |
function (string $key) use ($noticeState) { |
| 77 |
return $noticeState->getRuntimeFlag($key); |
| 78 |
}, |
| 79 |
function (string $key, $value, int $ttl) use ($noticeState): void { |
| 80 |
$noticeState->setRuntimeFlag($key, $value, $ttl); |
| 81 |
}, |
| 82 |
function (array &$result) use ($resultHarvester): void { |
| 83 |
$resultHarvester->harvestWpdbResult($result); |
| 84 |
}, |
| 85 |
$logger |
| 86 |
); |
| 87 |
$this->tableRepairer = new ABJ_404_Solution_DatabaseTableRepairer( |
| 88 |
function (string $query, array $options) use ($core): array { |
| 89 |
return $core->queryAndGetResults($query, $options); |
| 90 |
}, |
| 91 |
function (array &$result) use ($resultHarvester): void { |
| 92 |
$resultHarvester->harvestWpdbResult($result); |
| 93 |
}, |
| 94 |
function () use ($queryExecutor): string { |
| 95 |
return $queryExecutor->getCurrentResultType(); |
| 96 |
}, |
| 97 |
function (string $type, string $message, string $guidance, string $errorString) use ($noticeState): void { |
| 98 |
$noticeState->setPluginDbNotice($type, $message, $guidance, $errorString); |
| 99 |
}, |
| 100 |
$functions, |
| 101 |
$logger |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** @return ABJ_404_Solution_DatabaseErrorClassifier */ |
| 106 |
public function errorClassifier(): ABJ_404_Solution_DatabaseErrorClassifier { |
| 107 |
return $this->errorClassifier; |
| 108 |
} |
| 109 |
|
| 110 |
/** @return ABJ_404_Solution_DatabaseRepairPolicy */ |
| 111 |
public function repairPolicy(): ABJ_404_Solution_DatabaseRepairPolicy { |
| 112 |
return $this->repairPolicy; |
| 113 |
} |
| 114 |
|
| 115 |
/** @return ABJ_404_Solution_DatabaseSqlErrorReporter */ |
| 116 |
public function sqlErrorReporter(): ABJ_404_Solution_DatabaseSqlErrorReporter { |
| 117 |
return $this->sqlErrorReporter; |
| 118 |
} |
| 119 |
|
| 120 |
/** @return ABJ_404_Solution_DatabaseCollationHelper */ |
| 121 |
public function collationHelper(): ABJ_404_Solution_DatabaseCollationHelper { |
| 122 |
return $this->collationHelper; |
| 123 |
} |
| 124 |
|
| 125 |
/** @return ABJ_404_Solution_DatabaseTableRepairer */ |
| 126 |
public function tableRepairer(): ABJ_404_Solution_DatabaseTableRepairer { |
| 127 |
return $this->tableRepairer; |
| 128 |
} |
| 129 |
} |
| 130 |
|