Skip to content

Commit bb74a2a

Browse files
committed
Updated php-etl/configurator-contracts to version 0.4 in order to help automatic plugin loading in php-etl/satellite
Fixed extractor and loader configuration issues
1 parent f6a1304 commit bb74a2a

File tree

12 files changed

+465
-342
lines changed

12 files changed

+465
-342
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"nikic/php-parser": "^4.10",
1717
"symfony/config": "^5.2",
1818
"symfony/expression-language": "^5.2",
19-
"php-etl/configurator-contracts": "^0.3.0",
20-
"php-etl/satellite-toolbox": "^0.1.0",
21-
"php-etl/fast-map-plugin": "^0.4.0"
19+
"php-etl/configurator-contracts": "^0.4.0",
20+
"php-etl/satellite-toolbox": "^0.2.0@dev",
21+
"php-etl/fast-map-plugin": "dev-feature/plugins-loading"
2222
},
2323
"require-dev": {
2424
"phpunit/phpunit": "^9.0",

composer.lock

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

src/Builder/Extractor.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ public function addParameter(string|int $key, Node\Expr $parameter): StepBuilder
9494
public function getNode(): Node
9595
{
9696
return new Node\Expr\New_(
97-
class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Extractor'),
97+
class: new Node\Name\FullyQualified('Kibok\\Component\\Flow\\SQL\\Extractor'),
9898
args: [
9999
new Node\Arg(
100100
value: $this->connection->getNode()
101101
),
102102
new Node\Arg(
103103
value: $this->query
104104
),
105-
$this->parameters ? new Node\Arg(
106-
value: new Node\Expr\Closure(
105+
count($this->parameters) > 0
106+
? new Node\Arg(value: new Node\Expr\Closure(
107107
subNodes: [
108108
'params' => [
109109
new Node\Param(
@@ -115,22 +115,18 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Extractor'),
115115
...$this->compileParameters()
116116
],
117117
],
118-
),
119-
) : new Node\Expr\ConstFetch(new Node\Name('null')),
120-
$this->beforeQueries ? new Node\Arg(
121-
value: $this->compileBeforeQueries()
122-
) : new Node\Expr\Array_(
123-
attributes: [
118+
))
119+
: new Node\Expr\ConstFetch(new Node\Name('null')),
120+
count($this->beforeQueries) > 0
121+
? new Node\Arg(value: $this->compileBeforeQueries())
122+
: new Node\Expr\Array_(attributes: [
124123
'kind' => Node\Expr\Array_::KIND_SHORT
125-
],
126-
),
127-
$this->afterQueries ? new Node\Arg(
128-
value: $this->compileAfterQueries()
129-
): new Node\Expr\Array_(
130-
attributes: [
124+
]),
125+
count($this->afterQueries) > 0
126+
? new Node\Arg(value: $this->compileAfterQueries())
127+
: new Node\Expr\Array_(attributes: [
131128
'kind' => Node\Expr\Array_::KIND_SHORT
132-
],
133-
),
129+
]),
134130
],
135131
);
136132
}
@@ -183,7 +179,6 @@ public function compileBeforeQueries(): Node\Expr
183179
);
184180
}
185181

