-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
ShareErrorsFromSession.php
51 lines (42 loc) · 1.56 KB
/
ShareErrorsFromSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Http\Middleware;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Support\ViewErrorBag;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
/**
* Inspired by Illuminate\View\Middleware\ShareErrorsFromSession.
*
* @author Taylor Otwell
*/
class ShareErrorsFromSession implements Middleware
{
public function __construct(
protected ViewFactory $view
) {
}
public function process(Request $request, Handler $handler): Response
{
$session = $request->getAttribute('session');
// If the current session has an "errors" variable bound to it, we will share
// its value with all view instances so the views can easily access errors
// without having to bind. An empty bag is set when there aren't errors.
$this->view->share(
'errors',
$session->get('errors', new ViewErrorBag)
);
// Putting the errors in the view for every view allows the developer to just
// assume that some errors are always available, which is convenient since
// they don't have to continually run checks for the presence of errors.
$session->remove('errors');
return $handler->handle($request);
}
}