Something went wrong. Try again.
A barebones implementation of an atproto PDS in PHP and Slim Framework 4.
pds php atproto
Something went wrong. Try again.
2.5 kB · 97 lines
PHP
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<?php
declare(strict_types=1);
namespace App\Application\Actions;
use App\Domain\DomainException\DomainRecordNotFoundException;use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;use Psr\Log\LoggerInterface;use Slim\Exception\HttpBadRequestException;use Slim\Exception\HttpNotFoundException;
abstract class Action{ protected LoggerInterface $logger;
protected Request $request;
protected Response $response;
/** @var array<string, string> */ protected array $args;
public function __construct(LoggerInterface $logger) { $this->logger = $logger; }
/** * @param array<string, string> $args * * @throws HttpNotFoundException * @throws HttpBadRequestException */ public function __invoke(Request $request, Response $response, array $args): Response { $this->request = $request; $this->response = $response; $this->args = $args;
try { return $this->action(); } catch (DomainRecordNotFoundException $e) { throw new HttpNotFoundException($this->request, $e->getMessage()); } }
/** * @throws DomainRecordNotFoundException * @throws HttpBadRequestException */ abstract protected function action(): Response;
/** * @return array<string, mixed>|object|null */ protected function getFormData() { /** @var array<string, mixed>|object|null $body */ $body = $this->request->getParsedBody(); return $body; }
/** * @return mixed * @throws HttpBadRequestException */ protected function resolveArg(string $name) { if (!isset($this->args[$name])) { throw new HttpBadRequestException($this->request, "Could not resolve argument `{$name}`."); }
return $this->args[$name]; }
/** * @param array<string, mixed>|object|null $data */ protected function respondWithData($data = null, int $statusCode = 200): Response { $payload = new ActionPayload($statusCode, $data);
return $this->respond($payload); }
protected function respond(ActionPayload $payload): Response { $json = (string) json_encode($payload, JSON_PRETTY_PRINT); $this->response->getBody()->write($json);
return $this->response ->withHeader('Content-Type', 'application/json') ->withStatus($payload->getStatusCode()); }}