Skip to content

Commit f4ee6b4

Browse files
committed
Added the ConditionalLoader
1 parent f6ef988 commit f4ee6b4

File tree

2 files changed

+90
-149
lines changed

2 files changed

+90
-149
lines changed

src/Builder/AlternativeLoader.php

Lines changed: 54 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Kiboko\Plugin\SQL\Builder;
44

5+
use Kiboko\Component\SatelliteToolbox\Builder\IsolatedCodeBuilder;
56
use Kiboko\Component\SatelliteToolbox\Builder\IsolatedValueAppendingBuilder;
7+
use Kiboko\Component\SatelliteToolbox\Builder\IsolatedValueTransformationBuilder;
68
use Kiboko\Contract\Configurator\StepBuilderInterface;
79
use PhpParser\Builder;
810
use PhpParser\Node;
@@ -14,15 +16,15 @@ final class AlternativeLoader implements StepBuilderInterface
1416
private ?Node\Expr $state;
1517
/** @var array<Node\Expr> */
1618
private array $parameters;
17-
private ?Builder $merge;
1819

19-
public function __construct(private Node\Expr $query)
20+
public function __construct(
21+
private Node\Expr $query
22+
)
2023
{
2124
$this->logger = null;
2225
$this->rejection = null;
2326
$this->state = null;
2427
$this->parameters = [];
25-
$this->merge = null;
2628
}
2729

2830
public function withLogger(Node\Expr $logger): StepBuilderInterface
@@ -62,150 +64,72 @@ public function withMerge(Builder $merge): self
6264

6365
public function getNode(): Node
6466
{
65-
return (new IsolatedValueAppendingBuilder(
66-
new Node\Expr\Variable('input'),
67-
new Node\Expr\Variable('output'),
68-
[
69-
...array_filter(
70-
[
71-
$this->getAlternativeLookupNode(),
72-
$this->merge?->getNode(),
73-
new Node\Stmt\Return_(
74-
new Node\Expr\Variable('output')
75-
),
76-
]
77-
)
78-
],
79-
new Node\Expr\Variable('dbh'),
80-
))->getNode();
81-
}
82-
83-
public function getAlternativeLookupNode() : Node
84-
{
85-
return (new IsolatedValueAppendingBuilder(
86-
new Node\Expr\Variable('input'),
87-
new Node\Expr\Variable('lookup'),
88-
[
89-
new Node\Stmt\TryCatch(
90-
stmts: [
67+
return new Node\Stmt\Expression(
68+
expr: new Node\Expr\FuncCall(
69+
name: new Node\Expr\Closure([
70+
'params' => [
71+
new Node\Param(
72+
var: new Node\Expr\Variable('input'),
73+
)
74+
],
75+
'stmts' => [
9176
new Node\Stmt\Expression(
9277
expr: new Node\Expr\Assign(
93-
var: new Node\Expr\Variable('stmt'),
78+
var: new Node\Expr\Variable('statement'),
9479
expr: new Node\Expr\MethodCall(
95-
var: new Node\Expr\Variable('dbh'),
96-
name: new Node\Identifier('prepare'),
80+
var: new Node\Expr\Variable('connection'),
81+
name: new Node\Name('prepare'),
9782
args: [
98-
new Node\Arg($this->query)
99-
],
100-
),
101-
),
83+
new Node\Arg(
84+
value: $this->query
85+
)
86+
]
87+
)
88+
)
10289
),
10390
...$this->compileParameters(),
10491
new Node\Stmt\Expression(
10592
expr: new Node\Expr\MethodCall(
106-
var: new Node\Expr\Variable('stmt'),
107-
name: new Node\Identifier('execute')
108-
),
109-
),
110-
new Node\Stmt\Expression(
111-
expr: new Node\Expr\Assign(
112-
var: new Node\Expr\Variable('data'),
113-
expr: new Node\Expr\MethodCall(
114-
var: new Node\Expr\Variable('stmt'),
115-
name: new Node\Identifier('fetch'),
116-
args: [
117-
new Node\Arg(
118-
new Node\Expr\ClassConstFetch(
119-
class: new Node\Name\FullyQualified('PDO'),
120-
name: new Node\Identifier('FETCH_NAMED')
121-
),
122-
),
123-
],
124-
),
125-
),
126-
),
127-
new Node\Stmt\Expression(
128-
expr: new Node\Expr\Assign(
129-
var: new Node\Expr\Variable('dbh'),
130-
expr: new Node\Expr\ConstFetch(
131-
name: new Node\Name('null')
132-
),
133-
),
134-
),
135-
new Node\Stmt\Return_(
136-
expr: new Node\Expr\Variable('data')
93+
var: new Node\Expr\Variable('statement'),
94+
name: new Node\Name('execute'),
95+
)
13796
)
13897
],
139-
catches: [
140-
new Node\Stmt\Catch_(
141-
types: [
142-
new Node\Name\FullyQualified('PDOException')
143-
],
144-
var: new Node\Expr\Variable('exception'),
145-
stmts: [
146-
new Node\Stmt\Expression(
147-
expr: new Node\Expr\MethodCall(
148-
var: new Node\Expr\PropertyFetch(
149-
var: new Node\Expr\Variable('this'),
150-
name: 'logger',
151-
),
152-
name: new Node\Identifier('critical'),
153-
args: [
154-
new Node\Arg(
155-
value: new Node\Expr\MethodCall(
156-
var: new Node\Expr\Variable('exception'),
157-
name: new Node\Identifier('getMessage'),
158-
),
159-
),
160-
new Node\Arg(
161-
value: new Node\Expr\Array_(
162-
items: [
163-
new Node\Expr\ArrayItem(
164-
value: new Node\Expr\Variable('exception'),
165-
key: new Node\Scalar\String_('exception'),
166-
),
167-
],
168-
attributes: [
169-
'kind' => Node\Expr\Array_::KIND_SHORT,
170-
],
171-
),
172-
),
173-
]
174-
),
175-
),
176-
],
177-
),
98+
'uses' => [
99+
new Node\Expr\Variable('connection')
178100
],
179-
),
180-
],
181-
new Node\Expr\Variable('dbh'),
182-
))->getNode();
101+
]),
102+
args: [
103+
new Node\Arg(
104+
value: new Node\Expr\Variable('input')
105+
),
106+
]
107+
)
108+
);
183109
}
184110

