Skip to content

Instantly share code, notes, and snippets.

@Crell

Crell/events.php Secret

Created July 11, 2023 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Crell/f5929e2ee44decd4e9353c41874f26c8 to your computer and use it in GitHub Desktop.
Save Crell/f5929e2ee44decd4e9353c41874f26c8 to your computer and use it in GitHub Desktop.
Interface default method use case
<?php
interface CarriesError
{
public function setError(Error $error): static;
public function getError(): ?Error;
}
interface CarriesResponse
{
public function setResponse(ResponseInterface $response): static;
public function getResponse(): ?ResponseInterface;
}
trait ErrorCarrier
{
private Error $error;
public function setError(Error $error): static
{
$this->error = $error;
return $this;
}
public function getError(): ?Error
{
return $this->error ?? null;
}
}
trait ResponseCarrier
{
private ResponseInterface $response;
public function setResponse(ResponseInterface $response): static
{
$this->response = $response;
return $this;
}
public function getResponse(): ?ResponseInterface
{
return $this->response ?? null;
}
}
class PreRouting implements StoppableEventInterface, CarriesResponse, CarriesError
{
use ResponseCarrier;
use ErrorCarrier;
// @todo Here's where we want asymmetric visibility.
public function __construct(
private ServerRequestInterface $request,
) {}
public function request(): ServerRequestInterface
{
return $this->request;
}
public function setRequest(ServerRequestInterface $request): static
{
$this->request = $request;
return $this;
}
public function isPropagationStopped(): bool
{
return isset($this->response) || isset($this->error);
}
}
class PostRouting implements StoppableEventInterface, CarriesResponse, CarriesError
{
use ResponseCarrier;
use ErrorCarrier;
// @todo Here's where we want asymmetric visibility.
public function __construct(
private ServerRequestInterface $request,
) {}
public function request(): ServerRequestInterface
{
return $this->request;
}
public function setRequest(ServerRequestInterface $request): static
{
$this->request = $request;
return $this;
}
public function isPropagationStopped(): bool
{
return isset($this->response) || isset($this->error);
}
}
class HandleResponse implements CarriesResponse
{
public function __construct(
private ResponseInterface $response,
public readonly ServerRequestInterface $request,
) {}
public function setResponse(ResponseInterface $response): static
{
$this->response = $response;
return $this;
}
public function getResponse(): ResponseInterface
{
return $this->response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment