1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larapie\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Throwable; |
9
|
|
|
|
10
|
|
|
abstract class Action extends Controller |
11
|
|
|
{ |
12
|
|
|
use Concerns\HasAttributes; |
13
|
|
|
use Concerns\ResolvesMethodDependencies; |
|
|
|
|
14
|
|
|
use Concerns\ResolvesAuthorization; |
15
|
|
|
use Concerns\ResolvesValidation; |
16
|
|
|
use Concerns\ResolveIncludes; |
17
|
|
|
use Concerns\ResolveDefaults; |
18
|
|
|
use Concerns\ResolveCasting; |
19
|
|
|
use Concerns\RunsAsController; |
20
|
|
|
use Concerns\SuccessHook; |
21
|
|
|
use Concerns\FailHook; |
22
|
|
|
|
23
|
|
|
protected $actingAs; |
24
|
|
|
protected $runningAs = 'object'; |
25
|
|
|
|
26
|
63 |
|
public function __construct(array $attributes = []) |
27
|
|
|
{ |
28
|
63 |
|
$this->fill($attributes); |
29
|
|
|
|
30
|
63 |
|
if (method_exists($this, 'register')) { |
31
|
27 |
|
$this->register(); |
|
|
|
|
32
|
|
|
} |
33
|
63 |
|
} |
34
|
|
|
|
35
|
6 |
|
public static function createFrom(self $action) |
36
|
|
|
{ |
37
|
6 |
|
return (new static())->fill($action->all()); |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
public function runAs(self $action) |
41
|
|
|
{ |
42
|
6 |
|
if ($action->runningAs('controller')) { |
43
|
1 |
|
return $this->runAsController($action->getRequest()); |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
return $this->run(); |
47
|
|
|
} |
48
|
|
|
|
49
|
48 |
|
public function run(array $attributes = []) |
50
|
|
|
{ |
51
|
48 |
|
$this->resolveDefaults(); |
52
|
48 |
|
$this->fill($attributes); |
53
|
48 |
|
$this->resolveIncludes(); |
54
|
48 |
|
$this->resolveBeforeHook(); |
55
|
48 |
|
$this->resolveAuthorization(); |
56
|
46 |
|
$this->resolveValidation(); |
57
|
42 |
|
$this->resolveCasting(); |
58
|
|
|
try { |
59
|
42 |
|
$value = $this->resolveAndCall($this, 'handle'); |
60
|
6 |
|
} catch (Throwable $exception) { |
61
|
6 |
|
$this->failHook($exception); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return tap($value, function ($value) { |
65
|
36 |
|
$this->successHook($value); |
66
|
36 |
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
48 |
|
public function resolveBeforeHook() |
70
|
|
|
{ |
71
|
48 |
|
$method = 'as' . Str::studly($this->runningAs); |
72
|
|
|
|
73
|
48 |
|
if (method_exists($this, $method)) { |
74
|
1 |
|
return $this->resolveAndCall($this, $method); |
75
|
|
|
} |
76
|
47 |
|
} |
77
|
|
|
|
78
|
29 |
|
public function runningAs($matches) |
79
|
|
|
{ |
80
|
29 |
|
return in_array($this->runningAs, is_array($matches) ? $matches : func_get_args()); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function actingAs($user) |
84
|
|
|
{ |
85
|
2 |
|
$this->actingAs = $user; |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
public function user() |
91
|
|
|
{ |
92
|
6 |
|
return $this->actingAs ?? Auth::user(); |
93
|
|
|
} |
94
|
|
|
|
95
|
13 |
|
public function reset($user = null) |
96
|
|
|
{ |
97
|
13 |
|
$this->actingAs = $user; |
98
|
13 |
|
$this->attributes = []; |
99
|
13 |
|
$this->validator = null; |
100
|
13 |
|
} |
101
|
|
|
|
102
|
6 |
|
public function delegateTo($actionClass) |
103
|
|
|
{ |
104
|
6 |
|
return $actionClass::createFrom($this)->runAs($this); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function make(array $attributes = []) |
108
|
|
|
{ |
109
|
|
|
return new static($attributes); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function execute(array $attributes = []) |
113
|
|
|
{ |
114
|
|
|
return self::make($attributes)->run(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|