Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
operating-system:
- ubuntu-latest
php-version:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- name: Checkout
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 6.1.0

- Reintroduce PHP 8.1–8.3 support; `ExtendedPdo::connect()` is not supported in those versions, and requires PHP 8.4, by @francislavoie in https://github.com/auraphp/Aura.Sql/pull/245
- Mark all `$dsn` and `$password` parameters as `#[\SensitiveParameter]` to prevent leaking sensitive information in error messages, by @francislavoie in https://github.com/auraphp/Aura.Sql/pull/245

## 6.0.0

- CHG: BC Break `ExtendedPdo::connect` renamed to `ExtendedPdo::lazyConnect`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
],
"require": {
"php": "^8.4",
"php": ">=8.1",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"ext-pdo": "*"
},
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./phpunit.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<!-- Uncovered files is disabled since it causes fatal errors due to the version-specific classes, due to redeclaration of the same class -->
<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">./src</directory>
</include>
Expand Down
4 changes: 2 additions & 2 deletions src/DecoratedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null)
}

public static function connect(
string $dsn,
#[\SensitiveParameter] string $dsn,
?string $username = null,
?string $password = null,
#[\SensitiveParameter] ?string $password = null,
?array $options = []
): static {
return new static(\PDO::connect($dsn, $username, $password, $options));
Expand Down
162 changes: 5 additions & 157 deletions src/ExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,160 +8,8 @@
*/
namespace Aura\Sql;

use Aura\Sql\Profiler\Profiler;
use Aura\Sql\Profiler\ProfilerInterface;
use PDO;

/**
*
* A lazy-connecting PDO with extended methods.
*
* @package Aura.Sql
*
*/
class ExtendedPdo extends AbstractExtendedPdo
{
/**
*
* Constructor arguments for instantiating the PDO connection.
*
* @var array
*
*/
protected array $args = [];

/**
*
* Constructor.
*
* This overrides the parent so that it can take connection attributes as a
* constructor parameter, and set them after connection.
*
* @param string $dsn The data source name for the connection.
*
* @param string|null $username The username for the connection.
*
* @param string|null $password The password for the connection.
*
* @param array $options Driver-specific options for the connection.
*
* @param array $queries Queries to execute after the connection.
*
* @param \Aura\Sql\Profiler\ProfilerInterface|null $profiler Tracks and logs query profiles.
*
* @see http://php.net/manual/en/pdo.construct.php
*/
public function __construct(
string $dsn,
?string $username = null,
?string $password = null,
array $options = [],
array $queries = [],
?ProfilerInterface $profiler = null
) {
// if no error mode is specified, use exceptions
if (! isset($options[PDO::ATTR_ERRMODE])) {
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
}

// retain the arguments for later
$this->args = [
$dsn,
$username,
$password,
$options,
$queries
];

// retain a profiler, instantiating a default one if needed
$this->setProfiler($profiler ?? new Profiler());

// retain a query parser
$parts = explode(":", $dsn);
$parser = $this->newParser($parts[0]);
$this->setParser($parser);

// set quotes for identifier names
$this->setQuoteName($parts[0]);
}

public static function connect(
string $dsn,
?string $username = null,
?string $password = null,
?array $options = []
): static {
return new static($dsn, $username, $password, $options);
}

/**
*
* Connects to the database.
*
* @return void
*/
public function lazyConnect(): void
{
if ($this->pdo) {
return;
}

// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = new PDO($dsn, $username, $password, $options);
$this->profiler->finish();

// connection-time queries
foreach ($queries as $query) {
$this->exec($query);
}
}

/**
*
* Disconnects from the database.
*
* @return void
*
*/
public function disconnect(): void
{
$this->profiler->start(__FUNCTION__);
$this->pdo = null;
$this->profiler->finish();
}

/**
*
* The purpose of this method is to hide sensitive data from stack traces.
*
* @return array
*
*/
public function __debugInfo(): array
{
return [
'args' => [
$this->args[0],
'****',
'****',
$this->args[3],
$this->args[4],
],
];
}

/**
*
* Return the inner PDO (if any)
*
* @return \PDO
*
*/
public function getPdo(): PDO
{
$this->lazyConnect();
return $this->pdo;
}
}
if (PHP_VERSION_ID < 80400) {
require_once __DIR__ . '/ExtendedPdoPHP83.php';
} else {
require_once __DIR__ . '/ExtendedPdoPHP84.php';
}
163 changes: 163 additions & 0 deletions src/ExtendedPdoPHP83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license https://opensource.org/licenses/MIT MIT
*
*/
namespace Aura\Sql;

use Aura\Sql\Profiler\Profiler;
use Aura\Sql\Profiler\ProfilerInterface;
use PDO;

/**
*
* A lazy-connecting PDO with extended methods.
*
* @package Aura.Sql
*
*/
class ExtendedPdo extends AbstractExtendedPdo
{
/**
*
* Constructor arguments for instantiating the PDO connection.
*
* @var array
*
*/
protected array $args = [];

/**
*
* Constructor.
*
* This overrides the parent so that it can take connection attributes as a
* constructor parameter, and set them after connection.
*
* @param string $dsn The data source name for the connection.
*
* @param string|null $username The username for the connection.
*
* @param string|null $password The password for the connection.
*
* @param array $options Driver-specific options for the connection.
*
* @param array $queries Queries to execute after the connection.
*
* @param \Aura\Sql\Profiler\ProfilerInterface|null $profiler Tracks and logs query profiles.
*
* @see http://php.net/manual/en/pdo.construct.php
*/
public function __construct(
#[\SensitiveParameter] string $dsn,
?string $username = null,
#[\SensitiveParameter] ?string $password = null,
array $options = [],
array $queries = [],
?ProfilerInterface $profiler = null
) {
// if no error mode is specified, use exceptions
if (! isset($options[PDO::ATTR_ERRMODE])) {
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
}

// retain the arguments for later
$this->args = [
$dsn,
$username,
$password,
$options,
$queries
];

// retain a profiler, instantiating a default one if needed
$this->setProfiler($profiler ?? new Profiler());

// retain a query parser
$parts = explode(":", $dsn);
$parser = $this->newParser($parts[0]);
$this->setParser($parser);

// set quotes for identifier names
$this->setQuoteName($parts[0]);
}

public function connect(): void
{
throw new \RuntimeException('The connect() method is no longer supported in Aura.Sql v6. Use lazyConnect() instead.');
}

/**
*
* Connects to the database.
*
* @return void
*/
public function lazyConnect(): void
{
if ($this->pdo) {
return;
}

// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = new PDO($dsn, $username, $password, $options);
$this->profiler->finish();

// connection-time queries
foreach ($queries as $query) {
$this->exec($query);
}
}

/**
*
* Disconnects from the database.
*
* @return void
*
*/
public function disconnect(): void
{
$this->profiler->start(__FUNCTION__);
$this->pdo = null;
$this->profiler->finish();
}

/**
*
* The purpose of this method is to hide sensitive data from stack traces.
*
* @return array
*
*/
public function __debugInfo(): array
{
return [
'args' => [
$this->args[0],
'****',
'****',
$this->args[3],
$this->args[4],
],
];
}

/**
*
* Return the inner PDO (if any)
*
* @return \PDO
*
*/
public function getPdo(): PDO
{
$this->lazyConnect();
return $this->pdo;
}
}
Loading