|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Dispatcher\Resolver\Pipeline\Stages; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Controllers\Controller; |
|
6
|
|
|
use Nip\Dispatcher\Exceptions\InvalidCommandException; |
|
7
|
|
|
use Nip\Dispatcher\Resolver\ClassResolver\NameFormatter; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ClassInstanceStage |
|
13
|
|
|
* @package Nip\Dispatcher\Resolver\Pipeline\Stages |
|
14
|
|
|
*/ |
|
15
|
|
|
class ActionCallStage extends AbstractStage |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @return void |
|
19
|
|
|
* @throws \Exception |
|
20
|
|
|
*/ |
|
21
|
4 |
|
public function processCommand() |
|
22
|
|
|
{ |
|
23
|
4 |
|
if (!$this->hasInstanceAction()) { |
|
24
|
1 |
|
throw new InvalidCommandException( |
|
25
|
|
|
"No valid instance for callback in dispatcher command " . |
|
26
|
1 |
|
"[" . print_r($this->getCommand(), true) . "]" |
|
|
|
|
|
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
$return = $this->invokeAction(); |
|
31
|
3 |
|
$this->getCommand()->setReturn($return); |
|
32
|
3 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return ResponseInterface |
|
36
|
|
|
* @throws \Exception |
|
37
|
|
|
*/ |
|
38
|
3 |
|
protected function invokeAction() |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var Controller $controllerInstance */ |
|
41
|
3 |
|
$controllerInstance = $this->getCommand()->getActionParam('instance'); |
|
42
|
3 |
|
$controllerInstance->setRequest($this->getCommand()->getRequest()); |
|
43
|
3 |
|
$action = $this->getCommand()->getActionParam('action'); |
|
44
|
3 |
|
$method = NameFormatter::formatActionName($action); |
|
45
|
3 |
|
if (method_exists($controllerInstance, 'callAction')) { |
|
46
|
3 |
|
return $controllerInstance->callAction($method); |
|
47
|
|
|
} |
|
48
|
|
|
return $controllerInstance->{$method}(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|