186-
187182
public function compileAfterQueries(): Node\Expr
188183
{
189184
$output = [];

src/Builder/Inline.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
final class Inline implements Builder
1313
{
14-
public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper)
15-
{
16-
}
14+
public function __construct(
15+
private ArrayBuilderInterface|ObjectBuilderInterface $mapper
16+
) {}
1717

1818
public function getNode(): Node
1919
{

src/Builder/Loader.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ public function addParameter(string|int $key, Node\Expr $parameter): StepBuilder
9494
public function getNode(): Node
9595
{
9696
return new Node\Expr\New_(
97-
class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Loader'),
97+
class: new Node\Name\FullyQualified('Kiboko\\Component\\Flow\\SQL\\Loader'),
9898
args: [
9999
new Node\Arg(
100100
value: $this->connection->getNode()
101101
),
102102
new Node\Arg(
103103
value: $this->query
104104
),
105-
$this->parameters ? new Node\Arg(
106-
value: new Node\Expr\Closure(
105+
count($this->parameters) > 0
106+
? new Node\Arg(value: new Node\Expr\Closure(
107107
subNodes: [
108108
'params' => [
109109
new Node\Param(
@@ -116,24 +116,20 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Loader'),
116116
],
117117
'stmts' => [
118118
...$this->compileParameters()
119-
]
119+
],
120120
],
121-
),
122-
) : new Node\Expr\ConstFetch(new Node\Name('null')),
123-
$this->beforeQueries ? new Node\Arg(
124-
value: $this->compileBeforeQueries()
125-
) : new Node\Expr\Array_(
126-
attributes: [
121+
))
122+
: new Node\Expr\ConstFetch(new Node\Name('null')),
123+
count($this->beforeQueries) > 0
124+
? new Node\Arg(value: $this->compileBeforeQueries())
125+
: new Node\Expr\Array_(attributes: [
127126
'kind' => Node\Expr\Array_::KIND_SHORT
128-
]
129-
),
130-
$this->afterQueries ? new Node\Arg(
131-
value: $this->compileAfterQueries()
132-
): new Node\Expr\Array_(
133-
attributes: [
127+
]),
128+
count($this->afterQueries) > 0
129+
? new Node\Arg(value: $this->compileAfterQueries())
130+
: new Node\Expr\Array_(attributes: [
134131
'kind' => Node\Expr\Array_::KIND_SHORT
135-
]
136-
)
132+
]),
137133
],
138134
);
139135
}
@@ -146,18 +142,18 @@ public function compileParameters(): iterable
146142
var: new Node\Expr\Variable('statement'),
147143
name: new Node\Identifier('bindParam'),
148144
args: [
149-
new Node\Arg(
150-
is_string($key) ? new Node\Scalar\Encapsed(
151-
[
152-
new Node\Scalar\EncapsedStringPart(':'),
153-
new Node\Scalar\EncapsedStringPart($key)
154-
]
155-
) : new Node\Scalar\LNumber($key)
156-
),
157-
new Node\Arg(
158-
$parameter
159-
),
160-
],
145+
new Node\Arg(
146+
is_string($key) ? new Node\Scalar\Encapsed(
147+
[
148+
new Node\Scalar\EncapsedStringPart(':'),
149+
new Node\Scalar\EncapsedStringPart($key)
150+
]
151+
) : new Node\Scalar\LNumber($key)
152+
),
153+
new Node\Arg(
154+
$parameter
155+
),
156+
],
161157
)
162158
);
163159
}

src/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Kiboko\Plugin\SQL;
44

5+
use Kiboko\Contract\Configurator\PluginConfigurationInterface;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6-
use Symfony\Component\Config\Definition\ConfigurationInterface;
77
use function Kiboko\Component\SatelliteToolbox\Configuration\asExpression;
88
use function Kiboko\Component\SatelliteToolbox\Configuration\isExpression;
99

10-
final class Configuration implements ConfigurationInterface
10+
final class Configuration implements PluginConfigurationInterface
1111
{
1212
public function getConfigTreeBuilder()
1313
{

src/Configuration/Parameters.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,13 @@ public function getConfigTreeBuilder()
2323
})
2424
->thenUnset()
2525
->end()
26-
->arrayPrototype()
27-
->children()
28-
->variableNode('key')
29-
->isRequired()
30-
->cannotBeEmpty()
31-
->validate()
32-
->ifTrue(fn ($data) => !is_string($data) && !is_int($data))
33-
->thenInvalid('The key of your parameter must be a string or an integer.')
34-
->end()
35-
->validate()
36-
->ifTrue(fn ($data) => is_int($data) && $data < 1)
37-
->thenInvalid('The key of your parameter must be greater or equal to 1.')
38-
->end()
39-
->validate()
40-
->ifTrue(isExpression())
41-
->then(asExpression())
42-
->end()
43-
->end()
44-
->variableNode('value')
45-
->isRequired()
46-
->cannotBeEmpty()
47-
->validate()
48-
->ifTrue(isExpression())
49-
->then(asExpression())
50-
->end()
51-
->end()
26+
->useAttributeAsKey('key')
27+
->variablePrototype()
28+
->isRequired()
29+
->cannotBeEmpty()
30+
->validate()
31+
->ifTrue(isExpression())
32+
->then(asExpression())
5233
->end()
5334
->end();
5435

src/Factory/Extractor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function validate(array $config): bool
4343
$this->processor->processConfiguration($this->configuration, $config);
4444

4545
return true;
46-
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException) {
46+
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) {
4747
return false;
4848
}
4949
}
@@ -55,8 +55,8 @@ public function compile(array $config): SQL\Factory\Repository\Extractor
5555
);
5656

