22
33namespace Kiboko \Plugin \SQL \Builder ;
44
5+ use Kiboko \Component \SatelliteToolbox \Builder \IsolatedCodeBuilder ;
56use Kiboko \Component \SatelliteToolbox \Builder \IsolatedValueAppendingBuilder ;
7+ use Kiboko \Component \SatelliteToolbox \Builder \IsolatedValueTransformationBuilder ;
68use Kiboko \Contract \Configurator \StepBuilderInterface ;
79use PhpParser \Builder ;
810use 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}
0 commit comments