Skip to content

Commit 553e1ec

Browse files
committed
Initial namespace changes
Code restructing Factory implementations Code is not expected to run, this is just a preview Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 80238e7 commit 553e1ec

File tree

14 files changed

+125
-122
lines changed

14 files changed

+125
-122
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"require": {
3434
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
35-
"axleus/laminas-db": "dev-3.0.x"
35+
"axleus/laminas-db": "*"
3636
},
3737
"require-dev": {
3838
"ext-mysqli": "*",

src/Adapter.php

Lines changed: 53 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
namespace Laminas\Db\Mysql;
44

55
use InvalidArgumentException;
6+
use Laminas\Db\Adapter\AbstractAdapter;
67
use Laminas\Db\Adapter\AdapterInterface;
8+
use Laminas\Db\Adapter\ParameterContainer;
9+
use Laminas\Db\Platform;
710
use Laminas\Db\Adapter\Profiler;
11+
use Laminas\Db\Driver\DriverInterface;
12+
use Laminas\Db\Driver\Pdo\Pdo;
13+
use Laminas\Db\Driver\ResultInterface;
14+
use Laminas\Db\Exception;
815
use Laminas\Db\ResultSet;
916

1017
use function func_get_args;
@@ -16,10 +23,10 @@
1623
use function strtolower;
1724

1825
/**
19-
* @property Driver\DriverInterface $driver
26+
* @property DriverInterface $driver
2027
* @property Platform\PlatformInterface $platform
2128
*/
22-
class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
29+
class Adapter extends AbstractAdapter
2330
{
2431
/**
2532
* Query Mode Constants
@@ -39,7 +46,7 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
3946

4047
public const VALUE_QUOTE_SEPARATOR = 'quoteSeparator';
4148

42-
/** @var Driver\DriverInterface */
49+
/** @var DriverInterface */
4350
protected $driver;
4451

4552
/** @var Platform\PlatformInterface */
@@ -58,7 +65,7 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
5865
*/
5966
protected $lastPreparedStatement;
6067
/**
61-
* @param Driver\DriverInterface|array $driver
68+
* @param DriverInterface|array $driver
6269
* @throws Exception\InvalidArgumentException
6370
*/
6471
public function __construct(
@@ -76,9 +83,9 @@ public function __construct(
7683
$profiler = $this->createProfiler($parameters);
7784
}
7885
$driver = $this->createDriver($parameters);
79-
} elseif (! $driver instanceof Driver\DriverInterface) {
86+
} elseif (! $driver instanceof DriverInterface) {
8087
throw new Exception\InvalidArgumentException(
81-
'The supplied or instantiated driver object does not implement ' . Driver\DriverInterface::class
88+
'The supplied or instantiated driver object does not implement ' . DriverInterface::class
8289
);
8390
}
8491

@@ -121,7 +128,7 @@ public function getProfiler()
121128
* getDriver()
122129
*
123130
* @throws Exception\RuntimeException
124-
* @return Driver\DriverInterface
131+
* @return DriverInterface
125132
*/
126133
public function getDriver()
127134
{
@@ -198,7 +205,7 @@ public function query(
198205
$result = $this->driver->getConnection()->execute($sql);
199206
}
200207

201-
if ($result instanceof Driver\ResultInterface && $result->isQueryResult()) {
208+
if ($result instanceof ResultInterface && $result->isQueryResult()) {
202209
$resultSet = $resultPrototype ?? $this->queryResultSetPrototype;
203210
$resultSetCopy = clone $resultSet;
204211

@@ -254,23 +261,23 @@ public function getHelpers()
254261
/**
255262
* @param string $name
256263
* @throws Exception\InvalidArgumentException
257-
* @return Driver\DriverInterface|Platform\PlatformInterface
264+
* @return DriverInterface|Platform\PlatformInterface
258265
*/
259-
public function __get($name)
260-
{
261-
switch (strtolower($name)) {
262-
case 'driver':
263-
return $this->driver;
264-
case 'platform':
265-
return $this->platform;
266-
default:
267-
throw new Exception\InvalidArgumentException('Invalid magic property on adapter');
268-
}
269-
}
266+
// public function __get($name)
267+
// {
268+
// switch (strtolower($name)) {
269+
// case 'driver':
270+
// return $this->driver;
271+
// case 'platform':
272+
// return $this->platform;
273+
// default:
274+
// throw new Exception\InvalidArgumentException('Invalid magic property on adapter');
275+
// }
276+
// }
270277

271278
/**
272279
* @param array $parameters
273-
* @return Driver\DriverInterface
280+
* @return DriverInterface
274281
* @throws InvalidArgumentException
275282
* @throws Exception\InvalidArgumentException
276283
*/
@@ -282,7 +289,7 @@ protected function createDriver($parameters)
282289
);
283290
}
284291

285-
if ($parameters['driver'] instanceof Driver\DriverInterface) {
292+
if ($parameters['driver'] instanceof DriverInterface) {
286293
return $parameters['driver'];
287294
}
288295

@@ -301,44 +308,33 @@ protected function createDriver($parameters)
301308
$driverName = strtolower($parameters['driver']);
302309
switch ($driverName) {
303310
case 'mysqli':
304-
$driver = new Driver\Mysqli\Mysqli($parameters, null, null, $options);
305-
break;
306-
case 'sqlsrv':
307-
$driver = new Driver\Sqlsrv\Sqlsrv($parameters);
308-
break;
309-
case 'oci8':
310-
$driver = new Driver\Oci8\Oci8($parameters);
311-
break;
312-
case 'pgsql':
313-
$driver = new Driver\Pgsql\Pgsql($parameters);
314-
break;
315-
case 'ibmdb2':
316-
$driver = new Driver\IbmDb2\IbmDb2($parameters);
311+
$driver = new Driver\Mysqli\Driver($parameters, null, null, $options);
317312
break;
318313
case 'pdo':
319314
default:
320315
if ($driverName === 'pdo' || strpos($driverName, 'pdo') === 0) {
321-
$driver = new Driver\Pdo\Pdo($parameters);
316+
$driver = new Pdo($parameters);
322317
}
323318
}
324319

325-
if (! isset($driver) || ! $driver instanceof Driver\DriverInterface) {
320+
if (! isset($driver) || ! $driver instanceof DriverInterface) {
326321
throw new Exception\InvalidArgumentException('DriverInterface expected');
327322
}
328323

329324
return $driver;
330325
}
331326

332327
/**
328+
* todo: replace with factory
333329
* @param array $parameters
334330
* @return Platform\PlatformInterface
335331
*/
336332
protected function createPlatform(array $parameters)
337333
{
338334
if (isset($parameters['platform'])) {
339335
$platformName = $parameters['platform'];
340-
} elseif ($this->driver instanceof Driver\DriverInterface) {
341-
$platformName = $this->driver->getDatabasePlatformName(Driver\DriverInterface::NAME_FORMAT_CAMELCASE);
336+
} elseif ($this->driver instanceof DriverInterface) {
337+
$platformName = $this->driver->getDatabasePlatformName(DriverInterface::NAME_FORMAT_CAMELCASE);
342338
} else {
343339
throw new Exception\InvalidArgumentException(
344340
'A platform could not be determined from the provided configuration'
@@ -348,87 +344,40 @@ protected function createPlatform(array $parameters)
348344
// currently only supported by the IbmDb2 & Oracle concrete implementations
349345
$options = $parameters['platform_options'] ?? [];
350346

347+
348+
// todo: replace with factory
351349
switch ($platformName) {
352350
case 'Mysql':
353351
// mysqli or pdo_mysql driver
354-
if ($this->driver instanceof Driver\Mysqli\Mysqli || $this->driver instanceof Driver\Pdo\Pdo) {
352+
if ($this->driver instanceof Driver\Mysqli\Driver || $this->driver instanceof Pdo) {
355353
$driver = $this->driver;
356354
} else {
357355
$driver = null;
358356
}
359357
return new Platform\Mysql($driver);
360-
case 'SqlServer':
361-
// PDO is only supported driver for quoting values in this platform
362-
return new Platform\SqlServer($this->driver instanceof Driver\Pdo\Pdo ? $this->driver : null);
363-
case 'Oracle':
364-
if ($this->driver instanceof Driver\Oci8\Oci8 || $this->driver instanceof Driver\Pdo\Pdo) {
365-
$driver = $this->driver;
366-
} else {
367-
$driver = null;
368-
}
369-
return new Platform\Oracle($options, $driver);
370-
case 'Sqlite':
371-
// PDO is only supported driver for quoting values in this platform
372-
if ($this->driver instanceof Driver\Pdo\Pdo) {
373-
return new Platform\Sqlite($this->driver);
374-
}
375-
return new Platform\Sqlite(null);
376-
case 'Postgresql':
377-
// pgsql or pdo postgres driver
378-
if ($this->driver instanceof Driver\Pgsql\Pgsql || $this->driver instanceof Driver\Pdo\Pdo) {
379-
$driver = $this->driver;
380-
} else {
381-
$driver = null;
382-
}
383-
return new Platform\Postgresql($driver);
384-
case 'IbmDb2':
385-
// ibm_db2 driver escaping does not need an action connection
386-
return new Platform\IbmDb2($options);
387358
default:
388359
return new Platform\Sql92();
389360
}
390361
}
391362

392363
/**
364+
* todo: remains in abstract adapter
393365
* @param array $parameters
394366
* @return Profiler\ProfilerInterface
395367
* @throws Exception\InvalidArgumentException
396368
*/
397-
protected function createProfiler($parameters)
398-
{
399-
if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) {
400-
return $parameters['profiler'];
401-
}
402-
403-
if (is_bool($parameters['profiler'])) {
404-
return $parameters['profiler'] === true ? new Profiler\Profiler() : null;
405-
}
406-
407-
throw new Exception\InvalidArgumentException(
408-
'"profiler" parameter must be an instance of ProfilerInterface or a boolean'
409-
);
410-
}
411-
412-
/**
413-
* @deprecated
414-
*
415-
* @param array $parameters
416-
* @return Driver\DriverInterface
417-
* @throws InvalidArgumentException
418-
* @throws Exception\InvalidArgumentException
419-
*/
420-
protected function createDriverFromParameters(array $parameters)
421-
{
422-
return $this->createDriver($parameters);
423-
}
424-
425-
/**
426-
* @deprecated
427-
*
428-
* @return Platform\PlatformInterface
429-
*/
430-
protected function createPlatformFromDriver(Driver\DriverInterface $driver)
431-
{
432-
return $this->createPlatform($driver);
433-
}
369+
// protected function createProfiler($parameters)
370+
// {
371+
// if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) {
372+
// return $parameters['profiler'];
373+
// }
374+
375+
// if (is_bool($parameters['profiler'])) {
376+
// return $parameters['profiler'] === true ? new Profiler\Profiler() : null;
377+
// }
378+
379+
// throw new Exception\InvalidArgumentException(
380+
// '"profiler" parameter must be an instance of ProfilerInterface or a boolean'
381+
// );
382+
// }
434383
}

src/AdapterServiceFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Laminas\Db\Mysql;
4+
5+
use Laminas\ServiceManager\Factory\FactoryInterface;
6+
use Psr\Container\ContainerInterface;
7+
8+
class AdapterServiceFactory implements FactoryInterface
9+
{
10+
/**
11+
* Create db adapter service
12+
*
13+
* @param string $requestedName
14+
* @param array $options
15+
* @return Adapter
16+
*/
17+
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
18+
{
19+
$config = $container->get('config');
20+
return new Adapter(
21+
$config['db']
22+
);
23+
}
24+
}

src/ConfigProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Laminas\Db\Mysql;
44

5+
use Laminas\Db\Adapter\Driver\DriverInterface;
6+
use Laminas\Db\Adapter\Platform\PlatformInterface;
7+
use Laminas\ServiceManager\Factory\InvokableFactory;
8+
59
readonly class ConfigProvider
610
{
711
public function __invoke(): array
@@ -14,8 +18,14 @@ public function __invoke(): array
1418
public function getDependencies(): array
1519
{
1620
return [
21+
'aliases' => [
22+
DriverInterface::class => Driver\Mysqli\Driver::class,
23+
PlatformInterface::class => Platform\Mysql::class,
24+
],
1725
'factories' => [
18-
Adapter::class => AdapterServiceFactory::class,
26+
Adapter::class => AdapterServiceFactory::class,
27+
Driver\Mysqli\Driver::class => Driver\
28+
Platform\Mysql::class => InvokableFactory::class,
1929
],
2030
];
2131
}

src/Driver/DriverFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Mysql\Driver;
6+
7+
use Laminas\Db\Adapter\Driver\DriverInterface;
8+
use Laminas\Db\Adapter\Driver\Pdo\Connection;
9+
use Laminas\Db\Adapter\Driver\Pdo\Pdo;
10+
use Laminas\ServiceManager\Factory\FactoryInterface;
11+
use Psr\Container\ContainerInterface;
12+
13+
final class DriverFactory implements FactoryInterface
14+
{
15+
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
16+
{
17+
return new Pdo(new Connection($container->get('config')['db']));
18+
}
19+
}

src/Driver/Mysqli/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

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

55
use Exception as GenericException;
66
use Laminas\Db\Adapter\Driver\AbstractConnection;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

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

55
use Laminas\Db\Adapter\Driver\DriverInterface;
66
use Laminas\Db\Adapter\Exception;
@@ -12,7 +12,7 @@
1212
use function extension_loaded;
1313
use function is_string;
1414

15-
class Mysqli implements DriverInterface, Profiler\ProfilerAwareInterface
15+
class Driver implements DriverInterface, Profiler\ProfilerAwareInterface
1616
{
1717
/** @var Connection */
1818
protected $connection;

src/Driver/Mysqli/Result.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

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

55
use Iterator;
66
use Laminas\Db\Adapter\Driver\ResultInterface;

0 commit comments

Comments
 (0)