-
-
Save Crell/f5929e2ee44decd4e9353c41874f26c8 to your computer and use it in GitHub Desktop.
Interface default method use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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