185-
/**
186-
* @return array<int, Node\Stmt\Expression>
187-
*/
188-
public function compileParameters(): array
111+
public function compileParameters(): iterable
189112
{
190-
$output = [];
191-
192113
foreach ($this->parameters as $key => $parameter) {
193-
$output[] = new Node\Stmt\Expression(
194-
expr: new Node\Expr\MethodCall(
195-
var: new Node\Expr\Variable('stmt'),
196-
name: new Node\Identifier('bindParam'),
114+
yield new Node\Stmt\Expression(
115+
new Node\Expr\MethodCall(
116+
var: new Node\Expr\Variable('statement'),
117+
name: new Node\Name('bindParam'),
197118
args: [
198-
new Node\Arg(
199-
is_string($key) ? new Node\Scalar\Encapsed([new Node\Scalar\EncapsedStringPart(':'), new Node\Scalar\EncapsedStringPart($key)]) : new Node\Scalar\LNumber($key)
200-
),
201-
new Node\Arg(
202-
$parameter
203-
),
204-
],
205-
),
119+
new Node\Arg(
120+
is_string($key) ? new Node\Scalar\Encapsed(
121+
[
122+
new Node\Scalar\EncapsedStringPart(':'),
123+
new Node\Scalar\EncapsedStringPart($key)
124+
]
125+
) : new Node\Scalar\LNumber($key)
126+
),
127+
new Node\Arg(
128+
$parameter
129+
),
130+
],
131+
)
206132
);
207133
}
208-
209-
return $output;
210134
}
211135
}

src/Builder/ConditionalLoader.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function withConnection(Node\Expr|Connection $connection): StepBuilderInt
5656
return $this;
5757
}
5858

59-
public function addAlternative(Node\Expr $condition, AlternativeLoader $loader): self
59+
public function addAlternative(Node\Expr $condition, AlternativeLoader $lookup): self
6060
{
61-
$this->alternatives[] = [$condition, $loader];
61+
$this->alternatives[] = [$condition, $lookup];
6262

6363
return $this;
6464
}
@@ -101,32 +101,49 @@ public function withAfterQueries(?InitializerQueries ...$queries): self
101101
return $this;
102102
}
103103

104-
private function getNodeAlternatives(): Node\Expr
105-
{
106-
$output = [];
107-
foreach ($this->alternatives as $alternative) {
108-
$output[] = new Node\Expr\ArrayItem(
109-
110-
);
111-
}
112-
113-
return new Node\Expr\Array_(
114-
items: [
115-
116-
]
117-
);
118-
}
119-
120104
public function getNode(): Node
121105
{
106+
$alternatives = $this->alternatives;
107+
[$condition, $alternative] = array_shift($alternatives);
108+
122109
return new Node\Expr\New_(
123110
class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\ConditionalLoader'),
124111
args: [
125112
new Node\Arg(
126113
value: $this->connection->getNode()
127114
),
128115
new Node\Arg(
129-
$this->getNodeAlternatives()
116+
value: new Node\Expr\Closure(
117+
subNodes: [
118+
'params' => [
119+
new Node\Param(
120+
var: new Node\Expr\Variable('input')
121+
),
122+
new Node\Param(
123+
var: new Node\Expr\Variable('connection')
124+
),
125+
],
126+
'stmts' => [
127+
new Node\Stmt\If_(
128+
cond: $condition,
129+
subNodes: [
130+
'stmts' => [
131+
...$this->compileAlternative($alternative)
132+
],
133+
'elseifs' => array_map(
134+
fn (Node\Expr $condition, AlternativeLoader $lookup)
135+
=> new Node\Stmt\ElseIf_(
136+
cond: $condition,
137+
stmts: $this->compileAlternative($lookup),
138+
),
139+
array_column($alternatives, 0),
140+
array_column($alternatives, 1)
141+
)
142+
],
143+
)
144+
]
145+
]
146+
)
130147
),
131148
$this->beforeQueries ? new Node\Arg(
132149
value: $this->compileBeforeQueries()

0 commit comments

Comments
 (0)