Skip to content

Commit 01fbaa1

Browse files
committed
Made the namespace management configurable for the PDOPool class
1 parent 3720a50 commit 01fbaa1

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

src/Builder/Connection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ final class Connection implements ConnectionBuilderInterface
1313
public function __construct(
1414
private Node\Expr $dsn,
1515
private ?Node\Expr $username = null,
16-
private ?Node\Expr $password = null
16+
private ?Node\Expr $password = null,
17+
private readonly string $generatedNamespace = 'GyroscopsGenerated',
1718
) {
1819
$this->persistentConnection = null;
1920
}
@@ -42,10 +43,10 @@ public function withPersistentConnection(bool $option): self
4243
public function getNode(): Node\Expr
4344
{
4445
return new Node\Expr\StaticCall(
45-
class: new Node\Name('GyroscopsGenerated\\PDOPool'),
46+
class: new Node\Name($this->generatedNamespace.'\\PDOPool'),
4647
name: new Node\Name('unique'),
4748
args: [
48-
new Node\Arg($this->dsn),
49+
new Node\Arg(value: $this->dsn),
4950
$this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')),
5051
$this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null')),
5152
new Node\Arg(

src/Builder/SharedConnection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ final class SharedConnection implements ConnectionBuilderInterface
1313
public function __construct(
1414
private Node\Expr $dsn,
1515
private ?Node\Expr $username = null,
16-
private ?Node\Expr $password = null
16+
private ?Node\Expr $password = null,
17+
private readonly string $generatedNamespace = 'GyroscopsGenerated',
1718
) {
1819
$this->persistentConnection = null;
1920
}
@@ -42,7 +43,7 @@ public function withPersistentConnection(bool $option): self
4243
public function getNode(): Node\Expr
4344
{
4445
return new Node\Expr\StaticCall(
45-
class: new Node\Name('GyroscopsGenerated\\PDOPool'),
46+
class: new Node\Name($this->generatedNamespace.'\\PDOPool'),
4647
name: new Node\Name('shared'),
4748
args: [
4849
new Node\Arg(value: $this->dsn),

src/Factory/Connection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ final class Connection implements FactoryInterface
2121
private Processor $processor;
2222
private ConfigurationInterface $configuration;
2323

24-
public function __construct(private ExpressionLanguage $interpreter)
25-
{
24+
public function __construct(
25+
private ExpressionLanguage $interpreter,
26+
private readonly string $generatedNamespace = 'GyroscopsGenerated',
27+
) {
2628
$this->processor = new Processor();
2729
$this->configuration = new SQL\Configuration\Extractor();
2830
}
@@ -56,11 +58,13 @@ public function compile(array $config): SQL\Factory\Repository\Connection
5658
{
5759
if (\array_key_exists('shared', $config) && true === $config['shared']) {
5860
$connection = new SQL\Builder\SharedConnection(
59-
compileValueWhenExpression($this->interpreter, $config['dsn'])
61+
compileValueWhenExpression($this->interpreter, $config['dsn']),
62+
generatedNamespace: $this->generatedNamespace,
6063
);
6164
} else {
6265
$connection = new SQL\Builder\Connection(
63-
compileValueWhenExpression($this->interpreter, $config['dsn'])
66+
compileValueWhenExpression($this->interpreter, $config['dsn']),
67+
generatedNamespace: $this->generatedNamespace,
6468
);
6569
}
6670

@@ -84,7 +88,7 @@ public function compile(array $config): SQL\Factory\Repository\Connection
8488
$repository->addFiles(new File('PDOPool.php', new InMemory(<<<'PHP'
8589
<?php
8690
87-
namespace GyroscopsGenerated;
91+
namespace ${$this->generatedNamespace};
8892
final class PDOPool {
8993
private static array $connections = [];
9094
public static function unique(string $dsn, ?string $username = null, ?string $password = null, $options = []): \PDO {

src/Service.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ final class Service implements Configurator\PipelinePluginInterface
2727
private Configurator\PluginConfigurationInterface $configuration;
2828
private ExpressionLanguage $interpreter;
2929

30-
public function __construct(?ExpressionLanguage $interpreter = null)
31-
{
30+
public function __construct(
31+
?ExpressionLanguage $interpreter = null,
32+
private readonly string $generatedNamespace = 'GyroscopsGenerated',
33+
) {
3234
$this->processor = new Processor();
3335
$this->configuration = new Configuration();
3436
$this->interpreter = $interpreter ?? new ExpressionLanguage();
@@ -77,7 +79,7 @@ public function compile(array $config): Factory\Repository\Extractor|Factory\Rep
7779
}
7880
}
7981

80-
$connection = (new Connection($interpreter))->compile($config['connection']);
82+
$connection = (new Connection($interpreter, $this->generatedNamespace))->compile($config['connection']);
8183

8284
if (\array_key_exists('extractor', $config)) {
8385
$extractorFactory = new Factory\Extractor($interpreter);

tests/functional/PDOPool.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace functional\Kiboko\Plugin\SQL;
4+
5+
final class PDOPool {
6+
private static array $connections = [];
7+
public static function unique(string $dsn, ?string $username = null, ?string $password = null, $options = []): \PDO {
8+
return new \PDO($dsn, $username, $password, $options);
9+
}
10+
public static function shared(string $dsn, ?string $username = null, ?string $password = null, $options = []): \PDO {
11+
if (isset(self::$connections[$dsn])) {
12+
return self::$connections[$dsn];
13+
}
14+
15+
return self::$connections[$dsn] = self::unique($dsn, $username, $password, $options);
16+
}
17+
}

tests/functional/ServiceTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public function testValidatingConditionalLoaderConfiguration(): void
481481

482482
public function testExtractor(): void
483483
{
484-
$service = new Service();
484+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
485485

486486
$this->assertBuildsExtractorExtractsExactly(
487487
[
@@ -520,7 +520,7 @@ public function testExtractor(): void
520520

521521
public function testLookup(): void
522522
{
523-
$service = new Service();
523+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
524524

525525
$this->assertBuildsTransformerTransformsExactly(
526526
[
@@ -587,7 +587,7 @@ public function testLookup(): void
587587

588588
public function testConditionalLookup(): void
589589
{
590-
$service = new Service();
590+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
591591

592592
$this->assertBuildsTransformerTransformsExactly(
593593
[
@@ -653,7 +653,7 @@ public function testConditionalLookup(): void
653653

654654
public function testLoader(): void
655655
{
656-
$service = new Service();
656+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
657657

658658
$this->assertBuildsLoaderLoadsExactly(
659659
[
@@ -724,7 +724,7 @@ public function testLoader(): void
724724

725725
public function testLoaderWithAfterQueries(): void
726726
{
727-
$service = new Service();
727+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
728728

729729
$this->assertBuildsLoaderLoadsExactly(
730730
[
@@ -791,7 +791,7 @@ public function testLoaderWithAfterQueries(): void
791791

792792
public function testLoaderWithTypedParam(): void
793793
{
794-
$service = new Service();
794+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
795795

796796
$this->assertBuildsLoaderLoadsExactly(
797797
[
@@ -859,7 +859,7 @@ public function testLoaderWithTypedParam(): void
859859

860860
public function testConditionalLoader(): void
861861
{
862-
$service = new Service();
862+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
863863

864864
$this->assertBuildsLoaderLoadsExactly(
865865
[
@@ -955,7 +955,7 @@ public function testConditionalLoader(): void
955955

956956
public function testConditionalLoaderWithAfterQueries(): void
957957
{
958-
$service = new Service();
958+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
959959

960960
$this->assertBuildsLoaderLoadsExactly(
961961
[
@@ -1049,7 +1049,7 @@ private function checkData(string $query): array
10491049

10501050
public function testExtractorConfigurationWithPersistentConnection(): void
10511051
{
1052-
$service = new Service();
1052+
$service = new Service(generatedNamespace: 'functional\Kiboko\Plugin\SQL');
10531053

10541054
$this->assertEquals(
10551055
[

0 commit comments

Comments
 (0)