-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handled API errors break preloaded content
- Loading branch information
Showing
9 changed files
with
140 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Flarum\Api; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Support\Collection; | ||
use Laminas\Stratigility\MiddlewarePipe; | ||
use Laminas\Stratigility\MiddlewarePipeInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class ClientMiddlewarePipe implements MiddlewarePipeInterface | ||
{ | ||
protected Collection $middlewares; | ||
protected MiddlewarePipeInterface $pipe; | ||
|
||
public function __construct( | ||
protected Container $container, | ||
array $middlewares | ||
) { | ||
$this->middlewares = new Collection($middlewares); | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return $this->getPipe()->process($request, $handler); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
return $this->getPipe()->handle($request); | ||
} | ||
|
||
public function pipe(MiddlewareInterface $middleware): void | ||
{ | ||
$this->middlewares->push($middleware); | ||
} | ||
|
||
protected function getPipe(): MiddlewarePipeInterface | ||
{ | ||
if (isset($this->pipe)) { | ||
return $this->pipe; | ||
} | ||
|
||
$this->pipe = new MiddlewarePipe(); | ||
|
||
foreach ($this->middlewares as $middleware) { | ||
$this->pipe->pipe($this->container->make($middleware)); | ||
} | ||
|
||
return $this->pipe; | ||
} | ||
|
||
public function errorHandling(bool $handleErrors): static | ||
{ | ||
$errorHandler = 'flarum.api.error_handler'; | ||
|
||
if ($handleErrors && ! $this->middlewares->contains($errorHandler)) { | ||
$this->middlewares = $this->middlewares->prepend($errorHandler); | ||
} elseif (! $handleErrors && $this->middlewares->contains($errorHandler)) { | ||
$this->middlewares = $this->middlewares->reject($errorHandler); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
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
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
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
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