1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use einfach\operation\Railway; |
4
|
|
|
use einfach\operation\Result; |
5
|
|
|
use function einfach\operation\response\ok; |
6
|
|
|
use function einfach\operation\response\error; |
|
|
|
|
7
|
|
|
|
8
|
|
|
class UpdateOperation implements \einfach\operation\IOperation |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
public function railway() : Railway |
11
|
|
|
{ |
12
|
|
|
return (new Railway) |
13
|
|
|
->step(function ($params) { |
14
|
|
|
echo "Hey {$params['name']}. Say hello to anonymous function!"; |
15
|
|
|
//return error($params, 'Early fail'); |
|
|
|
|
16
|
|
|
return ok($params, ['newParam' => 'newValue']); |
17
|
|
|
}, ['name' => 'First']) |
18
|
|
|
->step([$this, 'nestedRailway']) |
19
|
|
|
->step([$this, 'castRequest'], ['name' => 'CastReq']) |
20
|
|
|
->step([$this, 'validateRequest']) |
21
|
|
|
->step(function ($params) { |
22
|
|
|
return error($params, 'AAA!!!'); |
23
|
|
|
}, ['failFast' => true]) |
24
|
|
|
->step([$this, 'findUser']) |
25
|
|
|
->step([$this, 'updateDB']) |
26
|
|
|
->removeStep('CastReq') |
27
|
|
|
->tryCatch([$this, 'sendNotification']) |
28
|
|
|
->always([$this, 'writeLog']) |
29
|
|
|
->failure([$this, 'notifyAdmin'], ['name' => 'Last']) |
30
|
|
|
->step(function ($params) { |
31
|
|
|
return ok($params, ['a' => 'b']); |
32
|
|
|
}, ['after' => 'First', 'name' => 'FinalCheck']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $params |
37
|
|
|
*/ |
38
|
|
|
public function __invoke(array $params) : Result |
39
|
|
|
{ |
40
|
|
|
$result = $this->railway()->runWithParams($params); |
41
|
|
|
|
42
|
|
|
return $result; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function nestedRailway($params) |
46
|
|
|
{ |
47
|
|
|
return (new Railway) |
48
|
|
|
->step(function ($params) { |
49
|
|
|
//return error($params, 'Nested Railway failed!'); |
|
|
|
|
50
|
|
|
return ok($params, ['nestedRwParam' => 'nestedRwValue']); |
51
|
|
|
}) |
52
|
|
|
->runWithParams($params); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function castRequest($params) |
56
|
|
|
{ |
57
|
|
|
return ok($params); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function validateRequest($params) |
61
|
|
|
{ |
62
|
|
|
return ok($params); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function findUser($params) |
66
|
|
|
{ |
67
|
|
|
// pretend I am doing a query |
68
|
|
|
// $user = DB::findById($params['id']); |
|
|
|
|
69
|
|
|
$user = (object) ['id' => 123, 'name' => 'Eugene', 'phone' => '111111']; |
70
|
|
|
//return error($params, 'User not found!'); |
|
|
|
|
71
|
|
|
return ok($params, ['model' => $user]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function updateDB($params) |
75
|
|
|
{ |
76
|
|
|
return ok($params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function sendNotification($params) |
80
|
|
|
{ |
81
|
|
|
//throw new \Exception("Hey there, Exception!"); |
|
|
|
|
82
|
|
|
return ok($params); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function writeLog($params) |
86
|
|
|
{ |
87
|
|
|
return ok($params); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function notifyAdmin($params) |
91
|
|
|
{ |
92
|
|
|
return ok($params); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/* |
|
|
|
|
98
|
|
|
|
99
|
|
|
$castRequest = castRequest($request); // always success (one-way track) // Success |
100
|
|
|
$validRequest = validateRequest($castRequest); // true or false (two ways tracks) // Step |
101
|
|
|
$dbResult = updateDB($validRequest); // does not return (dead-end track) // Step |
102
|
|
|
sendNotification($dbResult, $validRequest); // try catch // TryCatch |
103
|
|
|
writeLog($dbResult, $validRequest); // supervisory (do smth for both tracks) // Proxy |
104
|
|
|
render($dbResult, $validRequest); |
105
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
// WRAPPER EXAMPLE WITH TRANSACTIONS |
|
|
|
|
109
|
|
|
//->step(function ($params) use ($dbConn) { |
110
|
|
|
// /** @var $pipe Pipe */ |
111
|
|
|
// $params['dbConn'] = $dbConn; |
112
|
|
|
// |
113
|
|
|
// return Pipe::with($params) |
114
|
|
|
// ->tryCatch(function ($params) { |
115
|
|
|
// return $params['dbConn']->beginTransaction(); |
116
|
|
|
// }) |
117
|
|
|
// ->step(function ($params) { |
118
|
|
|
// return $params['dbConn']->createCommand('SQL #1')->execute(); |
119
|
|
|
// }) |
120
|
|
|
// ->step(function ($params) { |
121
|
|
|
// return $params['dbConn']->createCommand('SQL #2')->execute(); |
122
|
|
|
// }) |
123
|
|
|
// ->tryCatch(function ($params) { |
124
|
|
|
// return $params['transaction']->commit(); |
125
|
|
|
// }) |
126
|
|
|
// ->fail(function ($params) { |
127
|
|
|
// return $params['transaction']->rollBack(); |
128
|
|
|
// }) |
129
|
|
|
// ->run(); |
130
|
|
|
//}) |
131
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: