Skip to content

Commit acc2a80

Browse files
committed
Fixed hook chaining
1 parent d326a2c commit acc2a80

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/Hook.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,49 @@ class Hook
88
{
99
use ResolvesArgumentTrait;
1010

11-
protected $group;
12-
protected $operation;
13-
protected $path;
14-
protected $status = 200;
15-
protected $response = 'application/json';
11+
public $group;
12+
public $operation;
13+
public $path;
14+
public $status = 200;
15+
public $response = 'application/json';
16+
17+
public function __construct($group = null, $path = null, $operation = null, $status = null, $response = null)
18+
{
19+
if ($group) {
20+
$this->group = $group;
21+
}
22+
23+
if ($operation) {
24+
$this->operation = $operation;
25+
}
26+
27+
if ($path) {
28+
$this->path = $path;
29+
}
30+
31+
if ($status) {
32+
$this->status = $status;
33+
}
34+
35+
if ($response) {
36+
$this->response = $response;
37+
}
38+
}
1639

1740
public function __call($method, $args)
1841
{
1942
if (in_array($method, ['group', 'path', 'operation', 'status', 'response'])) {
20-
$this->{$method} = $args[0];
43+
$hook = $this->reCreateSelf();
44+
45+
$hook->{$method} = $args[0];
2146

2247
if (isset($args[1])) {
23-
if ($callable = $this->resolveArgument($args[1])) {
24-
return $callable($this);
48+
if ($callable = $hook->resolveArgument($args[1])) {
49+
return $callable($hook);
2550
}
2651
}
2752

28-
return $this;
53+
return $hook;
2954
}
3055

3156
if (in_array($method, ['before'])) {
@@ -39,6 +64,11 @@ public function __call($method, $args)
3964
}
4065
}
4166

67+
private function reCreateSelf()
68+
{
69+
return new static($this->group, $this->path, $this->operation, $this->status, $this->response);
70+
}
71+
4272
private function buildTransactionName()
4373
{
4474
return implode(' > ', [

src/ResolvesArgumentTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
trait ResolvesArgumentTrait
66
{
7-
protected function resolveArgument($argument)
7+
public function resolveArgument($argument)
88
{
99
if (is_array($argument)) {
1010
$method = $argument[1];

src/Transaction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function replaceInPath($old, $new)
2626
public function alterBody(\Closure $closure)
2727
{
2828
$requestBody = json_decode($this->transaction->request->body);
29-
$requestBody = $closure($requestBody);
29+
$closure($requestBody);
3030
$this->transaction->request->body = json_encode($requestBody);
3131
}
3232

0 commit comments

Comments
 (0)