Skip to content

Commit 9cbe4ed

Browse files
committed
Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent ae0f940 commit 9cbe4ed

File tree

4 files changed

+21
-132
lines changed

4 files changed

+21
-132
lines changed

src/Adapter.php

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/Container/AdapterFactory.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace Laminas\Db\Adapter\Mysql\Container;
66

7+
use Laminas\Db\Adapter\Adapter;
78
use Laminas\Db\Adapter\AdapterInterface;
8-
use Laminas\Db\Adapter\Driver\DriverInterface;
99
use Laminas\Db\Adapter\Exception\RuntimeException;
10-
use Laminas\Db\Adapter\Mysql\Adapter;
1110
use Laminas\Db\Adapter\Platform\PlatformInterface;
1211
use Laminas\Db\Adapter\Profiler\ProfilerInterface;
1312
use Laminas\Db\Container\AdapterManager;
1413
use Laminas\Db\ResultSet\ResultSetInterface;
14+
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
1515
use Psr\Container\ContainerInterface;
1616

1717
final class AdapterFactory
@@ -20,22 +20,28 @@ public function __invoke(ContainerInterface $container): AdapterInterface
2020
{
2121
/** @var AdapterManager $adapterManager */
2222
$adapterManager = $container->get(AdapterManager::class);
23+
2324
/** @var array $config */
24-
$config = $container->get('config');
25+
$config = $container->get('config');
26+
2527
if (! isset($config['db']['driver'])) {
2628
throw new RuntimeException('Database driver configuration is missing.');
2729
}
30+
2831
if (! $adapterManager->has($config['db']['driver'])) {
29-
throw new RuntimeException(sprintf(
32+
throw new ServiceNotFoundException(sprintf(
3033
'Database driver "%s" is not registered in the adapter manager.',
3134
$config['db']['driver']
3235
));
3336
}
37+
3438
return new Adapter(
3539
$adapterManager->get($config['db']['driver']),
3640
$adapterManager->get(PlatformInterface::class),
3741
$adapterManager->get(ResultSetInterface::class),
38-
$adapterManager->get(ProfilerInterface::class)
42+
$adapterManager->has(ProfilerInterface::class)
43+
? $adapterManager->get(ProfilerInterface::class)
44+
: null
3945
);
4046
}
4147
}

src/Container/PlatformInterfaceFactory.php

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

1313
final class PlatformInterfaceFactory
1414
{
15-
public function __invoke(ContainerInterface $container): PlatformInterface
15+
public function __invoke(ContainerInterface $container): PlatformInterface&Mysql
1616
{
1717
/** @var AdapterManager $manager */
1818
$adapterManager = $container->get(AdapterManager::class);
19-
return new Mysql($adapterManager->get(DriverInterface::class));
19+
/** @var DriverInterface $driver */
20+
$driver = $container->get('config')['db']['driver'];
21+
return new Mysql($adapterManager->get($driver));
2022
}
2123
}

src/Platform/Mysql.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
class Mysql extends AbstractPlatform
2020
{
21+
public final const PLATFORM_NAME = 'MySQL';
22+
2123
/**
2224
* {@inheritDoc}
2325
*/
@@ -28,9 +30,6 @@ class Mysql extends AbstractPlatform
2830
*/
2931
protected $quoteIdentifierTo = '``';
3032

31-
/** @var \mysqli|\PDO|Pdo\Pdo|Mysqli\Mysqli */
32-
protected $driver;
33-
3433
/**
3534
* NOTE: Include dashes for MySQL only, need tests for others platforms
3635
*
@@ -42,42 +41,15 @@ class Mysql extends AbstractPlatform
4241
* todo: track down if this still needs to accept null
4342
*/
4443
public function __construct(
45-
DriverInterface|\mysqli|\PDO|null $driver = null
46-
) {
47-
if ($driver) {
48-
$this->setDriver($driver);
49-
}
50-
}
51-
52-
/**
53-
* @param \Laminas\Db\Adapter\Driver\Mysqli\Mysqli|\Laminas\Db\Adapter\Driver\Pdo\Pdo|\mysqli|\PDO $driver
54-
* @return $this Provides a fluent interface
55-
* @throws InvalidArgumentException
56-
*/
57-
public function setDriver($driver)
58-
{
59-
// handle Laminas\Db drivers
60-
if (
61-
$driver instanceof Mysqli\Mysqli
62-
|| ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() === 'Mysql')
63-
|| $driver instanceof \mysqli
64-
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql')
65-
) {
66-
$this->driver = $driver;
67-
return $this;
68-
}
69-
70-
throw new Exception\InvalidArgumentException(
71-
'$driver must be a Laminas\Db\Adapter\Mysql\Driver\*, Mysqli\Mysqli or Pdo\Pdo instance'
72-
);
73-
}
44+
protected readonly DriverInterface|\mysqli|\PDO $driver
45+
) {}
7446

7547
/**
7648
* {@inheritDoc}
7749
*/
7850
public function getName()
7951
{
80-
return 'MySQL';
52+
return self::PLATFORM_NAME;
8153
}
8254

8355
public function getSqlPlatformDecorator(): PlatformDecoratorInterface
@@ -113,11 +85,7 @@ public function quoteTrustedValue($value)
11385
return $quotedViaDriverValue ?? parent::quoteTrustedValue($value);
11486
}
11587

116-
/**
117-
* @param string $value
118-
* @return string|null
119-
*/
120-
protected function quoteViaDriver($value)
88+
protected function quoteViaDriver(string $value): ?string
12189
{
12290
if ($this->driver instanceof DriverInterface) {
12391
// todo: verify this can not return a PDOStatement instance

0 commit comments

Comments
 (0)