Skip to content

Commit

Permalink
Changes for PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
srjlewis committed Oct 3, 2024
1 parent 7f19b2f commit 12975d6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
34 changes: 17 additions & 17 deletions src/AbstractExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ abstract class AbstractExtendedPdo extends PDO implements ExtendedPdoInterface
*/
public function __call(string $name, array $arguments)
{
$this->connect();
$this->autoConnect();

if (! method_exists($this->pdo, $name)) {
$class = get_class($this);
Expand All @@ -127,7 +127,7 @@ public function __call(string $name, array $arguments)
*/
public function beginTransaction(): bool
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$result = $this->pdo->beginTransaction();
$this->profiler->finish();
Expand All @@ -145,7 +145,7 @@ public function beginTransaction(): bool
*/
public function commit(): bool
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$result = $this->pdo->commit();
$this->profiler->finish();
Expand All @@ -158,7 +158,7 @@ public function commit(): bool
*
* @return void
*/
abstract public function connect(): void;
abstract public function autoConnect(): void;

/**
*
Expand All @@ -177,7 +177,7 @@ abstract public function disconnect(): void;
*/
public function errorCode(): ?string
{
$this->connect();
$this->autoConnect();
return $this->pdo->errorCode();
}

Expand All @@ -190,7 +190,7 @@ public function errorCode(): ?string
*/
public function errorInfo(): array
{
$this->connect();
$this->autoConnect();
return $this->pdo->errorInfo();
}

Expand All @@ -207,7 +207,7 @@ public function errorInfo(): array
*/
public function exec(string $statement): int|false
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$affectedRows = $this->pdo->exec($statement);
$this->profiler->finish($statement);
Expand Down Expand Up @@ -493,7 +493,7 @@ public function getProfiler(): ProfilerInterface
*/
public function inTransaction(): bool
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$result = $this->pdo->inTransaction();
$this->profiler->finish();
Expand Down Expand Up @@ -525,7 +525,7 @@ public function isConnected(): bool
*/
public function lastInsertId(?string $name = null): string|false
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$result = $this->pdo->lastInsertId($name);
$this->profiler->finish();
Expand All @@ -550,7 +550,7 @@ public function lastInsertId(?string $name = null): string|false
*/
public function perform(string $statement, array $values = []): PDOStatement
{
$this->connect();
$this->autoConnect();
$sth = $this->prepareWithValues($statement, $values);
$this->profiler->start(__FUNCTION__);
$sth->execute();
Expand All @@ -574,7 +574,7 @@ public function perform(string $statement, array $values = []): PDOStatement
*/
public function prepare(string $query, array $options = []): PDOStatement|false
{
$this->connect();
$this->autoConnect();
$sth = $this->pdo->prepare($query, $options);
return $sth;
}
Expand Down Expand Up @@ -610,7 +610,7 @@ public function prepareWithValues(string $statement, array $values = []): PDOSta
return $this->prepare($statement);
}

$this->connect();
$this->autoConnect();

// rebuild the statement and values
$parser = clone $this->parser;
Expand Down Expand Up @@ -645,7 +645,7 @@ public function prepareWithValues(string $statement, array $values = []): PDOSta
*/
public function query(string $query, ?int $fetchMode = null, mixed ...$fetch_mode_args): PDOStatement|false
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$sth = $this->pdo->query($query, $fetchMode, ...$fetch_mode_args);
$this->profiler->finish($sth->queryString);
Expand All @@ -670,7 +670,7 @@ public function query(string $query, ?int $fetchMode = null, mixed ...$fetch_mod
*/
public function quote(string|int|array|float|null $value, int $type = self::PARAM_STR): string|false
{
$this->connect();
$this->autoConnect();

$value = $value ?? '';

Expand Down Expand Up @@ -742,7 +742,7 @@ public function quoteSingleName(string $name): string
*/
public function rollBack(): bool
{
$this->connect();
$this->autoConnect();
$this->profiler->start(__FUNCTION__);
$result = $this->pdo->rollBack();
$this->profiler->finish();
Expand Down Expand Up @@ -992,7 +992,7 @@ protected function setQuoteName(string $driver): void
*/
public function getAttribute(int $attribute): bool|int|string|array|null
{
$this->connect();
$this->autoConnect();
return $this->pdo->getAttribute($attribute);
}

Expand All @@ -1006,7 +1006,7 @@ public function getAttribute(int $attribute): bool|int|string|array|null
*/
public function setAttribute(int $attribute, mixed $value): bool
{
$this->connect();
$this->autoConnect();
return $this->pdo->setAttribute($attribute, $value);
}
}
2 changes: 1 addition & 1 deletion src/DecoratedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null)
* @return void
*
*/
public function connect(): void
public function autoConnect(): void
{
// already connected
}
Expand Down
23 changes: 20 additions & 3 deletions src/ExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ public function __construct(
$this->setQuoteName($parts[0]);
}

public static function connect(
string $dsn,
?string $username = null,
?string $password = null,
?array $options = [],
array $queries = [],
?ProfilerInterface $profiler = null
): static {
$pdo = new static($dsn, $username, $password, $options ?? [], $queries, $profiler);
$pdo->autoConnect();
return $pdo;
}

/**
*
* Connects to the database.
*
* @return void
*/
public function connect(): void
public function autoConnect(): void
{
if ($this->pdo) {
return;
Expand All @@ -100,7 +113,11 @@ public function connect(): void
// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = new PDO($dsn, $username, $password, $options);
if(version_compare(phpversion(), '8.4.0', '<')) {
$this->pdo = new PDO($dsn, $username, $password, $options);
} else {
$this->pdo = PDO::connect($dsn, $username, $password, $options);
}
$this->profiler->finish();

// connection-time queries
Expand Down Expand Up @@ -152,7 +169,7 @@ public function __debugInfo(): array
*/
public function getPdo(): PDO
{
$this->connect();
$this->autoConnect();
return $this->pdo;
}
}
2 changes: 1 addition & 1 deletion src/ExtendedPdoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ExtendedPdoInterface extends PdoInterface
* Connects to the database.
*
*/
public function connect(): void;
public function autoConnect(): void;

/**
*
Expand Down

0 comments on commit 12975d6

Please sign in to comment.