Skip to content

Commit 8b7011e

Browse files
committed
latest revisions. Pdo named arguments are broken
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent bc056e0 commit 8b7011e

23 files changed

+280
-716
lines changed

composer.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "laminas/laminas-db-mysql-adapter",
2+
"name": "laminas/laminas-db-adapter-mysql",
33
"description": "MySQL support for laminas-db",
44
"license": "BSD-3-Clause",
55
"keywords": [
66
"laminas",
77
"mysql",
88
"db"
99
],
10-
"homepage": "https://github.com/axleus/laminas-db-mysql/discussions",
10+
"homepage": "https://github.com/axleus/laminas-db-adapter-mysql/discussions",
1111
"support": {
12-
"issues": "https://github.com/alxeus/laminas-db-mysql/issues",
13-
"source": "https://github.com/axleus/laminas-db-mysql",
14-
"forum": "https://github.com/axleus/laminas-db-mysql/discussions"
12+
"issues": "https://github.com/alxeus/laminas-db-adapter-mysql/issues",
13+
"source": "https://github.com/axleus/laminas-db-adapter-mysql",
14+
"forum": "https://github.com/axleus/laminas-db-adapter-mysql/discussions"
1515
},
1616
"minimum-stability": "dev",
1717
"prefer-stable": true,
@@ -26,8 +26,8 @@
2626
},
2727
"extra": {
2828
"laminas": {
29-
"component": "Laminas\\Db\\Mysql",
30-
"config-provider": "Laminas\\Db\\Mysql\\ConfigProvider"
29+
"component": "Laminas\\Db\\Adapter\\Mysql",
30+
"config-provider": "Laminas\\Db\\Adapter\\Mysql\\ConfigProvider"
3131
}
3232
},
3333
"require": {
@@ -52,13 +52,13 @@
5252
},
5353
"autoload": {
5454
"psr-4": {
55-
"Laminas\\Db\\Mysql\\": "src/"
55+
"Laminas\\Db\\Adapter\\Mysql\\": "src/"
5656
}
5757
},
5858
"autoload-dev": {
5959
"psr-4": {
60-
"LaminasTest\\Db\\Mysql\\": "test/unit/",
61-
"LaminasIntegrationTest\\Db\\Mysql\\": "test/integration/"
60+
"LaminasTest\\Db\\Adapter\\Mysql\\": "test/unit/",
61+
"LaminasIntegrationTest\\Db\\Adapter\\Mysql\\": "test/integration/"
6262
}
6363
},
6464
"scripts": {
@@ -73,8 +73,5 @@
7373
"test-integration": "phpunit --colors=always --testsuite \"integration test\"",
7474
"static-analysis": "psalm --shepherd --stats",
7575
"upload-coverage": "coveralls -v"
76-
},
77-
"conflict": {
78-
"laminas/laminas-db": "*"
7976
}
8077
}

src/Adapter.php

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,92 @@
11
<?php
22

3-
declare(strict_types=1);
3+
namespace Laminas\Db\Adapter\Mysql;
44

5-
namespace Laminas\Db\Mysql;
6-
7-
use Laminas\Db\Adapter\AbstractAdapter;
8-
use Laminas\Db\Adapter\Profiler;
95
use Laminas\Db\Adapter\Driver\DriverInterface;
10-
use Laminas\Db\Adapter\Platform;
11-
use Laminas\Db\Exception;
12-
use Laminas\Db\ResultSet;
6+
use Laminas\Db\Adapter\Platform\PlatformInterface;
7+
use Laminas\Db\Adapter\AbstractAdapter;
8+
use Laminas\Db\Adapter\Exception;
9+
10+
use function is_string;
11+
use function str_starts_with;
12+
use function strtolower;
1313

