Skip to content

Commit 111f57a

Browse files
committed
Integrated the lookup and ran cs-fixer
1 parent fe0fac5 commit 111f57a

File tree

13 files changed

+1938
-452
lines changed

13 files changed

+1938
-452
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"symfony/config": "^5.2",
1818
"symfony/expression-language": "^5.2",
1919
"php-etl/configurator-contracts": "^0.3.0",
20-
"php-etl/satellite-toolbox": "^0.1.0"
20+
"php-etl/satellite-toolbox": "^0.1.0",
21+
"php-etl/fast-map-plugin": "^0.4.0"
2122
},
2223
"autoload": {
2324
"psr-4": {

composer.lock

Lines changed: 1124 additions & 444 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/AlternativeLookup.php

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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 AlternativeLookup implements StepBuilderInterface
11+
{
12+
private ?Node\Expr $logger;
13+
private ?Node\Expr $rejection;
14+
private ?Node\Expr $state;
15+
private array $params;
16+
private ?Builder $merge;
17+
18+
public function __construct(private Node\Expr $query, private Node\Expr $dsn, private ?Node\Expr $username = null, private ?Node\Expr $password = null)
19+
{
20+
$this->logger = null;
21+
$this->rejection = null;
22+
$this->state = null;
23+
$this->params = [];
24+
$this->merge = null;
25+
}
26+
27+
public function withLogger(Node\Expr $logger): StepBuilderInterface
28+
{
29+
$this->logger = $logger;
30+
31+
return $this;
32+
}
33+
34+
public function withRejection(Node\Expr $rejection): StepBuilderInterface
35+
{
36+
$this->rejection = $rejection;
37+
38+
return $this;
39+
}
40+
41+
public function withState(Node\Expr $state): StepBuilderInterface
42+
{
43+
$this->state = $state;
44+
45+
return $this;
46+
}
47+
48+
public function withUsername(Node\Expr $username): StepBuilderInterface
49+
{
50+
$this->username = $username;
51+
52+
return $this;
53+
}
54+
55+
public function withPassword(Node\Expr $password): StepBuilderInterface
56+
{
57+
$this->password = $password;
58+
59+
return $this;
60+
}
61+
62+
public function addParam(int|string $key, Node\Expr $param): StepBuilderInterface
63+
{
64+
$this->params[$key] = $param;
65+
66+
return $this;
67+
}
68+
69+
public function withMerge(Builder $merge): self
70+
{
71+
$this->merge = $merge;
72+
73+
return $this;
74+
}
75+
76+
public function getNode(): Node
77+
{
78+
return (new IsolatedValueAppendingBuilder(
79+
new Node\Expr\Variable('input'),
80+
new Node\Expr\Variable('output'),
81+
array_filter([
82+
new Node\Stmt\TryCatch(
83+
stmts: [
84+
new Node\Stmt\Expression(
85+
expr: new Node\Expr\Assign(
86+
var: new Node\Expr\Variable('dbh'),
87+
expr: new Node\Expr\New_(
88+
class: new Node\Name\FullyQualified('PDO'),
89+
args: [
90+
new Node\Arg($this->dsn),
91+
$this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')),
92+
$this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null'))
93+
],
94+
),
95+
),
96+
),
97+
new Node\Stmt\Expression(
98+
expr: new Node\Expr\Assign(
99+
var: new Node\Expr\Variable('stmt'),
100+
expr: new Node\Expr\MethodCall(
101+
var: new Node\Expr\Variable('dbh'),
102+
name: new Node\Name('prepare'),
103+
args: [
104+
new Node\Arg($this->query)
105+
],
106+
),
107+
),
108+
),
109+
...$this->compileParams(),
110+
new Node\Stmt\Expression(
111+
expr: new Node\Expr\MethodCall(
112+
var: new Node\Expr\Variable('stmt'),
113+
name: new Node\Name('execute')
114+
),
115+
),
116+
new Node\Stmt\Expression(
117+
expr: new Node\Expr\Assign(
118+
var: new Node\Expr\Variable('lookup'),
119+
expr: new Node\Expr\MethodCall(
120+
var: new Node\Expr\Variable('stmt'),
121+
name: new Node\Name('fetchAll'),
122+
args: [
123+
new Node\Arg(
124+
new Node\Expr\ClassConstFetch(
125+
class: new Node\Name\FullyQualified('PDO'),
126+
name: new Node\Name('FETCH_NAMED')
127+
),
128+
),
129+
],
130+
),
131+
),
132+
),
133+
new Node\Stmt\Expression(
134+
expr: new Node\Expr\Assign(
135+
var: new Node\Expr\Variable('dbh'),
136+
expr: new Node\Expr\ConstFetch(
137+
name: new Node\Name('null')
138+
),
139+
),
140+
),
141+
],
142+
catches: [
143+
new Node\Stmt\Catch_(
144+
types: [
145+
new Node\Name('PDOException')
146+
],
147+
var: new Node\Expr\Variable('exception'),
148+
stmts: [
149+
new Node\Stmt\Expression(
150+
expr: new Node\Expr\MethodCall(
151+
var: new Node\Expr\PropertyFetch(
152+
var: new Node\Expr\Variable('this'),
153+
name: 'logger',
154+
),
155+
name: new Node\Identifier('critical'),
156+
args: [
157+
new Node\Arg(
158+
value: new Node\Expr\MethodCall(
159+
var: new Node\Expr\Variable('exception'),
160+
name: new Node\Identifier('getMessage'),
161+
),
162+
),
163+
new Node\Arg(
164+
value: new Node\Expr\Array_(
165+
items: [
166+
new Node\Expr\ArrayItem(
167+
value: new Node\Expr\Variable('exception'),
168+
key: new Node\Scalar\String_('exception'),
169+
),
170+
],
171+
attributes: [
172+
'kind' => Node\Expr\Array_::KIND_SHORT,
173+
],
174+
),
175+
),
176+
]
177+
),
178+
),
179+
],
180+
),
181+
],
182+
),
183+
$this->merge?->getNode(),
184+
new Node\Stmt\Return_(
185+
new Node\Expr\Variable('output')
186+
),
187+
])
188+
))->getNode();
189+
}
190+
191+
public function compileParams(): array
192+
{
193+
$output = [];
194+
195+
foreach ($this->params as $key => $param) {
196+
$output[] = new Node\Stmt\Expression(
197+
expr: new Node\Expr\MethodCall(
198+
var: new Node\Expr\Variable('stmt'),
199+
name: new Node\Name('bindParam'),
200+
args: [
201+
new Node\Arg(
202+
is_string($key) ? new Node\Scalar\String_($key) : new Node\Scalar\LNumber($key)
203+
),
204+
new Node\Arg(
205+
$param
206+
)
207+
],
208+
),
209+
);
210+
}
211+
212+
return $output;
213+
}
214+
}

src/Builder/ConditionalLookup.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Plugin\SQL\Builder;
4+
5+
use Kiboko\Contract\Configurator\StepBuilderInterface;
6+
use PhpParser\Node;
7+
8+
final class ConditionalLookup implements StepBuilderInterface
9+
{
10+
private ?Node\Expr $logger;
11+
private ?Node\Expr $rejection;
12+
private ?Node\Expr $state;
13+
private iterable $alternatives;
14+
15+
public function __construct()
16+
{
17+
$this->logger = null;
18+
$this->rejection = null;
19+
$this->state = null;
20+
$this->alternatives = [];
21+
}
22+
23+
public function withLogger(Node\Expr $logger): StepBuilderInterface
24+
{
25+
$this->logger = $logger;
26+
27+
return $this;
28+
}
29+
30+
public function withRejection(Node\Expr $rejection): StepBuilderInterface
31+
{
32+
$this->rejection = $rejection;
33+
34+
return $this;
35+
}
36+
37+
public function withState(Node\Expr $state): StepBuilderInterface
38+
{
39+
$this->state = $state;
40+
41+
return $this;
42+
}
43+
44+
public function addAlternative(Node\Expr $condition, AlternativeLookup $lookup): self
45+
{
46+
$this->alternatives[] = [$condition, $lookup];
47+
48+
return $this;
49+
}
50+
51+
private function compileAlternative(AlternativeLookup $lookup): array
52+
{
53+
return [
54+
$lookup->getNode(),
55+
];
56+
}
57+
58+
private function getNodeAlternatives(): Node
59+
{
60+
$alternatives = $this->alternatives;
61+
[$condition, $alternative] = array_shift($alternatives);
62+
63+
return new Node\Stmt\Do_(
64+
cond: new Node\Expr\Assign(
65+
var: new Node\Expr\Variable('input'),
66+
expr: new Node\Expr\Yield_(
67+
value: new Node\Expr\New_(
68+
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
69+
args: [
70+
new Node\Arg(
71+
value: new Node\Expr\Variable('input')
72+
)
73+
],
74+
),
75+
),
76+
),
77+
stmts: array_filter([
78+
new Node\Stmt\Expression(
79+
new Node\Expr\Assign(
80+
var: new Node\Expr\Variable('output'),
81+
expr:new Node\Expr\Variable('input'),
82+
),
83+
),
84+
new Node\Stmt\If_(
85+
cond: $condition,
86+
subNodes: [
87+
'stmts' => [
88+
...$this->compileAlternative($alternative),
89+
],
90+
'elseifs' => array_map(
91+
fn (Node\Expr $condition, AlternativeLookup $lookup)
92+
=> new Node\Stmt\ElseIf_(
93+
cond: $condition,
94+
stmts: $this->compileAlternative($lookup)
95+
),
96+
array_column($alternatives, 0),
97+
array_column($alternatives, 1)
98+
),
99+
'else' => new Node\Stmt\Else_(
100+
stmts: [
101+
new Node\Stmt\Expression(
102+
new Node\Expr\Yield_(
103+
new Node\Expr\Variable('input')
104+
),
105+
),
106+
],
107+
),
108+
],
109+
),
110+
]),
111+
);
112+
}
113+
114+
public function getNode(): Node
115+
{
116+
return new Node\Expr\New_(
117+
class: new Node\Stmt\Class_(
118+
name: null,
119+
subNodes: [
120+
'implements' => [
121+
new Node\Name\FullyQualified(name: 'Kiboko\\Contract\\Pipeline\\TransformerInterface'),
122+
],
123+
'stmts' => [
124+
new Node\Stmt\ClassMethod(
125+
name: new Node\Identifier(name: '__construct'),
126+
subNodes: [
127+
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
128+
'params' => [
129+
new Node\Param(
130+
var: new Node\Expr\Variable('logger'),
131+
type: new Node\Name\FullyQualified(name: 'Psr\\Log\\LoggerInterface'),
132+
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
133+
),
134+
],
135+
],
136+
),
137+
new Node\Stmt\ClassMethod(
138+
name: new Node\Identifier('transform'),
139+
subNodes: [
140+
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
141+
'returnType' => new Node\Name\FullyQualified(name: 'Generator'),
142+
'stmts' => [
143+
new Node\Stmt\Expression(
144+
expr: new Node\Expr\Assign(
145+
var: new Node\Expr\Variable('input'),
146+
expr: new Node\Expr\Yield_()
147+
)
148+
),
149+
$this->getNodeAlternatives(),
150+
],
151+
],
152+
),
153+
],
154+
],
155+
),
156+
args: [
157+
new Node\Arg(value: $this->logger ?? new Node\Expr\New_(new Node\Name\FullyQualified('Psr\\Log\\NullLogger'))),
158+
],
159+
);
160+
}
161+
}

0 commit comments

Comments
 (0)