Skip to content

Commit 9952437

Browse files
committed
Latest revisions
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 7c9f5a6 commit 9952437

20 files changed

Lines changed: 452 additions & 350 deletions

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[Makefile]
15+
indent_style = tab

src/ConfigProvider.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace PhpDb\Adapter\Pgsql;
66

77
use Laminas\ServiceManager\Factory\InvokableFactory;
8+
use PhpDb\Adapter\Adapter;
89
use PhpDb\Adapter\AdapterInterface;
910
use PhpDb\Adapter\Driver\ConnectionInterface;
1011
use PhpDb\Adapter\Driver\DriverInterface;
1112
use PhpDb\Adapter\Driver\PdoConnectionInterface;
1213
use PhpDb\Adapter\Driver\PdoDriverInterface;
14+
use PhpDb\Adapter\Driver\Pdo\Statement as PdoStatement;
1315
use PhpDb\Adapter\Platform\PlatformInterface;
1416
use PhpDb\Adapter\Pgsql\Pdo\Connection as PdoConnection;
1517
use PhpDb\Adapter\Pgsql\Pdo\Driver as PdoDriver;
@@ -25,16 +27,26 @@ public function __invoke(): array
2527
{
2628
return [
2729
'dependencies' => $this->getDependencies(),
28-
AdapterInterface::class => $this->getAdapters(),
30+
AdapterInterface::class => $this->getConfig(),
2931
];
3032
}
3133

