Skip to content

Commit 2d75642

Browse files
committed
Fixes and improvements for the Dispatcher
1 parent da5f833 commit 2d75642

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

src/Infrastructure/Http/Dispatcher/Dispatcher.php

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Dispatcher implements DispatcherInterface
3333
/**
3434
* @var string
3535
*/
36-
protected $defaultNamespace = 'App';
36+
protected $defaultNamespace = '';
3737

3838
/**
3939
* @var string
@@ -48,7 +48,7 @@ class Dispatcher implements DispatcherInterface
4848
/**
4949
* @var string
5050
*/
51-
protected $classTemplate = '{namespace}\\Http\\RequestHandler\\{className}';
51+
protected $classTemplate = '{namespace}{className}';
5252

5353
/**
5454
* @var array
@@ -70,10 +70,65 @@ public function __construct(
7070
}
7171

7272
/**
73-
* @inheritDoc
73+
* @param string $namespace Namespace
74+
* @return $this
75+
*/
76+
public function setDefaultNamespace(string $namespace)
77+
{
78+
$this->defaultNamespace = $namespace;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* @param array $classParts Class Template Vars
85+
* @return $this
86+
*/
87+
public function setClassParts(array $classParts)
88+
{
89+
$this->setClassParts = $classParts;
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* @param string $template Template String
96+
* @return $this
97+
*/
98+
public function setClassTemplate(string $template)
99+
{
100+
$this->classTemplate = $template;
101+
}
102+
103+
/**
104+
* @param string $separator Separator
105+
* @return $this
74106
*/
75-
public function dispatch(ServerRequestInterface $request, $handler): ?ResponseInterface
107+
public function setMethodSeparator(string $separator)
76108
{
109+
$this->methodSeparator = $separator;
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* @param string $separator Separator
116+
* @return $this
117+
*/
118+
public function setNamespaceSeparator(string $separator)
119+
{
120+
$this->namespaceSeparator = $separator;
121+
122+
return $this;
123+
}
124+
125+
/**
126+
* @inheritDoc
127+
*/
128+
public function dispatch(
129+
ServerRequestInterface $request,
130+
$handler
131+
): ?ResponseInterface {
77132
if (is_string($handler)) {
78133
$handler = $this->stringHandler($handler, $request);
79134
}
@@ -89,8 +144,15 @@ public function dispatch(ServerRequestInterface $request, $handler): ?ResponseIn
89144
return null;
90145
}
91146

92-
protected function stringHandler(string $handler, ServerRequestInterface $request)
93-
{
147+
/**
148+
* @param string $handler Handler String
149+
* @param \Psr\Http\Message\ServerRequestInterface $request
150+
* @return mixed
151+
*/
152+
protected function stringHandler(
153+
string $handler,
154+
ServerRequestInterface $request
155+
) {
94156
$result = $this->parseString($handler);
95157

96158
if (!$this->container->has($result['className'])) {
@@ -99,17 +161,21 @@ protected function stringHandler(string $handler, ServerRequestInterface $reques
99161

100162
$handler = $this->container->get($result['className']);
101163

102-
if ($result['action' === null]) {
164+
if ($result['method'] === null) {
103165
return $handler;
104166
}
105167

106-
return $handler->{$result['action']}($request);
168+
return $handler->{$result['method']}($request);
107169
}
108170

171+
/**
172+
* @param string $handler Handler String
173+
* @return array
174+
*/
109175
protected function parseString(string $handler): array
110176
{
111177
$namespace = $this->defaultNamespace;
112-
$action = null;
178+
$method = null;
113179

114180
// Determine the namespace
115181
$position = strpos($handler, $this->namespaceSeparator);
@@ -121,7 +187,7 @@ protected function parseString(string $handler): array
121187
// Determine if there is an action besides a class
122188
$position = strpos($handler, $this->methodSeparator);
123189
if ($position) {
124-
$action = substr($handler, $position + 1);
190+
$method = substr($handler, $position + 1);
125191
$handler = substr($handler, 0, $position);
126192
}
127193

@@ -130,12 +196,16 @@ protected function parseString(string $handler): array
130196
$this->classParts['namespace'] = $namespace;
131197

132198
foreach ($this->classParts as $placeholder => $var) {
133-
$fqcn = str_replace('{' . (string)$placeholder . '}', (string)$var, $fqcn);
199+
$fqcn = str_replace(
200+
'{' . (string)$placeholder . '}',
201+
(string)$var,
202+
$fqcn
203+
);
134204
}
135205

136206
return [
137207
'className' => $fqcn,
138-
'method' => $action
208+
'method' => $method
139209
];
140210
}
141211
}

0 commit comments

Comments
 (0)