1
|
|
|
<?php |
2
|
|
|
namespace Kartenmacherei\RestFramework; |
3
|
|
|
|
4
|
|
|
use Kartenmacherei\RestFramework\Action\Action; |
5
|
|
|
use Kartenmacherei\RestFramework\Monitoring\TransactionMonitoring; |
6
|
|
|
use Kartenmacherei\RestFramework\Monitoring\TransactionNameMapper; |
7
|
|
|
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException; |
8
|
|
|
use Kartenmacherei\RestFramework\Request\Request; |
9
|
|
|
use Kartenmacherei\RestFramework\Request\UnauthorizedException; |
10
|
|
|
use Kartenmacherei\RestFramework\ResourceRequest\BadRequestException; |
11
|
|
|
use Kartenmacherei\RestFramework\Response\BadRequestResponse; |
12
|
|
|
use Kartenmacherei\RestFramework\Response\NotFoundResponse; |
13
|
|
|
use Kartenmacherei\RestFramework\Response\OptionsResponse; |
14
|
|
|
use Kartenmacherei\RestFramework\Response\Response; |
15
|
|
|
use Kartenmacherei\RestFramework\Response\UnauthorizedResponse; |
16
|
|
|
use Kartenmacherei\RestFramework\Router\NoMoreRoutersException; |
17
|
|
|
use Kartenmacherei\RestFramework\Router\ResourceRouter; |
18
|
|
|
use Kartenmacherei\RestFramework\Router\RouterChain; |
19
|
|
|
|
20
|
|
|
class Framework |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RouterChain |
24
|
|
|
*/ |
25
|
|
|
private $routerChain; |
26
|
|
|
/** |
27
|
|
|
* @var ActionMapper |
28
|
|
|
*/ |
29
|
|
|
private $actionMapper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TransactionMonitoring |
33
|
|
|
*/ |
34
|
|
|
private $transactionMonitoring; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var TransactionNameMapper |
38
|
|
|
*/ |
39
|
|
|
private $transactionNameMapper; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RouterChain $routerChain |
43
|
|
|
* @param ActionMapper $actionMapper |
44
|
|
|
* @param TransactionMonitoring $transactionMonitoring |
45
|
|
|
* @param TransactionNameMapper $transactionNameMapper |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
RouterChain $routerChain, |
49
|
|
|
ActionMapper $actionMapper, |
50
|
|
|
TransactionMonitoring $transactionMonitoring, |
51
|
|
|
TransactionNameMapper $transactionNameMapper |
52
|
|
|
) { |
53
|
|
|
$this->routerChain = $routerChain; |
54
|
|
|
$this->actionMapper = $actionMapper; |
55
|
|
|
$this->transactionMonitoring = $transactionMonitoring; |
56
|
|
|
$this->transactionNameMapper = $transactionNameMapper; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Config $config |
61
|
|
|
* @return Framework |
62
|
|
|
*/ |
63
|
|
|
public static function createInstance(Config $config): Framework |
64
|
|
|
{ |
65
|
|
|
$factory = new Factory($config); |
66
|
|
|
|
67
|
|
|
return new self( |
68
|
|
|
$factory->createRouterChain(), |
69
|
|
|
$factory->createActionMapper(), |
70
|
|
|
$factory->createTransactionMonitoring(), |
71
|
|
|
$factory->createTransactionNameMapper() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param ResourceRouter $router |
77
|
|
|
*/ |
78
|
|
|
public function registerResourceRouter(ResourceRouter $router) |
79
|
|
|
{ |
80
|
|
|
$this->routerChain->addRouter($router); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Request $request |
85
|
|
|
* @return Response |
86
|
|
|
* @throws UnsupportedRequestMethodException |
87
|
|
|
*/ |
88
|
|
|
public function run(Request $request): Response |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
$resource = $this->routerChain->route($request); |
92
|
|
|
if ($request->isOptionsRequest()) { |
93
|
|
|
return new OptionsResponse($resource->getSupportedMethods()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$action = $this->actionMapper->getAction($request, $resource); |
97
|
|
|
$this->setTransactionName($action); |
98
|
|
|
|
99
|
|
|
return $action->execute(); |
100
|
|
|
} catch (NoMoreRoutersException $e) { |
101
|
|
|
return new NotFoundResponse(); |
102
|
|
|
} catch (UnauthorizedException $e) { |
103
|
|
|
return new UnauthorizedResponse(); |
104
|
|
|
} catch (BadRequestException $e) { |
105
|
|
|
return new BadRequestResponse($e); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function setTransactionName(Action $action) |
110
|
|
|
{ |
111
|
|
|
$transactionName = $this->transactionNameMapper->getTransactionName(get_class($action)); |
112
|
|
|
$this->transactionMonitoring->nameTransaction($transactionName); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|