5757
if ($config['parameters'] != null) {
58-
foreach ($config["parameters"] as $parameter) {
59-
$extractor->addParameter($parameter['key'], compileValue($this->interpreter, $parameter['value']));
58+
foreach ($config["parameters"] as $key => $value) {
59+
$extractor->addParameter($key, compileValue($this->interpreter, $value));
6060
}
6161
}
6262

src/Service.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
namespace Kiboko\Plugin\SQL;
44

5-
use Kiboko\Component\SatelliteToolbox\Builder\IsolatedCodeBuilder;
6-
use Kiboko\Contract\Configurator\FactoryInterface;
7-
use Kiboko\Contract\Configurator\InvalidConfigurationException;
8-
use Kiboko\Contract\Configurator\RepositoryInterface;
5+
use Kiboko\Contract\Configurator;
96
use Kiboko\Plugin\SQL\Factory\Connection;
10-
use Kiboko\Plugin\SQL\Factory\InitializerQueries;
11-
use PhpParser\Builder;
127
use Symfony\Component\Config\Definition\ConfigurationInterface;
138
use Symfony\Component\Config\Definition\Processor;
149
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1510
use Symfony\Component\Config\Definition\Exception as Symfony;
1611

17-
final class Service implements FactoryInterface
12+
#[Configurator\Pipeline(
13+
name: "sql",
14+
dependencies: [
15+
'ext-pdo',
16+
],
17+
steps: [
18+
"extractor" => "extractor",
19+
"lookup" => "transformer",
20+
"loader" => "loader",
21+
],
22+
)]
23+
final class Service implements Configurator\FactoryInterface
1824
{
1925
private Processor $processor;
2026
private ConfigurationInterface $configuration;
@@ -37,7 +43,7 @@ public function normalize(array $config): array
3743
try {
3844
return $this->processor->processConfiguration($this->configuration, $config);
3945
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) {
40-
throw new InvalidConfigurationException($exception->getMessage(), 0, $exception);
46+
throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception);
4147
}
4248
}
4349

@@ -52,7 +58,7 @@ public function validate(array $config): bool
5258
}
5359
}
5460

55-
public function compile(array $config): RepositoryInterface
61+
public function compile(array $config): Factory\Repository\Extractor|Factory\Repository\Lookup|Factory\Repository\Loader
5662
{
5763
if (array_key_exists('expression_language', $config)
5864
&& is_array($config['expression_language'])
@@ -65,38 +71,34 @@ public function compile(array $config): RepositoryInterface
6571

6672
$connection = (new Connection($this->interpreter))->compile($config['connection']);
6773

68-
try {
69-
if (array_key_exists('extractor', $config)) {
70-
$extractorFactory = new Factory\Extractor($this->interpreter);
74+
if (array_key_exists('extractor', $config)) {
75+
$extractorFactory = new Factory\Extractor($this->interpreter);
7176

72-
return $extractorFactory
73-
->compile($config['extractor'])
74-
->withConnection($connection)
75-
->withBeforeQueries(...($config['before']['queries'] ?? []))
76-
->withAfterQueries(...($config['after']['queries'] ?? []));
77-
} elseif (array_key_exists('lookup', $config)) {
78-
$lookupFactory = new Factory\Lookup($this->interpreter);
77+
return $extractorFactory
78+
->compile($config['extractor'])
79+
->withConnection($connection)
80+
->withBeforeQueries(...($config['before']['queries'] ?? []))
81+
->withAfterQueries(...($config['after']['queries'] ?? []));
82+
} elseif (array_key_exists('lookup', $config)) {
83+
$lookupFactory = new Factory\Lookup($this->interpreter);
7984

80-
return $lookupFactory
81-
->compile($config['lookup'])
82-
->withConnection($connection)
83-
->withBeforeQueries(...($config['before']['queries'] ?? []))
84-
->withAfterQueries(...($config['after']['queries'] ?? []));
85-
} elseif (array_key_exists('loader', $config)) {
86-
$loaderFactory = new Factory\Loader($this->interpreter);
85+
return $lookupFactory
86+
->compile($config['lookup'])
87+
->withConnection($connection)
88+
->withBeforeQueries(...($config['before']['queries'] ?? []))
89+
->withAfterQueries(...($config['after']['queries'] ?? []));
90+
} elseif (array_key_exists('loader', $config)) {
91+
$loaderFactory = new Factory\Loader($this->interpreter);
8792

88-
return $loaderFactory
89-
->compile($config['loader'])
90-
->withConnection($connection)
91-
->withBeforeQueries(...($config['before']['queries'] ?? []))
92-
->withAfterQueries(...($config['after']['queries'] ?? []));
93-
} else {
94-
throw new InvalidConfigurationException(
95-
'Could not determine if the factory should build an extractor, a lookup or a loader.'
96-
);
97-
}
98-
} catch (InvalidConfigurationException $exception) {
99-
throw new InvalidConfigurationException($exception->getMessage(), 0, $exception);
93+
return $loaderFactory
94+
->compile($config['loader'])
95+
->withConnection($connection)
96+
->withBeforeQueries(...($config['before']['queries'] ?? []))
97+
->withAfterQueries(...($config['after']['queries'] ?? []));
98+
} else {
99+
throw new Configurator\InvalidConfigurationException(
100+
'Could not determine if the factory should build an extractor, a lookup or a loader.'
101+
);
100102
}
101103
}
102104
}

0 commit comments

Comments
 (0)