Skip to content

Commit f6ef988

Browse files
committed
Fixed extractor and lookup, and added the ConditionalLoader
1 parent f0cf777 commit f6ef988

File tree

7 files changed

+457
-15
lines changed

7 files changed

+457
-15
lines changed

src/Builder/AlternativeLoader.php

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Plugin\SQL\Builder;
4+
5+
use Kiboko\Component\SatelliteToolbox\Builder\IsolatedValueAppendingBuilder;
6+
use Kiboko\Contract\Configurator\StepBuilderInterface;
7+
use PhpParser\Builder;
8+
use PhpParser\Node;
9+
10+
final class AlternativeLoader implements StepBuilderInterface
11+
{
12+
private ?Node\Expr $logger;
13+
private ?Node\Expr $rejection;
14+
private ?Node\Expr $state;
15+
/** @var array<Node\Expr> */
16+
private array $parameters;
17+
private ?Builder $merge;
18+
19+
public function __construct(private Node\Expr $query)
20+
{
21+
$this->logger = null;
22+
$this->rejection = null;
23+
$this->state = null;
24+
$this->parameters = [];
25+
$this->merge = null;
26+
}
27+
28+
public function withLogger(Node\Expr $logger): StepBuilderInterface
29+
{
30+
$this->logger = $logger;
31+
32+
return $this;
33+
}
34+
35+
public function withRejection(Node\Expr $rejection): StepBuilderInterface
36+
{
37+
$this->rejection = $rejection;
38+
39+
return $this;
40+
}
41+
42+
public function withState(Node\Expr $state): StepBuilderInterface
43+
{
44+
$this->state = $state;
45+
46+
return $this;
47+
}
48+
49+
public function addParam(int|string $key, Node\Expr $param): StepBuilderInterface
50+
{
51+
$this->parameters[$key] = $param;
52+
53+
return $this;
54+
}
55+
56+
public function withMerge(Builder $merge): self
57+
{
58+
$this->merge = $merge;
59+
60+
return $this;
61+
}
62+
63+
public function getNode(): Node
64+
{
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: [
91+
new Node\Stmt\Expression(
92+
expr: new Node\Expr\Assign(
93+
var: new Node\Expr\Variable('stmt'),
94+
expr: new Node\Expr\MethodCall(
95+
var: new Node\Expr\Variable('dbh'),
96+
name: new Node\Identifier('prepare'),
97+
args: [
98+
new Node\Arg($this->query)
99+
],
100+
),
101+
),
102+
),
103+
...$this->compileParameters(),
104+
new Node\Stmt\Expression(
105+
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')
137+
)
138+
],
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+
),
178+
],
179+
),
180+
],
181+
new Node\Expr\Variable('dbh'),
182+
))->getNode();
183+
}
184+
185+
/**
186+
* @return array<int, Node\Stmt\Expression>
187+
*/
188+
public function compileParameters(): array
189+
{
190+
$output = [];
191+
192+
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'),
197+
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+
),
206+
);
207+
}
208+
209+
return $output;
210+
}
211+
}

0 commit comments

Comments
 (0)