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('Early fail'); |
|
|
|
|
16
|
|
|
return ok(['newParam' => 'newValue']); |
17
|
|
|
}) |
18
|
|
|
->step([$this, 'nestedRailway']) |
19
|
|
|
->step([$this, 'castRequest']) |
20
|
|
|
->step([$this, 'validateRequest']) |
21
|
|
|
->step([$this, 'findUser']) |
22
|
|
|
->step([$this, 'updateDB']) |
23
|
|
|
->tryCatch([$this, 'sendNotification']) |
24
|
|
|
->always([$this, 'writeLog']) |
25
|
|
|
->failure([$this, 'notifyAdmin']); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $params |
30
|
|
|
*/ |
31
|
|
|
public function __invoke(array $params) : Result |
32
|
|
|
{ |
33
|
|
|
$result = $this->railway()->runWithParams($params); |
34
|
|
|
|
35
|
|
|
return $result; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function nestedRailway($params) |
39
|
|
|
{ |
40
|
|
|
return (new Railway) |
41
|
|
|
->step(function ($params) { |
|
|
|
|
42
|
|
|
//return error('Nested Railway failed!'); |
|
|
|
|
43
|
|
|
return ok(['nestedRwParam' => 'nestedRwValue']); |
44
|
|
|
}) |
45
|
|
|
->runWithParams($params); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function castRequest($params) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return ok(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function validateRequest($params) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return ok(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findUser($params) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
// pretend I am doing a query |
61
|
|
|
// $user = DB::findById($params['id']); |
|
|
|
|
62
|
|
|
$user = (object) ['id' => 123, 'name' => 'Eugene', 'phone' => '111111']; |
63
|
|
|
return ok(['model' => $user]); |
64
|
|
|
//return error('User not found!'); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function updateDB($params) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return ok(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function sendNotification($params) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
//throw new \Exception("Hey there, Exception!"); |
|
|
|
|
75
|
|
|
return ok(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function writeLog($params) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function notifyAdmin($params) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/* |
|
|
|
|
89
|
|
|
|
90
|
|
|
$castRequest = castRequest($request); // always success (one-way track) // Success |
91
|
|
|
$validRequest = validateRequest($castRequest); // true or false (two ways tracks) // Step |
92
|
|
|
$dbResult = updateDB($validRequest); // does not return (dead-end track) // Step |
93
|
|
|
sendNotification($dbResult, $validRequest); // try catch // TryCatch |
94
|
|
|
writeLog($dbResult, $validRequest); // supervisory (do smth for both tracks) // Proxy |
95
|
|
|
render($dbResult, $validRequest); |
96
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
// WRAPPER EXAMPLE WITH TRANSACTIONS |
|
|
|
|
100
|
|
|
//->step(function ($params) use ($dbConn) { |
101
|
|
|
// /** @var $pipe Pipe */ |
102
|
|
|
// $params['dbConn'] = $dbConn; |
103
|
|
|
// |
104
|
|
|
// return Pipe::with($params) |
105
|
|
|
// ->tryCatch(function ($params) { |
106
|
|
|
// return $params['dbConn']->beginTransaction(); |
107
|
|
|
// }) |
108
|
|
|
// ->step(function ($params) { |
109
|
|
|
// return $params['dbConn']->createCommand('SQL #1')->execute(); |
110
|
|
|
// }) |
111
|
|
|
// ->step(function ($params) { |
112
|
|
|
// return $params['dbConn']->createCommand('SQL #2')->execute(); |
113
|
|
|
// }) |
114
|
|
|
// ->tryCatch(function ($params) { |
115
|
|
|
// return $params['transaction']->commit(); |
116
|
|
|
// }) |
117
|
|
|
// ->fail(function ($params) { |
118
|
|
|
// return $params['transaction']->rollBack(); |
119
|
|
|
// }) |
120
|
|
|
// ->run(); |
121
|
|
|
//}) |
122
|
|
|
|
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: