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 Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory; |
9
|
|
|
use Mcustiel\PowerRoute\Common\ConfigOptions; |
10
|
|
|
use Mcustiel\Creature\LazyCreator; |
11
|
|
|
use Mcustiel\PowerRoute\Actions\Psr7MiddlewareAction; |
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
|
|
|
* @var \Mcustiel\Creature\LazyCreator $psr7InvokerCreator |
36
|
|
|
*/ |
37
|
|
|
private $psr7InvokerCreator; |
38
|
|
|
|
39
|
3 |
|
public function __construct( |
40
|
|
|
array $config, |
41
|
|
|
ActionFactory $actionFactory, |
42
|
|
|
ConditionsMatcherFactory $conditionsMatcherFactory |
43
|
|
|
) { |
44
|
3 |
|
$this->conditionMatchers = []; |
45
|
3 |
|
$this->config = $config; |
46
|
3 |
|
$this->conditionMatcherFactory = $conditionsMatcherFactory; |
47
|
3 |
|
$this->actionFactory = $actionFactory; |
48
|
3 |
|
$this->psr7InvokerCreator = new LazyCreator(Psr7MiddlewareAction::class); |
49
|
3 |
|
} |
50
|
|
|
|
51
|
|
|
public function setConfig(array $config) |
52
|
|
|
{ |
53
|
|
|
$this->config = $config; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
58
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
59
|
|
|
*/ |
60
|
|
|
public function start(ServerRequestInterface $request, ResponseInterface $response) |
61
|
|
|
{ |
62
|
|
|
$transactionData = new TransactionData($request, $response); |
63
|
|
|
$this->execute($this->config[ConfigOptions::CONFIG_ROOT_NODE], $transactionData); |
64
|
|
|
return $transactionData->getResponse(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $routeName |
69
|
|
|
* @param \PowerRoute\Common\TransactionData $transactionData |
70
|
|
|
*/ |
71
|
3 |
|
public function execute($routeName, TransactionData $transactionData) |
72
|
|
|
{ |
73
|
3 |
|
$route = $this->config[ConfigOptions::CONFIG_NODES][$routeName]; |
74
|
|
|
|
75
|
3 |
|
$actions = $this->actionFactory->createFromConfig( |
76
|
3 |
|
$this->getActionsToRun( |
77
|
3 |
|
$route, |
78
|
3 |
|
$this->evaluateConditions($route, $transactionData->getRequest()) |
79
|
3 |
|
), |
80
|
|
|
$this |
81
|
3 |
|
); |
82
|
|
|
|
83
|
3 |
|
foreach ($actions as $action) { |
84
|
3 |
|
$instance = $action->getInstance(); |
85
|
3 |
|
if (is_callable($instance)) { |
86
|
1 |
|
$this->psr7InvokerCreator->getInstance()->execute($transactionData, $action); |
87
|
1 |
|
} else { |
88
|
3 |
|
$instance->execute($transactionData, $action->getArgument()); |
89
|
|
|
} |
90
|
3 |
|
} |
91
|
3 |
|
} |
92
|
|
|
|
93
|
3 |
|
private function evaluateConditions($route, $request) |
94
|
|
|
{ |
95
|
3 |
|
if (!$route[ConfigOptions::CONFIG_NODE_CONDITION]) { |
96
|
1 |
|
return true; |
97
|
|
|
} |
98
|
3 |
|
if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL])) { |
99
|
3 |
|
return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ALL)->matches( |
100
|
3 |
|
$route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL], |
101
|
|
|
$request |
102
|
3 |
|
); |
103
|
|
|
} |
104
|
2 |
|
if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE])) { |
105
|
2 |
|
return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ONE)->matches( |
106
|
2 |
|
$route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE], |
107
|
|
|
$request |
108
|
2 |
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new \RuntimeException('Invalid condition specified for route: ' . $route); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
private function getConditionsMatcher($matcher) |
115
|
|
|
{ |
116
|
3 |
|
if (!isset($this->conditionMatchers[$matcher])) { |
117
|
3 |
|
$this->conditionMatchers[$matcher] = $this->conditionMatcherFactory->get($matcher); |
118
|
3 |
|
} |
119
|
3 |
|
return $this->conditionMatchers[$matcher]; |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
private function getActionsToRun($route, $matched) |
123
|
|
|
{ |
124
|
3 |
|
if ($matched) { |
125
|
3 |
|
return $route[ConfigOptions::CONFIG_NODE_ACTIONS] |
126
|
3 |
|
[ConfigOptions::CONFIG_NODE_ACTIONS_MATCH]; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $route[ConfigOptions::CONFIG_NODE_ACTIONS] |
130
|
2 |
|
[ConfigOptions::CONFIG_NODE_ACTIONS_NOTMATCH]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|