| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves admin action names to handler instances. |
| 9 |
* |
| 10 |
* Exact action names are checked before prefix handlers so broad operations |
| 11 |
* like "bulk*" cannot shadow a specific registered action. |
| 12 |
*/ |
| 13 |
class ABJ_404_Solution_AdminActionRegistry { |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_PluginLogicAdminActions */ |
| 16 |
private $parent; |
| 17 |
|
| 18 |
public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) { |
| 19 |
$this->parent = $parent; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @param string $action |
| 24 |
* @return ABJ_404_Solution_AdminActionHandlerInterface|null |
| 25 |
*/ |
| 26 |
public function resolve(string $action) { |
| 27 |
$exact = $this->actionHandlerMap(); |
| 28 |
if (isset($exact[$action])) { |
| 29 |
return $this->instantiateHandler($exact[$action]); |
| 30 |
} |
| 31 |
foreach ($this->actionHandlerPrefixMap() as $prefix => $cls) { |
| 32 |
if ($action !== '' && strpos($action, $prefix) === 0) { |
| 33 |
return $this->instantiateHandler($cls); |
| 34 |
} |
| 35 |
} |
| 36 |
return null; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @return array<string, class-string<ABJ_404_Solution_AdminActionHandlerInterface>> |
| 41 |
*/ |
| 42 |
private function actionHandlerMap(): array { |
| 43 |
return array( |
| 44 |
'updateOptions' => 'ABJ_404_Solution_UpdateOptionsHandler', |
| 45 |
'addRedirect' => 'ABJ_404_Solution_AddRedirectHandler', |
| 46 |
'emptyRedirectTrash' => 'ABJ_404_Solution_EmptyRedirectTrashHandler', |
| 47 |
'emptyCapturedTrash' => 'ABJ_404_Solution_EmptyCapturedTrashHandler', |
| 48 |
'purgeRedirects' => 'ABJ_404_Solution_PurgeRedirectsHandler', |
| 49 |
'runMaintenance' => 'ABJ_404_Solution_RunMaintenanceHandler', |
| 50 |
'rebuildNgramCache' => 'ABJ_404_Solution_RebuildNgramCacheHandler', |
| 51 |
'clearSpellingCache' => 'ABJ_404_Solution_ClearSpellingCacheHandler', |
| 52 |
'saveGscSettings' => 'ABJ_404_Solution_SaveGscSettingsHandler', |
| 53 |
'importFromPlugin' => 'ABJ_404_Solution_ImportFromPluginHandler', |
| 54 |
'undoRegexAutoPromote' => 'ABJ_404_Solution_UndoRegexAutoPromoteHandler', |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @return array<string, class-string<ABJ_404_Solution_AdminActionHandlerInterface>> |
| 60 |
*/ |
| 61 |
private function actionHandlerPrefixMap(): array { |
| 62 |
return array( |
| 63 |
'bulk' => 'ABJ_404_Solution_BulkActionHandler', |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param class-string<ABJ_404_Solution_AdminActionHandlerInterface> $cls |
| 69 |
* @return ABJ_404_Solution_AdminActionHandlerInterface |
| 70 |
*/ |
| 71 |
private function instantiateHandler(string $cls): ABJ_404_Solution_AdminActionHandlerInterface { |
| 72 |
$reflect = new ReflectionClass($cls); |
| 73 |
$ctor = $reflect->getConstructor(); |
| 74 |
if ($ctor === null || $ctor->getNumberOfParameters() === 0) { |
| 75 |
$instance = $reflect->newInstance(); |
| 76 |
} else { |
| 77 |
$instance = $reflect->newInstance($this->parent); |
| 78 |
} |
| 79 |
if (!$instance instanceof ABJ_404_Solution_AdminActionHandlerInterface) { |
| 80 |
throw new RuntimeException("Handler class {$cls} does not implement AdminActionHandlerInterface."); |
| 81 |
} |
| 82 |
return $instance; |
| 83 |
} |
| 84 |
} |
| 85 |
|