← All changes
|
vendor/wpfluent/framework/src/WPFluent/Foundation/Async.php
+48
-40
1.0.90
→
2.11.0
View file →
| @@ -6,8 +6,11 @@ | ||
| 6 | 6 | use FluentCommunity\Framework\Support\Arr; |
| 7 | 7 | use FluentCommunity\Framework\Support\Helper; |
| 8 | 8 | use InvalidArgumentException; |
| 9 | 9 | |
| 10 | +/** | |
| 11 | + * @property \FluentCommunity\Framework\Foundation\Config $config | |
| 12 | + */ | |
| 10 | 13 | class Async |
| 11 | 14 | { |
| 12 | 15 | /** |
| 13 | 16 | * The dispatched handlers to stop recursion. |
| @@ -13,9 +16,9 @@ | ||
| 13 | 16 | * The dispatched handlers to stop recursion. |
| 14 | 17 | * |
| 15 | 18 | * @var array |
| 16 | 19 | */ |
| 17 | - protected $dispatched = []; | |
| 20 | + private $dispatched = []; | |
| 18 | 21 | |
| 19 | 22 | /** |
| 20 | 23 | * The application instance |
| 21 | 24 | * |
| @@ -54,24 +57,24 @@ | ||
| 54 | 57 | public static function init($app = null) |
| 55 | 58 | { |
| 56 | 59 | $app = $app ?: App::make(); |
| 57 | 60 | |
| 58 | - if (is_null(static::$instance)) { | |
| 59 | - static::$app = $app; | |
| 60 | - static::$instance = new static; | |
| 61 | + if (is_null(self::$instance)) { | |
| 62 | + self::$app = $app; | |
| 63 | + self::$instance = new static; | |
| 61 | 64 | } |
| 62 | 65 | |
| 63 | - $action = static::$instance->makeAsyncHookAction(); | |
| 66 | + $action = self::$instance->makeAsyncHookAction(); | |
| 64 | 67 | |
| 65 | - static::$app->addAction( | |
| 66 | - "admin_post_{$action}", [static::$instance, 'handle'] | |
| 68 | + self::$app->addAction( | |
| 69 | + "admin_post_{$action}", [self::$instance, 'handle'] | |
| 67 | 70 | ); |
| 68 | 71 | |
| 69 | - static::$app->addAction( | |
| 70 | - "admin_post_nopriv_{$action}", [static::$instance, 'handle'] | |
| 72 | + self::$app->addAction( | |
| 73 | + "admin_post_nopriv_{$action}", [self::$instance, 'handle'] | |
| 71 | 74 | ); |
| 72 | 75 | |
| 73 | - return static::$instance; | |
| 76 | + return self::$instance; | |
| 74 | 77 | } |
| 75 | 78 | |
| 76 | 79 | /** |
| 77 | 80 | * Makes the async hook action name |
| @@ -79,9 +82,9 @@ | ||
| 79 | 82 | * @return string |
| 80 | 83 | */ |
| 81 | 84 | public function makeAsyncHookAction() |
| 82 | 85 | { |
| 83 | - $slug = static::$app->config->get('app.slug'); | |
| 86 | + $slug = self::$app->config->get('app.slug'); | |
| 84 | 87 | |
| 85 | 88 | return "wpfluent_async_hook_{$slug}"; |
| 86 | 89 | } |
| 87 | 90 | |
| @@ -91,11 +94,11 @@ | ||
| 91 | 94 | * @return void |
| 92 | 95 | */ |
| 93 | 96 | public function handle() |
| 94 | 97 | { |
| 95 | - $post = static::$app->request->post(); | |
| 98 | + $post = self::$app->request->post(); | |
| 96 | 99 | |
| 97 | - $this->verifyRequest(static::$app, $post); | |
| 100 | + $this->verifyRequest(self::$app, $post); | |
| 98 | 101 | |
| 99 | 102 | $handlers = Arr::get($post, 'handlers', []); |
| 100 | 103 | |
| 101 | 104 | foreach ($handlers as $handler) { |
| @@ -101,9 +104,9 @@ | ||
| 101 | 104 | foreach ($handlers as $handler) { |
| 102 | 105 | try { |
| 103 | 106 | [$class, $action] = $this->resolveHandler($handler); |
| 104 | 107 | |
| 105 | - $this->execute(static::$app, $class, $action['params'] ?? []); | |
| 108 | + $this->execute(self::$app, $class, $action['params'] ?? []); | |
| 106 | 109 | |
| 107 | 110 | } catch (Exception $e) { |
| 108 | 111 | error_log($e->getMessage()); |
| 109 | 112 | } |
| @@ -112,9 +115,9 @@ | ||
| 112 | 115 | |
| 113 | 116 | /** |
| 114 | 117 | * Verify the request by checking the nonce. |
| 115 | 118 | * |
| 116 | - * @param FluentCommunity\Framework\Foundation\Application $app | |
| 119 | + * @param \FluentCommunity\Framework\Foundation\Application $app | |
| 117 | 120 | * @param array $data |
| 118 | 121 | * @return void |
| 119 | 122 | */ |
| 120 | 123 | protected function verifyRequest($app, $data) |
| @@ -131,9 +134,9 @@ | ||
| 131 | 134 | |
| 132 | 135 | /** |
| 133 | 136 | * Resolve the action handler. |
| 134 | 137 | * |
| 135 | - * @param JSON $handler | |
| 138 | + * @param array $action | |
| 136 | 139 | * @return array |
| 137 | 140 | */ |
| 138 | 141 | protected function resolveHandler($action) |
| 139 | 142 | { |
| @@ -173,37 +176,42 @@ | ||
| 173 | 176 | /** |
| 174 | 177 | * Add the async handler and register the shutdown handler |
| 175 | 178 | * All the handlers will be dispatched in a separate request |
| 176 | 179 | * |
| 177 | - * @param string (Class@handler or with __invoke method) $handler | |
| 180 | + * @param string $handler (Class@handler or with __invoke method) $handler | |
| 178 | 181 | * @return self |
| 179 | 182 | * @throws \InvalidArgumentException |
| 180 | 183 | */ |
| 181 | 184 | public static function call($handler, array $params = []) |
| 182 | 185 | { |
| 183 | - if (!static::$instance) { | |
| 186 | + if (!self::$instance) { | |
| 184 | 187 | static::init(); |
| 185 | 188 | } |
| 186 | 189 | |
| 187 | - static::$handlers[] = static::$instance->validate( | |
| 190 | + self::$handlers[] = self::$instance->validate( | |
| 188 | 191 | $handler, $params, static::sign(debug_backtrace(false, 1)[0]) |
| 189 | 192 | ); |
| 190 | 193 | |
| 191 | - return static::$instance->maybeRegisterShutDownHandler(); | |
| 194 | + return self::$instance->maybeRegisterShutDownHandler(); | |
| 192 | 195 | } |
| 193 | 196 | |
| 194 | 197 | /** |
| 195 | - * Queue the async handler and register the shutdown handler | |
| 196 | - * Queued handlers will be dispatched in a single request | |
| 197 | - * | |
| 198 | - * @param string (Class@handler or with __invoke method) $handler | |
| 198 | + * Queue an async handler to be executed during shutdown. | |
| 199 | + * | |
| 200 | + * Queued handlers are grouped by queue name and dispatched | |
| 201 | + * together in a single async HTTP request. | |
| 202 | + * | |
| 203 | + * @param string $handler The handler 'Class@method'|invokable class. | |
| 204 | + * @param array|string $params Array of args or the queue name if a string. | |
| 205 | + * @param string $name The name of the queue (default is 'default'). | |
| 199 | 206 | * @return self |
| 207 | + * | |
| 200 | 208 | * @throws \InvalidArgumentException |
| 201 | 209 | */ |
| 202 | 210 | public static function queue( |
| 203 | 211 | $handler, $params = [], $name = 'default' |
| 204 | 212 | ) { |
| 205 | - if (!static::$instance) { | |
| 213 | + if (!self::$instance) { | |
| 206 | 214 | static::init(); |
| 207 | 215 | } |
| 208 | 216 | |
| 209 | 217 | if (is_string($params)) { |
| @@ -210,13 +218,13 @@ | ||
| 210 | 218 | $name = $params; |
| 211 | 219 | $params = []; |
| 212 | 220 | } |
| 213 | 221 | |
| 214 | - static::$queue[$name][] = static::$instance->validate( | |
| 222 | + self::$queue[$name][] = self::$instance->validate( | |
| 215 | 223 | $handler, $params, static::sign(debug_backtrace(false, 1)[0]) |
| 216 | 224 | ); |
| 217 | 225 | |
| 218 | - return static::$instance->maybeRegisterShutDownHandler(); | |
| 226 | + return self::$instance->maybeRegisterShutDownHandler(); | |
| 219 | 227 | } |
| 220 | 228 | |
| 221 | 229 | /** |
| 222 | 230 | * Sign the handler to mark as dispatched. |
| @@ -231,10 +239,10 @@ | ||
| 231 | 239 | |
| 232 | 240 | /** |
| 233 | 241 | * Validate the handler and add a sign to mark as dispatched. |
| 234 | 242 | * |
| 235 | - * @param string (Class@handler or with __invoke method) $handler | |
| 236 | - * @return string | |
| 243 | + * @param string $handler (Class@handler or with __invoke method) $handler | |
| 244 | + * @return array | |
| 237 | 245 | * @throws \InvalidArgumentException |
| 238 | 246 | */ |
| 239 | 247 | public function validate($handler, $params, $sign) |
| 240 | 248 | { |
| @@ -274,19 +282,19 @@ | ||
| 274 | 282 | |
| 275 | 283 | /** |
| 276 | 284 | * Register the shutdown handler |
| 277 | 285 | * |
| 278 | - * @return void | |
| 286 | + * @return self | |
| 279 | 287 | */ |
| 280 | 288 | protected function maybeRegisterShutDownHandler() |
| 281 | 289 | { |
| 282 | - $handler = [static::$instance, 'dispatch']; | |
| 290 | + $handler = [self::$instance, 'dispatch']; | |
| 283 | 291 | |
| 284 | - if (!static::$app->hasAction('shutdown', $handler)) { | |
| 285 | - static::$app->addAction('shutdown', $handler); | |
| 292 | + if (!self::$app->hasAction('shutdown', $handler)) { | |
| 293 | + self::$app->addAction('shutdown', $handler); | |
| 286 | 294 | } |
| 287 | 295 | |
| 288 | - return static::$instance; | |
| 296 | + return self::$instance; | |
| 289 | 297 | } |
| 290 | 298 | |
| 291 | 299 | /** |
| 292 | 300 | * Dispatches the async request |
| @@ -295,10 +303,10 @@ | ||
| 295 | 303 | */ |
| 296 | 304 | public function dispatch() |
| 297 | 305 | { |
| 298 | 306 | $stacks = array_filter([ |
| 299 | - array_filter(static::$queue), | |
| 300 | - array_filter(static::$handlers), | |
| 307 | + array_filter(self::$queue), | |
| 308 | + array_filter(self::$handlers), | |
| 301 | 309 | ]); |
| 302 | 310 | |
| 303 | 311 | // At first we need to mark all the handlers from all |
| 304 | 312 | // the stacks as dispatched before sending any request. |
| @@ -330,9 +338,9 @@ | ||
| 330 | 338 | return array_filter($stack, function ($handler) { |
| 331 | 339 | if (isset($handler['sign'])) { |
| 332 | 340 | $isDispatched = in_array( |
| 333 | 341 | $handler['sign'], |
| 334 | - static::$app->request->post('dispatched', []) | |
| 342 | + self::$app->request->post('dispatched', []) | |
| 335 | 343 | ); |
| 336 | 344 | |
| 337 | 345 | if (!$isDispatched) { |
| 338 | 346 | $this->dispatched[] = $handler['sign']; |
| @@ -381,14 +389,14 @@ | ||
| 381 | 389 | * @return array |
| 382 | 390 | */ |
| 383 | 391 | protected function data($handler) |
| 384 | 392 | { |
| 385 | - $post = static::$app->request->post(); | |
| 393 | + $post = self::$app->request->post(); | |
| 386 | 394 | |
| 387 | 395 | $data = [ |
| 388 | 396 | 'handlers' => $handler, |
| 389 | 397 | 'wpfluent_async_nonce' => wp_create_nonce( |
| 390 | - static::$app->config->get('app.slug') | |
| 398 | + self::$app->config->get('app.slug') | |
| 391 | 399 | ), |
| 392 | 400 | 'dispatched' => array_unique(array_merge( |
| 393 | 401 | Arr::get($post, 'dispatched', []), |
| 394 | 402 | $this->dispatched, |
| @@ -408,9 +416,9 @@ | ||
| 408 | 416 | protected function url() |
| 409 | 417 | { |
| 410 | 418 | return admin_url('admin-post.php') . '?' . http_build_query( |
| 411 | 419 | array_merge( |
| 412 | - static::$app->request->query(), | |
| 420 | + self::$app->request->query(), | |
| 413 | 421 | ['action' => $this->makeAsyncHookAction()] |
| 414 | 422 | ) |
| 415 | 423 | ); |
| 416 | 424 | } |