svc = $svc; $this->jobs = $jobs; } public function register(): void { $this->route('/jobs', 'GET', function (\WP_REST_Request $r) { $result = $this->jobs->list( ['status' => (string) $r->get_param('status'), 'type' => (string) $r->get_param('type')], max(1, (int) ($r->get_param('page') ?: 1)), (int) ($r->get_param('per_page') ?: 20) ); return ['items' => array_map(fn (Job $j) => $j->to_array(), $result['items']), 'total' => $result['total']]; }); $this->route('/jobs', 'POST', fn (\WP_REST_Request $r) => $this->created( $this->svc->create((string) $r->get_param('type'), (array) ($r->get_param('settings') ?? [])) )); $id = '/jobs/(?P[0-9a-f-]{36})'; $this->route($id, 'GET', fn (\WP_REST_Request $r) => [ 'job' => $this->svc->find($r['id'])->to_array(), 'events' => $this->jobs->events($r['id']), ]); $this->route($id . '/tick', 'POST', fn (\WP_REST_Request $r) => $this->svc->tick($r['id'])->to_array()); $this->route($id . '/pause', 'POST', fn (\WP_REST_Request $r) => $this->svc->pause($r['id'])->to_array()); $this->route($id . '/resume', 'POST', fn (\WP_REST_Request $r) => $this->svc->resume($r['id'])->to_array()); $this->route($id . '/cancel', 'POST', fn (\WP_REST_Request $r) => $this->svc->cancel($r['id'])->to_array()); $this->route($id . '/apply', 'POST', fn (\WP_REST_Request $r) => $this->created($this->svc->apply_preview($r['id']))); $this->route($id . '/restore', 'POST', fn (\WP_REST_Request $r) => $this->created($this->svc->create('restore', ['source_job' => $r['id']]))); $this->route($id . '/remote-restore', 'POST', fn (\WP_REST_Request $r) => $this->svc->remote_restore($r['id'])->to_array()); $this->route($id . '/preview', 'GET', fn (\WP_REST_Request $r) => $this->svc->find($r['id'])->stats['preview'] ?? []); $this->route($id . '/report', 'GET', function (\WP_REST_Request $r) { $job = $this->svc->find($r['id']); return [ 'filename' => 'relay-' . $job->type . '-' . substr($job->id, 0, 8) . '.txt', 'text' => Report::text($job, $this->jobs->events($job->id, 5000)), ]; }); } private function created(Job $job): \WP_REST_Response { return new \WP_REST_Response($job->to_array(), 201); } }