|
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
|
66 |
|
public function __construct(array $attributes = []) |
|
27
|
|
|
{ |
|
28
|
66 |
|
$this->fill($attributes); |
|
29
|
|
|
|
|
30
|
66 |
|
if (method_exists($this, 'register')) { |
|
31
|
27 |
|
$this->register(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
66 |
|
} |
|
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 |
|
$this->actingAs($action->user()); |
|
47
|
|
|
|
|
48
|
5 |
|
return $this->run(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function runUnauthorized(array $attributes = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->run($attributes, false); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
51 |
|
public function run(array $attributes = [], $authorization = true) |
|
57
|
|
|
{ |
|
58
|
51 |
|
$this->resolveDefaults(); |
|
59
|
51 |
|
$this->fill($attributes); |
|
60
|
51 |
|
$this->resolveIncludes(); |
|
61
|
51 |
|
$this->resolveBeforeHook(); |
|
62
|
|
|
|
|
63
|
51 |
|
if ($authorization) |
|
64
|
51 |
|
$this->resolveAuthorization(); |
|
65
|
|
|
|
|
66
|
49 |
|
$this->resolveValidation(); |
|
67
|
|
|
try { |
|
68
|
45 |
|
$value = $this->resolveAndCall($this, 'handle'); |
|
69
|
6 |
|
} catch (Throwable $exception) { |
|
70
|
6 |
|
$this->failHook($exception); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return tap($value, function ($value) { |
|
74
|
39 |
|
$this->successHook($value); |
|
75
|
39 |
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
51 |
|
public function resolveBeforeHook() |
|
79
|
|
|
{ |
|
80
|
51 |
|
$method = 'as' . Str::studly($this->runningAs); |
|
81
|
|
|
|
|
82
|
51 |
|
if (method_exists($this, $method)) { |
|
83
|
1 |
|
return $this->resolveAndCall($this, $method); |
|
84
|
|
|
} |
|
85
|
50 |
|
} |
|
86
|
|
|
|
|
87
|
29 |
|
public function runningAs($matches) |
|
88
|
|
|
{ |
|
89
|
29 |
|
return in_array($this->runningAs, is_array($matches) ? $matches : func_get_args()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
7 |
|
public function actingAs($user) |
|
93
|
|
|
{ |
|
94
|
7 |
|
$this->actingAs = $user; |
|
95
|
|
|
|
|
96
|
7 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
11 |
|
public function user() |
|
100
|
|
|
{ |
|
101
|
11 |
|
return $this->actingAs ?? Auth::user(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
13 |
|
public function reset($user = null) |
|
105
|
|
|
{ |
|
106
|
13 |
|
$this->actingAs = $user; |
|
107
|
13 |
|
$this->attributes = []; |
|
108
|
13 |
|
$this->validator = null; |
|
109
|
13 |
|
} |
|
110
|
|
|
|
|
111
|
6 |
|
public function delegateTo($actionClass) |
|
112
|
|
|
{ |
|
113
|
6 |
|
return $actionClass::createFrom($this)->runAs($this); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public static function make(array $attributes = []) |
|
117
|
|
|
{ |
|
118
|
|
|
return new static($attributes); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function execute(array $attributes = [], $authorized = true) |
|
122
|
|
|
{ |
|
123
|
|
|
return self::make()->run($attributes, $authorized); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|