jonnybarnes.uk/app/Services/Micropub/MicropubHandlerRegistry.php
Jonny Barnes a8004f2958
Bring Micropub endpoint closer to the spec
- Detect form-encoded action=... requests, so form-encoded updates work
  and unhandled actions are no longer created as notes
- Default to h-entry when no type is given
- Read form-encoded photos from `photo` rather than `photos`
- Pass `category` through EntryData so bookmark tags are saved
- Accept single values for form-encoded multi-value properties
- Return 403 for insufficient_scope and 401 for invalid tokens
- Return 400 invalid_request for unsupported post types, update
  operations and models instead of reporting them as 500s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 22:26:41 +01:00

59 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Micropub;
use App\Exceptions\MicropubHandlerException;
use App\Services\Micropub\Handlers\MicropubHandlerInterface;
/**
* Maps Micropub post types to their handler instances.
*
* MicropubRequest normalises every incoming request and resolves it to a type
* string ("entry", "card", "update"). The controller asks the registry for the
* right handler, then asks the handler which data class to build, and finally
* calls handle() with that data object.
*
* Flow:
* MicropubRequest (normalise) → MicropubController
* → MicropubHandlerRegistry::getHandler($type)
* → $handler->dataClass()::fromArray($rawData)
* → $handler->handle($dataObject)
*
* Handlers are registered in MicropubServiceProvider. To support a new
* Micropub post type, create a handler in App\Services\Micropub\Handlers, a
* matching data class in App\Services\Micropub\Data, and register the handler
* here with its type string.
*/
class MicropubHandlerRegistry
{
/**
* @var MicropubHandlerInterface[]
*/
protected array $handlers = [];
/**
* Register a handler for a given Micropub type string.
*/
public function register(string $type, MicropubHandlerInterface $handler): self
{
$this->handlers[$type] = $handler;
return $this;
}
/**
* Retrieve the handler for a given type, or throw if none is registered.
*
* @throws MicropubHandlerException
*/
public function getHandler(string $type): MicropubHandlerInterface
{
if (! isset($this->handlers[$type])) {
throw new MicropubHandlerException("Unsupported Micropub request type '{$type}'");
}
return $this->handlers[$type];
}
}