1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2019 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Fisharebest\Webtrees\Http\Middleware; |
20
|
|
|
|
21
|
|
|
use Illuminate\Support\Str; |
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
24
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
25
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
26
|
|
|
|
27
|
|
|
use function app; |
28
|
|
|
use function explode; |
29
|
|
|
use function is_string; |
30
|
|
|
use function substr_compare; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Middleware to run a controller/action or a request-handler. |
34
|
|
|
*/ |
35
|
|
|
class WrapHandler implements MiddlewareInterface |
36
|
|
|
{ |
37
|
|
|
private const CONTROLLER_NAMESPACE = '\\Fisharebest\\Webtrees\\Http\\Controllers\\'; |
38
|
|
|
|
39
|
|
|
// To parse Controller::action |
40
|
|
|
private const SCOPE_OPERATOR = '::'; |
41
|
|
|
|
42
|
|
|
/** @var string|MiddlewareInterface */ |
43
|
|
|
private $handler; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* WrapController constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $handler |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $handler) |
51
|
|
|
{ |
52
|
|
|
$this->handler = $handler; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ServerRequestInterface $request |
57
|
|
|
* @param RequestHandlerInterface $handler |
58
|
|
|
* |
59
|
|
|
* @return ResponseInterface |
60
|
|
|
*/ |
61
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
62
|
|
|
{ |
63
|
|
|
if (is_string($this->handler)) { |
64
|
|
|
if (Str::contains($this->handler, self::SCOPE_OPERATOR)) { |
65
|
|
|
[$class, $method] = explode(self::SCOPE_OPERATOR, $this->handler); |
66
|
|
|
|
67
|
|
|
if (substr_compare($class, '\\', 0, 1) !== 0) { |
68
|
|
|
$class = self::CONTROLLER_NAMESPACE . $class; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$controller = app($class); |
72
|
|
|
|
73
|
|
|
return $controller->$method($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return app($this->handler)->handle($request); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->handler->handle($request); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|