Skip to content

Commit

Permalink
Merge pull request #24 from php-etl/fix/connection-shared
Browse files Browse the repository at this point in the history
Fix/connection shared
  • Loading branch information
clemzarch authored May 23, 2023
2 parents 86aa4c4 + eaed17e commit 03a1347
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/Factory/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,25 @@ public function compile(array $config): SQL\Factory\Repository\Connection

$repository = new SQL\Factory\Repository\Connection($connection);

if (\array_key_exists('shared', $config) && true === $config['shared']) {
$repository->addFiles(new File('PDOPool.php', new InMemory(<<<PHP
<?php
namespace {$this->generatedNamespace};
final class PDOPool {
private static array \$connections = [];
public static function unique(string \$dsn, ?string \$username = null, ?string \$password = null, \$options = []): \\PDO {
return new \\PDO(\$dsn, \$username, \$password, \$options);
}
public static function shared(string \$dsn, ?string \$username = null, ?string \$password = null, \$options = []): \\PDO {
if (isset(self::\$connections[\$dsn])) {
return self::\$connections[\$dsn];
}
return self::\$connections[\$dsn] = self::unique(\$dsn, \$username, \$password, \$options);
$repository->addFiles(new File('PDOPool.php', new InMemory(<<<PHP
<?php
namespace {$this->generatedNamespace};
final class PDOPool {
private static array \$connections = [];
public static function unique(string \$dsn, ?string \$username = null, ?string \$password = null, \$options = []): \\PDO {
return new \\PDO(\$dsn, \$username, \$password, \$options);
}
public static function shared(string \$dsn, ?string \$username = null, ?string \$password = null, \$options = []): \\PDO {
if (isset(self::\$connections[\$dsn])) {
return self::\$connections[\$dsn];
}
return self::\$connections[\$dsn] = self::unique(\$dsn, \$username, \$password, \$options);
}
PHP
)));
}
}
PHP
)));

return $repository;
}
Expand Down

0 comments on commit 03a1347

Please sign in to comment.