PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / frontend / FrontendLegacyAdapters.php

FrontendLegacyAdapters.php in 404 Solution trunk, at includes/frontend/FrontendLegacyAdapters.php

189 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Resolves legacy frontend pipeline collaborators into typed services.
9 */
10 class ABJ_404_Solution_FrontendLegacyAdapters {
11
12 /**
13 * @param mixed $logsRepository
14 * @param mixed $redirectsRepository
15 * @return mixed
16 */
17 function resolveLogsRepository($logsRepository, $redirectsRepository) {
18 if ($logsRepository !== null) {
19 return $logsRepository;
20 }
21 if (is_object($redirectsRepository) && method_exists($redirectsRepository, 'getLogsRepo')) {
22 try {
23 $resolved = $redirectsRepository->getLogsRepo();
24 return ($resolved !== null) ? $resolved : $redirectsRepository;
25 } catch (\Throwable $e) {
26 // allow-silent-catch: Mockery mocks throw if getLogsRepo() has no expectation;
27 // legacy tests may still construct a redirects mock without LogsRepo plumbing.
28 return (method_exists($redirectsRepository, 'logRedirectHit'))
29 ? $redirectsRepository
30 : abj_service('logs_repository');
31 }
32 }
33 if (is_object($redirectsRepository) && method_exists($redirectsRepository, 'logRedirectHit')) {
34 return $redirectsRepository;
35 }
36 return abj_service('logs_repository');
37 }
38
39 /**
40 * @param mixed $notFoundResponse
41 * @return ABJ_404_Solution_NotFoundResponseService
42 */
43 function resolveNotFoundResponse($notFoundResponse) {
44 $resolved = $notFoundResponse !== null ? $notFoundResponse : abj_service('not_found_response');
45 if ($resolved instanceof ABJ_404_Solution_NotFoundResponseService) {
46 return $resolved;
47 }
48 if (is_object($resolved)) {
49 $forceRedirect = array($resolved, 'forceRedirect');
50 $sendTo404Page = array($resolved, 'sendTo404Page');
51 $thereIsAUserSpecified404Page = array($resolved, 'thereIsAUserSpecified404Page');
52 if (is_callable($forceRedirect)
53 && is_callable($sendTo404Page)
54 && is_callable($thereIsAUserSpecified404Page)) {
55 return $this->adaptNotFoundResponse($forceRedirect, $sendTo404Page, $thereIsAUserSpecified404Page);
56 }
57 }
58 throw new InvalidArgumentException('FrontendRequestPipeline requires NotFoundResponseService.');
59 }
60
61 /**
62 * @param mixed $requestIgnoreNormalizer
63 * @param ABJ_404_Solution_RequestIgnoreNormalizerDependencies $deps
64 * @return ABJ_404_Solution_RequestIgnoreNormalizer
65 */
66 function resolveRequestIgnoreNormalizer($requestIgnoreNormalizer, ABJ_404_Solution_RequestIgnoreNormalizerDependencies $deps) {
67 if ($requestIgnoreNormalizer instanceof ABJ_404_Solution_RequestIgnoreNormalizer) {
68 return $requestIgnoreNormalizer;
69 }
70 if (is_object($requestIgnoreNormalizer)) {
71 $initializeIgnoreValues = array($requestIgnoreNormalizer, 'initializeIgnoreValues');
72 $tryNormalPostQuery = array($requestIgnoreNormalizer, 'tryNormalPostQuery');
73 if (is_callable($initializeIgnoreValues) && is_callable($tryNormalPostQuery)) {
74 return $this->adaptRequestIgnoreNormalizer($initializeIgnoreValues, $tryNormalPostQuery);
75 }
76 }
77 return new ABJ_404_Solution_RequestIgnoreNormalizer($deps);
78 }
79
80 /**
81 * @param mixed $previousRequestCookieTracker
82 * @param ABJ_404_Solution_Logging $logging
83 * @return ABJ_404_Solution_PreviousRequestCookieTracker
84 */
85 function resolvePreviousRequestCookieTracker($previousRequestCookieTracker, $logging) {
86 if ($previousRequestCookieTracker instanceof ABJ_404_Solution_PreviousRequestCookieTracker) {
87 return $previousRequestCookieTracker;
88 }
89 if (is_object($previousRequestCookieTracker)) {
90 $setCookieWithPreviousRequest = array($previousRequestCookieTracker, 'setCookieWithPreviousRequest');
91 $readCookieWithPreviousRqeuestShort = array($previousRequestCookieTracker, 'readCookieWithPreviousRqeuestShort');
92 if (is_callable($setCookieWithPreviousRequest) && is_callable($readCookieWithPreviousRqeuestShort)) {
93 return $this->adaptPreviousRequestCookieTracker($readCookieWithPreviousRqeuestShort, $setCookieWithPreviousRequest);
94 }
95 }
96 return new ABJ_404_Solution_PreviousRequestCookieTracker($logging);
97 }
98
99 /**
100 * @param callable $forceRedirect
101 * @param callable $sendTo404Page
102 * @param callable $thereIsAUserSpecified404Page
103 * @return ABJ_404_Solution_NotFoundResponseService
104 */
105 private function adaptNotFoundResponse($forceRedirect, $sendTo404Page, $thereIsAUserSpecified404Page) {
106 return new class($forceRedirect, $sendTo404Page, $thereIsAUserSpecified404Page) extends ABJ_404_Solution_NotFoundResponseService {
107 /** @var \Closure(string, int, mixed, string, bool): bool */
108 private \Closure $forceRedirectCallback;
109 /** @var \Closure(string, string, bool, mixed): void */
110 private \Closure $sendTo404PageCallback;
111 /** @var \Closure(mixed): bool */
112 private \Closure $thereIsAUserSpecified404PageCallback;
113
114 function __construct(callable $forceRedirect, callable $sendTo404Page, callable $thereIsAUserSpecified404Page) {
115 $this->forceRedirectCallback = \Closure::fromCallable($forceRedirect);
116 $this->sendTo404PageCallback = \Closure::fromCallable($sendTo404Page);
117 $this->thereIsAUserSpecified404PageCallback = \Closure::fromCallable($thereIsAUserSpecified404Page);
118 }
119
120 function forceRedirect(string $location, int $status = 302, $type = -1, string $requestedURL = '', bool $isCustom404 = false): bool {
121 return (bool)call_user_func($this->forceRedirectCallback, $location, $status, $type, $requestedURL, $isCustom404);
122 }
123
124 function sendTo404Page(string $requestedURL, string $reason = '', bool $useUserSpecified404 = true, $optionsOverride = null): void {
125 call_user_func($this->sendTo404PageCallback, $requestedURL, $reason, $useUserSpecified404, $optionsOverride);
126 }
127
128 function thereIsAUserSpecified404Page($dest404page): bool {
129 return (bool)call_user_func($this->thereIsAUserSpecified404PageCallback, $dest404page);
130 }
131 };
132 }
133
134 /**
135 * @param callable $initializeIgnoreValues
136 * @param callable $tryNormalPostQuery
137 * @return ABJ_404_Solution_RequestIgnoreNormalizer
138 */
139 private function adaptRequestIgnoreNormalizer($initializeIgnoreValues, $tryNormalPostQuery) {
140 return new class($initializeIgnoreValues, $tryNormalPostQuery) extends ABJ_404_Solution_RequestIgnoreNormalizer {
141 /** @var \Closure(string, string): void */
142 private \Closure $initializeIgnoreValuesCallback;
143 /** @var \Closure(array<string, mixed>): void */
144 private \Closure $tryNormalPostQueryCallback;
145
146 function __construct(callable $initializeIgnoreValues, callable $tryNormalPostQuery) {
147 $this->initializeIgnoreValuesCallback = \Closure::fromCallable($initializeIgnoreValues);
148 $this->tryNormalPostQueryCallback = \Closure::fromCallable($tryNormalPostQuery);
149 }
150
151 function initializeIgnoreValues(string $urlRequest, string $urlSlugOnly): void {
152 call_user_func($this->initializeIgnoreValuesCallback, $urlRequest, $urlSlugOnly);
153 }
154
155 function tryNormalPostQuery(array $options): void {
156 call_user_func($this->tryNormalPostQueryCallback, $options);
157 }
158 };
159 }
160
161 /**
162 * @param callable $readCookieWithPreviousRqeuestShort
163 * @param callable $setCookieWithPreviousRequest
164 * @return ABJ_404_Solution_PreviousRequestCookieTracker
165 */
166 private function adaptPreviousRequestCookieTracker($readCookieWithPreviousRqeuestShort, $setCookieWithPreviousRequest) {
167 return new class($readCookieWithPreviousRqeuestShort, $setCookieWithPreviousRequest) extends ABJ_404_Solution_PreviousRequestCookieTracker {
168 /** @var \Closure(): string */
169 private \Closure $readCookieWithPreviousRqeuestShortCallback;
170 /** @var \Closure(): void */
171 private \Closure $setCookieWithPreviousRequestCallback;
172
173 function __construct(callable $readCookieWithPreviousRqeuestShort, callable $setCookieWithPreviousRequest) {
174 $this->readCookieWithPreviousRqeuestShortCallback = \Closure::fromCallable($readCookieWithPreviousRqeuestShort);
175 $this->setCookieWithPreviousRequestCallback = \Closure::fromCallable($setCookieWithPreviousRequest);
176 }
177
178 function readCookieWithPreviousRqeuestShort(): string {
179 $value = call_user_func($this->readCookieWithPreviousRqeuestShortCallback);
180 return is_string($value) ? $value : '';
181 }
182
183 function setCookieWithPreviousRequest(): void {
184 call_user_func($this->setCookieWithPreviousRequestCallback);
185 }
186 };
187 }
188 }
189