|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mcustiel\PowerRoute; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Mcustiel\PowerRoute\Common\Factories\ActionFactory; |
|
7
|
|
|
use Mcustiel\PowerRoute\Common\TransactionData; |
|
8
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
9
|
|
|
use Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherInterface; |
|
10
|
|
|
use Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory; |
|
11
|
|
|
use Mcustiel\PowerRoute\Common\ConfigOptions; |
|
12
|
|
|
|
|
13
|
|
|
class PowerRoute |
|
14
|
|
|
{ |
|
15
|
|
|
const CONDITIONS_MATCHER_ALL = 'allConditionsMatcher'; |
|
16
|
|
|
const CONDITIONS_MATCHER_ONE = 'oneConditionsMatcher'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array $config |
|
20
|
|
|
*/ |
|
21
|
|
|
private $config; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Mcustiel\PowerRoute\Common\Factories\ActionFactory $actionFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $actionFactory; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherInterface[] $conditionsMatchers |
|
28
|
|
|
*/ |
|
29
|
|
|
private $conditionMatchers; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory $conditionMatcherFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
private $conditionMatcherFactory; |
|
34
|
|
|
|
|
35
|
3 |
|
public function __construct( |
|
36
|
|
|
array $config, |
|
37
|
|
|
ActionFactory $actionFactory, |
|
38
|
|
|
ConditionsMatcherFactory $conditionsMatcherFactory |
|
39
|
|
|
) { |
|
40
|
3 |
|
$this->conditionMatchers = []; |
|
41
|
3 |
|
$this->config = $config; |
|
42
|
3 |
|
$this->conditionMatcherFactory = $conditionsMatcherFactory; |
|
43
|
3 |
|
$this->actionFactory = $actionFactory; |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setConfig(array $config) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
53
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
54
|
|
|
*/ |
|
55
|
|
|
public function start(ServerRequestInterface $request, ResponseInterface $response) |
|
56
|
|
|
{ |
|
57
|
|
|
$transactionData = new TransactionData($request, $response); |
|
58
|
|
|
$this->execute($this->config[ConfigOptions::CONFIG_ROOT_NODE], $transactionData); |
|
59
|
|
|
return $transactionData->getResponse(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $routeName |
|
64
|
|
|
* @param \PowerRoute\Common\TransactionData $transactionData |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function execute($routeName, TransactionData $transactionData) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$route = $this->config[ConfigOptions::CONFIG_NODES][$routeName]; |
|
69
|
|
|
|
|
70
|
3 |
|
$actions = $this->actionFactory->createFromConfig( |
|
71
|
3 |
|
$this->getActionsToRun( |
|
72
|
3 |
|
$route, |
|
73
|
3 |
|
$this->evaluateConditions($route, $transactionData->getRequest()) |
|
74
|
3 |
|
), |
|
75
|
|
|
$this |
|
76
|
3 |
|
); |
|
77
|
|
|
|
|
78
|
3 |
|
foreach ($actions as $action) { |
|
79
|
3 |
|
$action->getInstance()->execute($transactionData, $action->getArgument()); |
|
80
|
3 |
|
} |
|
81
|
3 |
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
private function evaluateConditions($route, $request) |
|
84
|
|
|
{ |
|
85
|
3 |
|
if (!$route[ConfigOptions::CONFIG_NODE_CONDITION]) { |
|
86
|
1 |
|
return true; |
|
87
|
|
|
} |
|
88
|
3 |
|
if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL])) { |
|
89
|
3 |
|
return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ALL)->matches( |
|
90
|
3 |
|
$route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL], |
|
91
|
|
|
$request |
|
92
|
3 |
|
); |
|
93
|
|
|
} |
|
94
|
2 |
|
if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE])) { |
|
95
|
2 |
|
return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ONE)->matches( |
|
96
|
2 |
|
$route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE], |
|
97
|
|
|
$request |
|
98
|
2 |
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new \RuntimeException('Invalid condition specified for route: ' . $route); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
private function getConditionsMatcher($matcher) |
|
105
|
|
|
{ |
|
106
|
3 |
|
if (!isset($this->conditionMatchers[$matcher])) { |
|
107
|
3 |
|
$this->conditionMatchers[$matcher] = $this->conditionMatcherFactory->get($matcher); |
|
108
|
3 |
|
} |
|
109
|
3 |
|
return $this->conditionMatchers[$matcher]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
private function getActionsToRun($route, $matched) |
|
113
|
|
|
{ |
|
114
|
3 |
|
if ($matched) { |
|
115
|
3 |
|
return $route[ConfigOptions::CONFIG_NODE_ACTIONS] |
|
116
|
3 |
|
[ConfigOptions::CONFIG_NODE_ACTIONS_MATCH]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
return $route[ConfigOptions::CONFIG_NODE_ACTIONS] |
|
120
|
2 |
|
[ConfigOptions::CONFIG_NODE_ACTIONS_NOTMATCH]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|