Skip to content

Commit fe0fac5

Browse files
committed
Integrated the loader, fixed the query built by the extractor and ran cs-fixer
1 parent fb91777 commit fe0fac5

File tree

4 files changed

+268
-22
lines changed

4 files changed

+268
-22
lines changed

src/Builder/Extractor.php

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ class: new Node\Stmt\Class_(
7777
],
7878
),
7979
new Node\Stmt\ClassMethod(
80-
name: new Node\Identifier(name: 'extract'),
81-
subNodes: [
80+
name: new Node\Identifier(name: 'extract'),
81+
subNodes: [
8282
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
8383
'params' => [],
8484
'returnType' => new Node\Name(name: 'iterable'),
8585
'stmts' => [
86-
new Node\Stmt\TryCatch(
87-
stmts: [
86+
new Node\Stmt\TryCatch(
87+
stmts: [
8888
new Node\Stmt\Expression(
8989
expr: new Node\Expr\Assign(
9090
var: new Node\Expr\Variable('dbh'),
@@ -104,17 +104,23 @@ class: new Node\Name\FullyQualified('PDO'),
104104
expr: new Node\Expr\Array_(
105105
attributes: [
106106
'kind' => Node\Expr\Array_::KIND_SHORT
107-
]
108-
)
109-
)
107+
],
108+
),
109+
),
110110
),
111111
new Node\Stmt\Foreach_(
112112
expr: new Node\Expr\MethodCall(
113113
var: new Node\Expr\Variable('dbh'),
114114
name: new Node\Name('query'),
115115
args: [
116-
new Node\Arg($this->query)
117-
]
116+
new Node\Arg($this->query),
117+
new Node\Arg(
118+
new Node\Expr\ClassConstFetch(
119+
class: new Node\Name\FullyQualified('PDO'),
120+
name: new Node\Name('FETCH_NAMED')
121+
),
122+
),
123+
],
118124
),
119125
valueVar: new Node\Expr\Variable('row'),
120126
subNodes: [
@@ -129,17 +135,35 @@ class: new Node\Name\FullyQualified('PDO'),
129135
new Node\Arg(
130136
new Node\Expr\Variable('row')
131137
),
132-
]
133-
)
134-
)
135-
]
136-
]
138+
],
139+
),
140+
),
141+
],
142+
],
143+
),
144+
new Node\Stmt\Expression(
145+
expr: new Node\Expr\Assign(
146+
var: new Node\Expr\Variable('dbh'),
147+
expr: new Node\Expr\ConstFetch(
148+
name: new Node\Name('null')
149+
)
150+
)
151+
),
152+
new Node\Stmt\Expression(
153+
new Node\Expr\Yield_(
154+
value: new Node\Expr\New_(
155+
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
156+
args: [
157+
new Node\Arg(
158+
value: new Node\Expr\Variable('output'),
159+
unpack: true
160+
),
161+
],
162+
)
163+
),
137164
),
138-
new Node\Stmt\Return_(
139-
expr: new Node\Expr\Variable('output')
140-
)
141165
],
142-
catches: [
166+
catches: [
143167
new Node\Stmt\Catch_(
144168
types: [
145169
new Node\Name('PDOException')
@@ -179,7 +203,7 @@ class: new Node\Name\FullyQualified('PDO'),
179203
],
180204
),
181205
],
182-
)
206+
)
183207
]
184208
]
185209
),

src/Builder/Loader.php

Lines changed: 192 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ final class Loader implements StepBuilderInterface
1010
private ?Node\Expr $logger;
1111
private ?Node\Expr $rejection;
1212
private ?Node\Expr $state;
13+
private array $params;
1314

14-
public function __construct()
15+
public function __construct(private Node\Expr $query, private Node\Expr $dsn, private ?Node\Expr $username = null, private ?Node\Expr $password = null)
1516
{
1617
$this->logger = null;
1718
$this->rejection = null;
1819
$this->state = null;
20+
$this->params = [];
1921
}
2022

2123
public function withLogger(Node\Expr $logger): StepBuilderInterface
@@ -39,8 +41,196 @@ public function withState(Node\Expr $state): StepBuilderInterface
3941
return $this;
4042
}
4143

