Skip to content

Commit 4941f90

Browse files
committed
Refactor
Aligning with laminas-db Adds additional factories Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent f486c15 commit 4941f90

8 files changed

+130
-74
lines changed

src/ConfigProvider.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
namespace Laminas\Db\Adapter\Mysql;
44

5-
use Laminas\Db\Adapter\AdapterAbstractServiceFactory;
6-
use Laminas\Db\Adapter\AdapterInterface;
7-
use Laminas\Db\Adapter\AdapterServiceFactory;
85
use Laminas\Db\Container\AdapterManager;
9-
use Laminas\Db\Container\MetadataFactory;
10-
use Laminas\Db\Metadata\MetadataInterface;
116

127
readonly class ConfigProvider
138
{
@@ -21,22 +16,11 @@ public function __invoke(): array
2116
public function getDependencies(): array
2217
{
2318
return [
24-
// 'abstract_factories' => [
25-
// AdapterAbstractServiceFactory::class,
26-
// ],
27-
'aliases' => [
28-
AdapterInterface::class => Adapter::class,
29-
//MetadataInterface::class => Metadata\Source\MysqlMetadata::class,
30-
],
31-
'delegators' => [
19+
'delegators' => [
3220
AdapterManager::class => [
3321
Container\AdapterManagerDelegator::class,
3422
],
3523
],
36-
'factories' => [
37-
Adapter::class => Container\AdapterFactory::class,
38-
//Metadata\Source\MysqlMetadata::class => MetadataFactory::class,
39-
],
4024
];
4125
}
4226
}

src/Container/AdapterFactory.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

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

7-
use Laminas\Db\Adapter\ConfigInterface;
87
use Laminas\Db\Adapter\Driver\DriverInterface;
8+
use Laminas\Db\Adapter\Driver\ResultInterface;
99
use Laminas\Db\Adapter\Mysql\Adapter;
1010
use Laminas\Db\Adapter\Platform\PlatformInterface;
1111
use Laminas\Db\Adapter\Profiler\ProfilerInterface;
@@ -16,16 +16,13 @@ final class AdapterFactory
1616
{
1717
public function __invoke(ContainerInterface $container): Adapter
1818
{
19-
// $adapter = new Adapter($container->get('config')['db']);
20-
// $adapter->setDriver($container->get('db_driver'));
21-
// $adapter->setPlatform($container->get('db_platform'));
22-
$manager = $container->get(AdapterManager::class);
23-
$config = $manager->get(ConfigInterface::class);
19+
/** @var AdapterManager $adapterManager */
20+
$adapterManager = $container->get(AdapterManager::class);
2421
return new Adapter(
25-
$manager->get(DriverInterface::class),
26-
$manager->get(PlatformInterface::class),
27-
null, // needs added to AdapterManager allowed services
28-
$manager->get(ProfilerInterface::class)
22+
$adapterManager->get(DriverInterface::class),
23+
$adapterManager->get(PlatformInterface::class),
24+
$adapterManager->get(ResultInterface::class),
25+
$adapterManager->get(ProfilerInterface::class)
2926
);
3027
}
3128
}

src/Container/AdapterManagerDelegator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use Laminas\Db\Adapter\AdapterInterface;
88
use Laminas\Db\Adapter\Driver\ConnectionInterface;
99
use Laminas\Db\Adapter\Driver\DriverInterface;
10+
use Laminas\Db\Adapter\Driver\ResultInterface;
1011
use Laminas\Db\Adapter\Platform\PlatformInterface;
12+
use Laminas\Db\Adapter\Profiler;
1113
use Laminas\Db\Container\AdapterManager;
1214
use Laminas\ServiceManager\Factory\DelegatorFactoryInterface;
15+
use Laminas\ServiceManager\Factory\InvokableFactory;
1316
use Psr\Container\ContainerInterface;
1417

1518
final class AdapterManagerDelegator implements DelegatorFactoryInterface
@@ -22,11 +25,16 @@ public function __invoke(
2225
): AdapterManager {
2326
$adapterManager = $callback();
2427
$adapterManager->configure([
28+
'aliases' => [
29+
Profiler\ProfilerInterface::class => Profiler\Profiler::class,
30+
],
2531
'factories' => [
2632
AdapterInterface::class => AdapterFactory::class,
2733
ConnectionInterface::class => ConnectionInterfaceFactory::class,
2834
DriverInterface::class => DriverInterfaceFactory::class,
29-
PlatformInterface::class => PlatformFactory::class
35+
PlatformInterface::class => PlatformInterfaceFactory::class,
36+
Profiler\Profiler::class => InvokableFactory::class,
37+
ResultInterface::class => ResultInterfaceFactory::class,
3038
],
3139
]);
3240

src/Container/ConnectionInterfaceFactory.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,16 @@
55
namespace Laminas\Db\Adapter\Mysql\Container;
66

77
use Laminas\Db\Adapter\Driver\ConnectionInterface;
8-
use Laminas\Db\Adapter\Exception\RuntimeException;
98
use Laminas\Db\Container\AdapterManager;
10-
use Laminas\Db\Adapter\Mysql\Driver;
11-
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
129
use Psr\Container\ContainerInterface;
1310

14-
use function strtolower;
15-
1611
final class ConnectionInterfaceFactory
1712
{
18-
public function __invoke(
19-
ContainerInterface $container
20-
): ConnectionInterface {
21-
$manager = $container->get(AdapterManager::class);
22-
$connection = $this->getConnection($manager->get('db'));
23-
return $connection;
24-
}
13+
use InterfaceFactoryTrait;
2514

26-
private function getConnection(array $config): ConnectionInterface
27-
{
28-
// Got to have this to determine the driver type
29-
return match(strtolower($config['driver'])) {
30-
'pdo_mysql',
31-
'pdomysql',
32-
'pdo' => new Driver\Pdo\Connection($config),
33-
'mysqli' => new Driver\Mysqli\Connection($config),
34-
default => throw new ServiceNotCreatedException(
35-
'Connection type can not be determined from provided driver: ' . $config['driver']
36-
),
37-
};
15+
public function __invoke(ContainerInterface $container): ConnectionInterface {
16+
/** @var AdapterManager $adapterManager */
17+
$adapterManager = $container->get(AdapterManager::class);
18+
return $this->getConnection($adapterManager);
3819
}
3920
}

src/Container/DriverInterfaceFactory.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,18 @@
44

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

7-
use Laminas\Db\Adapter\Driver\ConnectionInterface;
87
use Laminas\Db\Adapter\Driver\DriverInterface;
9-
use Laminas\Db\Adapter\Mysql\Driver;
108
use Laminas\Db\Container\AdapterManager;
11-
use Laminas\Db\Adapter\Exception\RuntimeException;
12-
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
139
use Psr\Container\ContainerInterface;
1410

1511
final class DriverInterfaceFactory
1612
{
17-
public function __invoke(ContainerInterface $container): DriverInterface
18-
{
19-
$manager = $container->get(AdapterManager::class);
20-
return $this->getDriver($manager);
21-
}
13+
use InterfaceFactoryTrait;
2214

23-
private function getDriver(AdapterManager $manager): DriverInterface
15+
public function __invoke(ContainerInterface $container): DriverInterface
2416
{
25-
$config = $manager->get('db');
26-
// Got to have this to determine the driver type
27-
28-
return match (strtolower($config['driver'])) {
29-
'pdo_mysql',
30-
'pdomysql',
31-
'pdo' => new Driver\Pdo\Pdo($manager->get(ConnectionInterface::class)),
32-
'mysqli' => new Driver\Mysqli\Mysqli($manager->get(ConnectionInterface::class)),
33-
default => throw new ServiceNotCreatedException(
34-
'Driver type can not be determined from provided driver: ' . $config['driver']
35-
),
36-
};
17+
/** @var AdapterManager $adapterManager */
18+
$adapterManager = $container->get(AdapterManager::class);
19+
return $this->getDriver($adapterManager);
3720
}
3821
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\ConnectionInterface;
8+
use Laminas\Db\Adapter\Driver\DriverInterface;
9+
use Laminas\Db\Adapter\Driver\ResultInterface;
10+
use Laminas\Db\Adapter\Driver\Pdo\Result;
11+
use Laminas\Db\Adapter\Mysql\Driver;
12+
use Laminas\Db\Container\AdapterManager;
13+
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
14+
15+
use function strtolower;
16+
17+
trait InterfaceFactoryTrait
18+
{
19+
private function getConnection(AdapterManager $adapterManager): ConnectionInterface
20+
{
21+
$config = $adapterManager->get('db');
22+
23+
if ($this->isPdo($config['driver'])) {
24+
return new Driver\Pdo\Connection($config);
25+
}
26+
return new Driver\Mysqli\Connection($config);
27+
}
28+
29+
private function getDriver(AdapterManager $adapterManager): DriverInterface
30+
{
31+
$config = $adapterManager->get('db');
32+
33+
if ($this->isPdo($config['driver'])) {
34+
return new Driver\Pdo\Pdo($adapterManager->get(ConnectionInterface::class));
35+
}
36+
return new Driver\Mysqli\Mysqli($adapterManager->get(ConnectionInterface::class));
37+
}
38+
39+
private function getResult(AdapterManager $adapterManager): ResultInterface
40+
{
41+
$config = $adapterManager->get('db');
42+
43+
if ($this->isPdo($config['driver'])) {
44+
return new Result();
45+
}
46+
return new Driver\Mysqli\Result();
47+
}
48+
49+
private function isPdo(string $driver): bool
50+
{
51+
return match (strtolower($driver)) {
52+
'pdo_mysql',
53+
'pdomysql',
54+
'pdo' => true,
55+
'mysqli' => false,
56+
default => throw new ServiceNotCreatedException(
57+
'Driver type can not be determined from provided driver: ' . $driver
58+
),
59+
};
60+
}
61+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\DriverInterface;
8+
use Laminas\Db\Adapter\Platform\PlatformInterface;
9+
use Laminas\Db\Adapter\Mysql\Platform\Mysql;
10+
use Laminas\Db\Container\AdapterManager;
11+
use Psr\Container\ContainerInterface;
12+
13+
final class PlatformInterfaceFactory
14+
{
15+
public function __invoke(ContainerInterface $container): PlatformInterface
16+
{
17+
/** @var AdapterManager $manager */
18+
$adapterManager = $container->get(AdapterManager::class);
19+
return new Mysql($adapterManager->get(DriverInterface::class));
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\ResultInterface;
8+
use Laminas\Db\Container\AdapterManager;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class ResultInterfaceFactory
12+
{
13+
use InterfaceFactoryTrait;
14+
15+
public function __invoke(ContainerInterface $container): ResultInterface
16+
{
17+
/** @var AdapterManager $adapterManager */
18+
$adapterManager = $container->get(AdapterManager::class);
19+
return $this->getResult($adapterManager);
20+
}
21+
}

0 commit comments

Comments
 (0)