generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionTrait.php
126 lines (109 loc) · 3.27 KB
/
ActionTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chevere\Action\Traits;
use Chevere\Action\Exceptions\ActionException;
use Chevere\Action\Interfaces\ReflectionActionInterface;
use Chevere\Action\ReflectionAction;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Throwable;
use function Chevere\Message\message;
use function Chevere\Parameter\mixed;
/**
* @method mixed main()
*/
trait ActionTrait
{
final public function __invoke(mixed ...$argument): mixed
{
try {
$reflection = static::assert();
$this->assertRuntime($reflection);
$arguments = $reflection->parameters()->__invoke(...$argument);
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...$this::getExceptionArguments($e),
);
}
$result = $this->main(...$arguments->toArray());
try {
$reflection->return()->__invoke($result);
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...$this::getExceptionArguments($e),
);
}
return $result;
}
public static function return(): ParameterInterface
{
return mixed();
}
public static function mainMethod(): string
{
return 'main';
}
final public static function parameters(): ParametersInterface
{
try {
$reflection = static::assert();
return $reflection->parameters();
} catch (Throwable $e) {
// @infection-ignore-all
throw new ActionException(
...self::getExceptionArguments($e),
);
}
}
final public static function assert(): ReflectionActionInterface
{
$reflection = new ReflectionAction(static::class);
static::assertStatic($reflection);
return $reflection;
}
/**
* Enables to define extra parameter assertion before the run method is called.
* @codeCoverageIgnore
*/
protected static function assertStatic(ReflectionActionInterface $reflection): void
{
// enables extra static assertion
}
/**
* Enables to define extra parameter assertion before the run method is called.
* @codeCoverageIgnore
*/
protected function assertRuntime(ReflectionActionInterface $reflection): void
{
// enables extra runtime assertion
}
// @phpstan-ignore-next-line
private static function getExceptionArguments(Throwable $e): array
{
// @infection-ignore-all
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
$message = (string) message(
'`%actor%` %exception% → %message%',
exception: $e::class,
actor: static::class,
message: $e->getMessage(),
);
// @infection-ignore-all
return [
$message,
$e,
$caller['file'] ?? 'na',
$caller['line'] ?? 0,
];
}
}