44+
public function withUsername(Node\Expr $username): StepBuilderInterface
45+
{
46+
$this->username = $username;
47+
48+
return $this;
49+
}
50+
51+
public function withPassword(Node\Expr $password): StepBuilderInterface
52+
{
53+
$this->password = $password;
54+
55+
return $this;
56+
}
57+
58+
public function addParam(int|string $key, Node\Expr $param): StepBuilderInterface
59+
{
60+
$this->params[$key] = $param;
61+
62+
return $this;
63+
}
64+
4265
public function getNode(): Node
4366
{
44-
// TODO: Implement getNode() method.
67+
return new Node\Expr\New_(
68+
class: new Node\Stmt\Class_(
69+
name: null,
70+
subNodes: [
71+
'implements' => [
72+
new Node\Name\FullyQualified(name: 'Kiboko\\Contract\\Pipeline\\LoaderInterface'),
73+
],
74+
'stmts' => [
75+
new Node\Stmt\ClassMethod(
76+
name: new Node\Identifier(name: '__construct'),
77+
subNodes: [
78+
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
79+
'params' => [
80+
new Node\Param(
81+
var: new Node\Expr\Variable('logger'),
82+
type: new Node\Name\FullyQualified(name: 'Psr\\Log\\LoggerInterface'),
83+
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
84+
),
85+
],
86+
],
87+
),
88+
new Node\Stmt\ClassMethod(
89+
name: new Node\Identifier('load'),
90+
subNodes: [
91+
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
92+
'returnType' => new Node\Name\FullyQualified(name: 'Generator'),
93+
'stmts' => [
94+
new Node\Stmt\Expression(
95+
expr: new Node\Expr\Assign(
96+
var: new Node\Expr\Variable('input'),
97+
expr: new Node\Expr\Yield_()
98+
)
99+
),
100+
new Node\Stmt\TryCatch(
101+
stmts: [
102+
new Node\Stmt\Expression(
103+
expr: new Node\Expr\Assign(
104+
var: new Node\Expr\Variable('dbh'),
105+
expr: new Node\Expr\New_(
106+
class: new Node\Name\FullyQualified('PDO'),
107+
args: [
108+
new Node\Arg($this->dsn),
109+
$this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')),
110+
$this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null'))
111+
],
112+
),
113+
),
114+
),
115+
new Node\Stmt\Do_(
116+
cond: new Node\Expr\Assign(
117+
var: new Node\Expr\Variable(name: 'input'),
118+
expr: new Node\Expr\Yield_(
119+
value: new Node\Expr\New_(
120+
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
121+
args: [
122+
new Node\Arg(
123+
new Node\Expr\Variable('input')
124+
),
125+
],
126+
),
127+
),
128+
),
129+
stmts: [
130+
new Node\Stmt\Expression(
131+
expr: new Node\Expr\Assign(
132+
var: new Node\Expr\Variable('stmt'),
133+
expr: new Node\Expr\MethodCall(
134+
var: new Node\Expr\Variable('dbh'),
135+
name: new Node\Name('prepare'),
136+
args: [
137+
new Node\Arg($this->query)
138+
],
139+
),
140+
),
141+
),
142+
...$this->compileParams(),
143+
new Node\Stmt\Expression(
144+
expr: new Node\Expr\MethodCall(
145+
var: new Node\Expr\Variable('stmt'),
146+
name: new Node\Name('execute')
147+
),
148+
),
149+
],
150+
),
151+
new Node\Stmt\Expression(
152+
expr: new Node\Expr\Assign(
153+
var: new Node\Expr\Variable('dbh'),
154+
expr: new Node\Expr\ConstFetch(
155+
name: new Node\Name('null')
156+
),
157+
),
158+
),
159+
],
160+
catches: [
161+
new Node\Stmt\Catch_(
162+
types: [
163+
new Node\Name('PDOException')
164+
],
165+
var: new Node\Expr\Variable('exception'),
166+
stmts: [
167+
new Node\Stmt\Expression(
168+
expr: new Node\Expr\MethodCall(
169+
var: new Node\Expr\PropertyFetch(
170+
var: new Node\Expr\Variable('this'),
171+
name: 'logger',
172+
),
173+
name: new Node\Identifier('critical'),
174+
args: [
175+
new Node\Arg(
176+
value: new Node\Expr\MethodCall(
177+
var: new Node\Expr\Variable('exception'),
178+
name: new Node\Identifier('getMessage'),
179+
),
180+
),
181+
new Node\Arg(
182+
value: new Node\Expr\Array_(
183+
items: [
184+
new Node\Expr\ArrayItem(
185+
value: new Node\Expr\Variable('exception'),
186+
key: new Node\Scalar\String_('exception'),
187+
),
188+
],
189+
attributes: [
190+
'kind' => Node\Expr\Array_::KIND_SHORT,
191+
],
192+
),
193+
),
194+
]
195+
),
196+
),
197+
],
198+
),
199+
],
200+
),
201+
],
202+
],
203+
),
204+
],
205+
],
206+
),
207+
args: [
208+
new Node\Arg(value: $this->logger ?? new Node\Expr\New_(new Node\Name\FullyQualified('Psr\\Log\\NullLogger'))),
209+
],
210+
);
211+
}
212+
213+
public function compileParams(): array
214+
{
215+
$output = [];
216+
217+
foreach ($this->params as $key => $param) {
218+
$output[] = new Node\Stmt\Expression(
219+
expr: new Node\Expr\MethodCall(
220+
var: new Node\Expr\Variable('stmt'),
221+
name: new Node\Name('bindParam'),
222+
args: [
223+
new Node\Arg(
224+
is_string($key) ? new Node\Scalar\String_($key) : new Node\Scalar\LNumber($key)
225+
),
226+
new Node\Arg(
227+
$param
228+
)
229+
],
230+
),
231+
);
232+
}
233+
234+
return $output;
45235
}
46236
}

src/Configuration/Loader.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ public function getConfigTreeBuilder()
1616
$builder->getRootNode()
1717
->children()
1818
->scalarNode('query')->end()
19+
->arrayNode('params')
20+
->variablePrototype()
21+
->validate()
22+
->ifArray()
23+
->thenInvalid('A parameter cann\'t be an array.')
24+
->end()
25+
->validate()
26+
->ifTrue(isExpression())
27+
->then(asExpression())
28+
->end()
29+
->end()
30+
->end()
1931
->arrayNode('connection')
2032
->children()
2133
->scalarNode('dsn')

0 commit comments

Comments
 (0)