32-
public function getAdapters(): array
34+
public function getConfig(): array
3335
{
3436
return [
37+
// Standard Single Adapter configuration
38+
'driver' => Driver::class,
39+
'connection' => [
40+
'host' => 'localhost',
41+
'port' => 5432,
42+
'username' => 'your_username',
43+
'password' => 'your_password',
44+
'database' => 'your_database',
45+
],
46+
// Named Adapter configurations
3547
'adapters' => [
3648
AdapterInterface::class => [
37-
'driver' => Driver::class,
49+
'driver' => Driver::class,
3850
'connection' => [
3951
'host' => 'localhost',
4052
'port' => 5432,
@@ -54,6 +66,7 @@ public function getDependencies(): array
5466
Container\AdapterAbstractServiceFactory::class,
5567
],
5668
'aliases' => [
69+
AdapterInterface::class => Adapter::class,
5770
DriverInterface::class => Driver::class,
5871
'pgsql' => Driver::class,
5972
'PgSQL' => Driver::class,
@@ -69,21 +82,21 @@ public function getDependencies(): array
6982
'PDO_Postgresql' => PdoDriver::class,
7083
'Pdo_Postgresql' => PdoDriver::class,
7184
'PDO_PostgreSQL' => PdoDriver::class,
72-
'pdo_PostgreSQL' => PdoDriver::class,
7385
ConnectionInterface::class => Connection::class,
7486
MetadataInterface::class => Metadata\Source::class,
7587
PdoConnectionInterface::class => PdoConnection::class,
7688
PlatformInterface::class => AdapterPlatform::class,
7789
],
7890
'factories' => [
79-
//AdapterInterface::class => Container\AdapterInterfaceFactory::class,
80-
AdapterPlatform::class => Container\PlatformInterfaceFactory::class,
81-
Connection::class => Container\ConnectionInterfaceFactory::class,
82-
Metadata\Source::class => Container\MetadataInterfaceFactory::class,
83-
Statement::class => Container\StatementInterfaceFactory::class,
84-
Driver::class => Container\DriverInterfaceFactory::class,
85-
PdoConnection::class => Container\PdoConnectionInterfaceFactory::class,
86-
PdoDriver::class => Container\PdoDriverInterfaceFactory::class,
91+
Adapter::class => Container\AdapterInterfaceFactory::class,
92+
AdapterPlatform::class => Container\PlatformInterfaceFactory::class,
93+
Connection::class => Container\ConnectionInterfaceFactory::class,
94+
Metadata\Source::class => Container\MetadataInterfaceFactory::class,
95+
Statement::class => Container\StatementInterfaceFactory::class,
96+
Driver::class => Container\DriverInterfaceFactory::class,
97+
PdoConnection::class => Container\PdoConnectionInterfaceFactory::class,
98+
PdoDriver::class => Container\PdoDriverInterfaceFactory::class,
99+
PdoStatement::class => Container\PdoStatemenFactory::class,
87100
// Provide the following if you wish to override the Profiler implementation
88101
//ProfilerInterface::class => YourCustomProfilerFactory::class,
89102
// Provide the following if you wish to override the ResultSet implementation

src/Connection.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpDb\Adapter\Driver\DriverInterface;
1313
use PhpDb\Adapter\Driver\ResultInterface;
1414
use PhpDb\Adapter\Exception;
15-
use PhpDb\ResultSet\ResultSetInterface;
1615

1716
use function array_filter;
1817
use function http_build_query;
@@ -36,10 +35,10 @@ class Connection extends AbstractConnection implements DriverAwareInterface
3635
{
3736
protected Driver $driver;
3837

39-
/** @var PgSqlConnection|null */
38+
/** @var ?PgSqlConnection */
4039
protected $resource;
4140

42-
/** @var null|int PostgreSQL connection type */
41+
/** PostgreSQL connection type */
4342
protected ?int $type = null;
4443

4544
public function __construct(PgSqlConnection|array $connectionInfo)
@@ -65,6 +64,7 @@ public function getResource(): ?PgSqlConnection
6564
return $this->resource;
6665
}
6766

67+
#[Override]
6868
public function setDriver(
6969
DriverInterface $driver
7070
): ConnectionInterface&DriverAwareInterface {
@@ -86,6 +86,10 @@ public function setType(?int $type): ConnectionInterface&DriverAwareInterface
8686
return $this;
8787
}
8888

89+
/**
90+
* {@inheritDoc}
91+
*/
92+
#[Override]
8993
public function getCurrentSchema(): string|false
9094
{
9195
if (! $this->isConnected()) {
@@ -105,6 +109,7 @@ public function getCurrentSchema(): string|false
105109
*
106110
* @throws Exception\RuntimeException On failure.
107111
*/
112+
#[Override]
108113
public function connect(): static
109114
{
110115
if ($this->resource instanceof PgSqlConnection) {
@@ -148,6 +153,7 @@ public function connect(): static
148153
/**
149154
* {@inheritDoc}
150155
*/
156+
#[Override]
151157
public function isConnected(): bool
152158
{
153159
return $this->resource instanceof PgSqlConnection;
@@ -156,16 +162,18 @@ public function isConnected(): bool
156162
/**
157163
* {@inheritDoc}
158164
*/
165+
#[Override]
159166
public function disconnect(): static
160167
{
161168
pg_close($this->resource);
162-
169+
$this->resource = null;
163170
return $this;
164171
}
165172

166173
/**
167174
* {@inheritDoc}
168175
*/
176+
#[Override]
169177
public function beginTransaction(): static
170178
{
171179
if ($this->inTransaction()) {
@@ -185,6 +193,7 @@ public function beginTransaction(): static
185193
/**
186194
* {@inheritDoc}
187195
*/
196+
#[Override]
188197
public function commit(): static
189198
{
190199
if (! $this->isConnected()) {
@@ -204,6 +213,7 @@ public function commit(): static
204213
/**
205214
* {@inheritDoc}
206215
*/
216+
#[Override]
207217
public function rollback(): static
208218
{
209219
if (! $this->isConnected()) {
@@ -224,8 +234,8 @@ public function rollback(): static
224234
* {@inheritDoc}
225235
*
226236
* @throws Exception\InvalidQueryException
227-
* @return resource|ResultSetInterface
228237
*/
238+
#[Override]
229239
public function execute($sql): ResultInterface
230240
{
231241
if (! $this->isConnected()) {
@@ -248,9 +258,8 @@ public function execute($sql): ResultInterface
248258

249259
/**
250260
* {@inheritDoc}
251-
*
252-
* @return string
253261
*/
262+
#[Override]
254263
public function getLastGeneratedValue($name = null): bool|int|string|null
255264
{
256265
if ($name === null) {
@@ -288,7 +297,7 @@ private function getConnectionString(): string
288297
}
289298
$conn[$name] = $value;
290299
}
291-
300+
// todo:
292301
return urldecode(
293302
http_build_query(
294303
array_filter($conn),

src/Container/AdapterAbstractServiceFactory.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828
class AdapterAbstractServiceFactory implements AbstractFactoryInterface
2929
{
30-
/** @var array */
31-
protected $config;
30+
protected ?array $config = null;
3231

3332
/**
3433
* Can we create an adapter by the requested name?
@@ -38,6 +37,7 @@ class AdapterAbstractServiceFactory implements AbstractFactoryInterface
3837
public function canCreate(ContainerInterface $container, $requestedName): bool
3938
{
4039
$config = $this->getConfig($container);
40+
4141
if ($config === []) {
4242
return false;
4343
}
@@ -66,7 +66,7 @@ public function __invoke(
6666
}
6767

6868
/** @var DriverInterface|PdoDriverInterface $driver */
69-
$driver = $container->build($driverClass, $this->config[$requestedName]);
69+
$driver = $container->build($driverClass, $this->config[$requestedName]);
7070
/** @var PlatformInterface&Pgsql\AdapterPlatform $platform */
7171
$platform = $container->build(PlatformInterface::class, ['driver' => $driver]);
7272
/** @var ResultSetInterface|null $resultSet */
@@ -80,31 +80,31 @@ public function __invoke(
8080

8181
return match(true) {
8282
$resultSet !== null && $profiler !== null => new Adapter(
83-
driver:$driver,
83+
driver: $driver,
8484
platform: $platform,
8585
queryResultSetPrototype: $resultSet,
8686
profiler: $profiler,
8787
),
8888
$resultSet !== null => new Adapter(
89-
driver:$driver,
89+
driver: $driver,
9090
platform: $platform,
9191
queryResultSetPrototype: $resultSet,
9292
),
9393
$profiler !== null => new Adapter(
94-
driver:$driver,
94+
driver: $driver,
9595
platform: $platform,
9696
profiler: $profiler,
9797
),
9898
default => new Adapter(
99-
driver:$driver,
99+
driver: $driver,
100100
platform: $platform,
101101
),
102102
};
103103
}
104104

105105
/**
106106
* Get db configuration, if any
107-
* todo: refactor to use PhpDb\ConfigProvider::ADAPTERS_CONFIG_KEY instead of hardcoding 'adapters'
107+
* todo: refactor to use PhpDb\ConfigProvider::NAMED_ADAPTER_KEY instead of hardcoding 'adapters'
108108
*/
109109
protected function getConfig(ContainerInterface $container): array
110110
{
@@ -117,25 +117,8 @@ protected function getConfig(ContainerInterface $container): array
117117
return $this->config;
118118
}
119119

120-
$config = $container->get('config');
121-
if (
122-
! isset($config[AdapterInterface::class])
123-
|| ! is_array($config[AdapterInterface::class])
124-
) {
125-
$this->config = [];
126-
return $this->config;
127-
}
128-
129-
$config = $config[AdapterInterface::class];
130-
if (
131-
! isset($config['adapters'])
132-
|| ! is_array($config['adapters'])
133-
) {
134-
$this->config = [];
135-
return $this->config;
136-
}
137-
138-
$this->config = $config['adapters'];
120+
$config = $container->get('config');
121+
$this->config = $config[AdapterInterface::class]['adapters'] ?? [];
139122
return $this->config;
140123
}
141124
}

0 commit comments

Comments
 (0)