1414
/**
15-
* @property DriverInterface $driver
15+
* @property Driver\DriverInterface $driver
1616
* @property Platform\PlatformInterface $platform
1717
*/
1818
class Adapter extends AbstractAdapter
1919
{
20-
/** @var DriverInterface */
21-
protected $driver;
22-
23-
/** @var Platform\PlatformInterface */
24-
protected $platform;
25-
26-
/**
27-
* @deprecated
28-
*
29-
* @var Driver\StatementInterface
30-
*/
31-
protected $lastPreparedStatement;
32-
/**
33-
* @param DriverInterface|array $driver
34-
* @throws Exception\InvalidArgumentException
35-
*/
36-
public function __construct(
37-
array $parameters,
38-
DriverInterface $driver,
39-
Platform\PlatformInterface $platform,
40-
?ResultSet\ResultSetInterface $queryResultPrototype = null,
41-
?Profiler\ProfilerInterface $profiler = null
42-
) {
43-
44-
if ($parameters !== []) {
45-
if ($profiler === null && isset($parameters['profiler'])) {
46-
$profiler = $this->createProfiler($parameters);
47-
}
48-
}
49-
50-
$driver->checkEnvironment();
51-
$this->driver = $driver;
52-
53-
$this->platform = $platform;
54-
$this->queryResultSetPrototype = $queryResultPrototype ?: new ResultSet\ResultSet();
55-
56-
if ($profiler) {
57-
$this->setProfiler($profiler);
20+
public function getCurrentSchema(): string
21+
{
22+
return $this->driver->getConnection()->getCurrentSchema();
23+
}
24+
25+
protected function createDriver(array $parameters): DriverInterface
26+
{
27+
if (! isset($parameters['driver'])) {
28+
throw new Exception\InvalidArgumentException(
29+
__FUNCTION__ . ' expects a "driver" key to be present inside the parameters'
30+
);
31+
}
32+
33+
if ($parameters['driver'] instanceof DriverInterface) {
34+
return $parameters['driver'];
35+
}
36+
37+
if (! is_string($parameters['driver'])) {
38+
throw new Exception\InvalidArgumentException(
39+
__FUNCTION__ . ' expects a "driver" to be a string or instance of DriverInterface'
40+
);
41+
}
42+
43+
$options = [];
44+
if (isset($parameters['options'])) {
45+
$options = (array) $parameters['options'];
46+
unset($parameters['options']);
5847
}
48+
49+
$driverName = strtolower($parameters['driver']);
50+
switch ($driverName) {
51+
case 'mysqli':
52+
$driver = new Driver\Mysqli\Mysqli($parameters, null, null, $options);
53+
break;
54+
case 'pdo':
55+
default:
56+
if ($driverName === 'pdo' || str_starts_with($driverName, 'pdo')) {
57+
$driver = new Driver\Pdo\Pdo($parameters);
58+
}
59+
}
60+
61+
if (! isset($driver) || ! $driver instanceof DriverInterface) {
62+
throw new Exception\InvalidArgumentException('DriverInterface expected');
63+
}
64+
65+
return $driver;
66+
}
67+
68+
protected function createPlatform(array $parameters): PlatformInterface
69+
{
70+
if (isset($parameters['platform'])) {
71+
$platformName = $parameters['platform'];
72+
} elseif ($this->driver instanceof DriverInterface) {
73+
$platformName = $this->driver->getDatabasePlatformName();
74+
} else {
75+
throw new Exception\InvalidArgumentException(
76+
'A platform could not be determined from the provided configuration'
77+
);
78+
}
79+
80+
// currently only supported by the IbmDb2 & Oracle concrete implementations
81+
//$options = $parameters['platform_options'] ?? [];
82+
83+
// mysqli or pdo_mysql driver
84+
if ($this->driver instanceof Driver\Mysqli\Mysqli || $this->driver instanceof Driver\Pdo\Pdo) {
85+
$driver = $this->driver;
86+
} else {
87+
$driver = null;
88+
}
89+
90+
return new Platform\Mysql($driver);
5991
}
6092
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Laminas\Db\Adapter\Mysql;
4+
5+
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
6+
use Psr\Container\ContainerInterface;
7+
8+
use function is_array;
9+
10+
/**
11+
* Database adapter abstract service factory.
12+
*
13+
* Allows configuring several database instances (such as writer and reader).
14+
*/
15+
class AdapterAbstractServiceFactory implements AbstractFactoryInterface
16+
{
17+
/** @var array */
18+
protected $config;
19+
20+
/**
21+
* Can we create an adapter by the requested name?
22+
*
23+
* @param string $requestedName
24+
* @return bool
25+
*/
26+
public function canCreate(ContainerInterface $container, $requestedName)
27+
{
28+
$config = $this->getConfig($container);
29+
if (empty($config)) {
30+
return false;
31+
}
32+
33+
return isset($config[$requestedName])
34+
&& is_array($config[$requestedName])
35+
&& ! empty($config[$requestedName]);
36+
}
37+
38+
/**
39+
* Create a DB adapter
40+
*
41+
* @param string $requestedName
42+
* @return Adapter
43+
*/
44+
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
45+
{
46+
$config = $this->getConfig($container);
47+
return new Adapter($config[$requestedName]);
48+
}
49+
50+
/**
51+
* Get db configuration, if any
52+
*
53+
* @return array
54+
*/
55+
protected function getConfig(ContainerInterface $container)
56+
{
57+
if ($this->config !== null) {
58+
return $this->config;
59+
}
60+
61+
if (! $container->has('config')) {
62+
$this->config = [];
63+
return $this->config;
64+
}
65+
66+
$config = $container->get('config');
67+
if (
68+
! isset($config['db'])
69+
|| ! is_array($config['db'])
70+
) {
71+
$this->config = [];
72+
return $this->config;
73+
}
74+
75+
$config = $config['db'];
76+
if (
77+
! isset($config['adapters'])
78+
|| ! is_array($config['adapters'])
79+
) {
80+
$this->config = [];
81+
return $this->config;
82+
}
83+
84+
$this->config = $config['adapters'];
85+
return $this->config;
86+
}
87+
}

src/AdapterServiceFactory.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?php
22

3-
namespace Laminas\Db\Mysql;
3+
namespace Laminas\Db\Adapter\Mysql;
44

5-
use Laminas\Db\Adapter\Driver\DriverInterface;
6-
use Laminas\Db\Adapter\Platform\PlatformInterface;
7-
use Laminas\Db\Profiler\ProfilerInterface;
8-
use Laminas\Db\ResultSet\ResultSetInterface;
5+
use Laminas\Db\Adapter\AdapterInterface;
96
use Laminas\ServiceManager\Factory\FactoryInterface;
107
use Psr\Container\ContainerInterface;
118

@@ -15,19 +12,10 @@ class AdapterServiceFactory implements FactoryInterface
1512
* Create db adapter service
1613
*
1714
* @param string $requestedName
18-
* @param array $options
19-
* @return Adapter
2015
*/
21-
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
16+
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): AdapterInterface
2217
{
2318
$config = $container->get('config');
24-
25-
return new Adapter(
26-
$config['db'],
27-
$container->get(DriverInterface::class),
28-
$container->get(PlatformInterface::class),
29-
$container->has(ResultSetInterface::class) ? $container->get(ResultSetInterface::class) : null,
30-
$container->has(ProfilerInterface::class) ? $container->get(ProfilerInterface::class) : null
31-
);
19+
return new Adapter($config['db']);
3220
}
3321
}

src/ConfigProvider.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php declare(strict_types=1);
22

3-
namespace Laminas\Db\Mysql;
3+
namespace Laminas\Db\Adapter\Mysql;
44

5-
use Laminas\Db\Adapter\AbstractAdapterServiceFactory;
65
use Laminas\Db\Adapter\AdapterInterface;
76
use Laminas\Db\Adapter\Driver\DriverInterface;
87
use Laminas\Db\Adapter\Platform\PlatformInterface;
@@ -20,14 +19,14 @@ public function __invoke(): array
2019
public function getDependencies(): array
2120
{
2221
return [
23-
'aliases' => [
24-
PlatformInterface::class => Platform\Mysql::class,
22+
'abstract_factories' => [
23+
AdapterAbstractServiceFactory::class,
2524
],
26-
'factories' => [
25+
'factories' => [
2726
AdapterInterface::class => AdapterServiceFactory::class,
28-
//DriverInterface::class => Driver\Mysqli\DriverFactory::class,
29-
DriverInterface::class => Driver\Pdo\DriverFactory::class,
30-
Platform\Mysql::class => InvokableFactory::class,
27+
],
28+
'aliases' => [
29+
Adapter::class => AdapterInterface::class,
3130
],
3231
];
3332
}

src/Driver/Mysqli/Connection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22

3-
namespace Laminas\Db\Mysql\Driver\Mysqli;
3+
namespace Laminas\Db\Adapter\Mysql\Driver\Mysqli;
44

55
use Exception as GenericException;
66
use Laminas\Db\Adapter\Driver\AbstractConnection;
7-
use Laminas\Db\Adapter\Driver\DriverInterface;
87
use Laminas\Db\Adapter\Exception;
98
use Laminas\Db\Adapter\Exception\InvalidArgumentException;
109

@@ -19,7 +18,7 @@
1918

2019
class Connection extends AbstractConnection
2120
{
22-
/** @var DriverInterface */
21+
/** @var Mysqli */
2322
protected $driver;
2423

2524
/** @var \mysqli */
@@ -47,7 +46,7 @@ public function __construct($connectionInfo = null)
4746
/**
4847
* @return $this Provides a fluent interface
4948
*/
50-
public function setDriver(DriverInterface $driver)
49+
public function setDriver(Mysqli $driver)
5150
{
5251
$this->driver = $driver;
5352

0 commit comments

Comments
 (0)