| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Registers frontend runtime, admin presentation, AJAX, and shortcode services. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RuntimeServiceRegistration implements ABJ_404_Solution_Abj404ServiceRegistrationContract { |
| 11 |
|
| 12 |
/** @return string[] */ |
| 13 |
public static function serviceNames(): array { |
| 14 |
return ABJ_404_Solution_Abj404ServiceRegistrationContract::SERVICE_NAMES; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* @param ABJ_404_Solution_ServiceContainer $container |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function register($container): void { |
| 22 |
$container->set('previous_request_cookie_tracker', function($c) { |
| 23 |
return new ABJ_404_Solution_PreviousRequestCookieTracker( |
| 24 |
$c->get('logging') |
| 25 |
); |
| 26 |
}); |
| 27 |
|
| 28 |
$container->set('not_found_response', function($c) { |
| 29 |
return new ABJ_404_Solution_NotFoundResponseService( |
| 30 |
new ABJ_404_Solution_NotFoundResponseDependencies( |
| 31 |
$c->get('functions'), |
| 32 |
$c->get('redirects_repository'), |
| 33 |
$c->get('logs_repository'), |
| 34 |
$c->get('logging'), |
| 35 |
$c->get('options_repository'), |
| 36 |
$c->get('previous_request_cookie_tracker') |
| 37 |
) |
| 38 |
); |
| 39 |
}); |
| 40 |
|
| 41 |
$container->set('wordpress_connector', function($c) { |
| 42 |
return new ABJ_404_Solution_WordPress_Connector( |
| 43 |
new ABJ_404_Solution_WordPressConnectorDependencies($c->get('plugin_logic'), |
| 44 |
$c->get('redirects_repository'), $c->get('logging'), $c->get('functions'), |
| 45 |
$c->get('spell_checker'), $c->get('logs_repository'), $c->get('stats_repository'))); |
| 46 |
}); |
| 47 |
|
| 48 |
$container->set('slug_change_handler', function($c) { |
| 49 |
return new ABJ_404_Solution_SlugChangeHandler($c->get('content_repository'), |
| 50 |
$c->get('redirects_repository'), $c->get('logging')); |
| 51 |
}); |
| 52 |
|
| 53 |
// 'published_posts_provider' is intentionally NOT registered as a shared |
| 54 |
// service. Its only consumer (SpellPostListeners) builds a fresh provider |
| 55 |
// from the content repository it was injected with, because the provider |
| 56 |
// is stateful (batch cursor, restricted ids) and must scan the caller's |
| 57 |
// repository, not a globally-shared one. See |
| 58 |
// ABJ_404_Solution_SpellPostListeners::initializePublishedPostsProvider. |
| 59 |
|
| 60 |
$container->set('sync_utils', function($c) { |
| 61 |
return new ABJ_404_Solution_SynchronizationUtils(); |
| 62 |
}); |
| 63 |
|
| 64 |
$container->set('request_context', function($c) { |
| 65 |
// Reuse the per-process singleton so callers that reach the |
| 66 |
// context via the class accessor and callers that reach it via |
| 67 |
// this factory share the same intra-request message bus. |
| 68 |
// Producing a fresh instance here forks request-scoped state |
| 69 |
// between the two paths. |
| 70 |
return ABJ_404_Solution_RequestContext::resolveForContainer(); |
| 71 |
}); |
| 72 |
|
| 73 |
$container->set('ajax_security_gate', function($c) { |
| 74 |
// Build with no captured dependencies: the gate resolves |
| 75 |
// admin_access_policy and logging from the container lazily on each |
| 76 |
// authorization. The container caches this gate instance, so |
| 77 |
// capturing those services here would pin whatever was registered |
| 78 |
// at first resolution and ignore any later re-registration. |
| 79 |
return self::buildAjaxSecurityGate(); |
| 80 |
}); |
| 81 |
|
| 82 |
$container->set('ajax_failure_logger', function($c) { |
| 83 |
return self::buildAjaxFailureLogger(abj_service('logging')); |
| 84 |
}); |
| 85 |
|
| 86 |
$container->set('view', function($c) { |
| 87 |
return new ABJ_404_Solution_View( |
| 88 |
$c->get('functions'), |
| 89 |
$c->get('plugin_logic'), |
| 90 |
$c->get('view_read_service'), |
| 91 |
$c->get('logging'), |
| 92 |
$c->get('stats_repository') |
| 93 |
); |
| 94 |
}); |
| 95 |
|
| 96 |
$container->set('view_suggestions', function($c) { |
| 97 |
return new ABJ_404_Solution_View_Suggestions( |
| 98 |
$c->get('functions') |
| 99 |
); |
| 100 |
}); |
| 101 |
|
| 102 |
$container->set('shortcode', function($c) { |
| 103 |
return new ABJ_404_Solution_ShortCode(); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Build the AJAX security gate. With no arguments the gate resolves |
| 109 |
* admin_access_policy and logging lazily from the container on each |
| 110 |
* authorization (the robust default). Explicit dependencies may still be |
| 111 |
* passed by callers that want a fixed wiring. |
| 112 |
* |
| 113 |
* @param object|null|string $adminAccessPolicy Service exposing isPluginAdmin(). |
| 114 |
* @param object|null|string $logging Service exposing infoMessage(). |
| 115 |
* @return ABJ_404_Solution_AjaxSecurityGate |
| 116 |
*/ |
| 117 |
private static function buildAjaxSecurityGate( |
| 118 |
$adminAccessPolicy = ABJ_404_Solution_AjaxSecurityGate::RESOLVE_FROM_CONTAINER, |
| 119 |
$logging = ABJ_404_Solution_AjaxSecurityGate::RESOLVE_FROM_CONTAINER |
| 120 |
) { |
| 121 |
return new ABJ_404_Solution_AjaxSecurityGate($adminAccessPolicy, $logging); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param object|null $logging Service exposing writeLineToDebugFile(). |
| 126 |
* @return ABJ_404_Solution_AjaxFailureLogger |
| 127 |
*/ |
| 128 |
private static function buildAjaxFailureLogger($logging) { |
| 129 |
return new ABJ_404_Solution_AjaxFailureLogger($logging); |
| 130 |
} |
| 131 |
} |
| 132 |
|