From 12975d6e97c87a57981b7089ee072517b98b2890 Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Thu, 3 Oct 2024 12:29:06 +0100 Subject: [PATCH 1/5] Changes for PHP 8.4 --- src/AbstractExtendedPdo.php | 34 +++++++++++++++++----------------- src/DecoratedPdo.php | 2 +- src/ExtendedPdo.php | 23 ++++++++++++++++++++--- src/ExtendedPdoInterface.php | 2 +- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/AbstractExtendedPdo.php b/src/AbstractExtendedPdo.php index cb28bbc2..f241969d 100644 --- a/src/AbstractExtendedPdo.php +++ b/src/AbstractExtendedPdo.php @@ -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); @@ -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(); @@ -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(); @@ -158,7 +158,7 @@ public function commit(): bool * * @return void */ - abstract public function connect(): void; + abstract public function autoConnect(): void; /** * @@ -177,7 +177,7 @@ abstract public function disconnect(): void; */ public function errorCode(): ?string { - $this->connect(); + $this->autoConnect(); return $this->pdo->errorCode(); } @@ -190,7 +190,7 @@ public function errorCode(): ?string */ public function errorInfo(): array { - $this->connect(); + $this->autoConnect(); return $this->pdo->errorInfo(); } @@ -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); @@ -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(); @@ -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(); @@ -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(); @@ -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; } @@ -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; @@ -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); @@ -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 ?? ''; @@ -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(); @@ -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); } @@ -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); } } diff --git a/src/DecoratedPdo.php b/src/DecoratedPdo.php index 8438b46a..b3798796 100644 --- a/src/DecoratedPdo.php +++ b/src/DecoratedPdo.php @@ -51,7 +51,7 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null) * @return void * */ - public function connect(): void + public function autoConnect(): void { // already connected } diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index 16e28194..62d2744f 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -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; @@ -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 @@ -152,7 +169,7 @@ public function __debugInfo(): array */ public function getPdo(): PDO { - $this->connect(); + $this->autoConnect(); return $this->pdo; } } diff --git a/src/ExtendedPdoInterface.php b/src/ExtendedPdoInterface.php index 88196a50..369fc28e 100644 --- a/src/ExtendedPdoInterface.php +++ b/src/ExtendedPdoInterface.php @@ -28,7 +28,7 @@ interface ExtendedPdoInterface extends PdoInterface * Connects to the database. * */ - public function connect(): void; + public function autoConnect(): void; /** * From e2f551ec128d4ca85856cadf4c088a8d96eb9213 Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Tue, 15 Oct 2024 13:28:37 +0100 Subject: [PATCH 2/5] Changes for PHP 8.4 --- src/AbstractExtendedPdo.php | 34 +++++++++++++++++----------------- src/DecoratedPdo.php | 2 +- src/ExtendedPdo.php | 6 +++--- src/ExtendedPdoInterface.php | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/AbstractExtendedPdo.php b/src/AbstractExtendedPdo.php index f241969d..3e237fe3 100644 --- a/src/AbstractExtendedPdo.php +++ b/src/AbstractExtendedPdo.php @@ -105,7 +105,7 @@ abstract class AbstractExtendedPdo extends PDO implements ExtendedPdoInterface */ public function __call(string $name, array $arguments) { - $this->autoConnect(); + $this->establishConnection(); if (! method_exists($this->pdo, $name)) { $class = get_class($this); @@ -127,7 +127,7 @@ public function __call(string $name, array $arguments) */ public function beginTransaction(): bool { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->beginTransaction(); $this->profiler->finish(); @@ -145,7 +145,7 @@ public function beginTransaction(): bool */ public function commit(): bool { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->commit(); $this->profiler->finish(); @@ -158,7 +158,7 @@ public function commit(): bool * * @return void */ - abstract public function autoConnect(): void; + abstract public function establishConnection(): void; /** * @@ -177,7 +177,7 @@ abstract public function disconnect(): void; */ public function errorCode(): ?string { - $this->autoConnect(); + $this->establishConnection(); return $this->pdo->errorCode(); } @@ -190,7 +190,7 @@ public function errorCode(): ?string */ public function errorInfo(): array { - $this->autoConnect(); + $this->establishConnection(); return $this->pdo->errorInfo(); } @@ -207,7 +207,7 @@ public function errorInfo(): array */ public function exec(string $statement): int|false { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $affectedRows = $this->pdo->exec($statement); $this->profiler->finish($statement); @@ -493,7 +493,7 @@ public function getProfiler(): ProfilerInterface */ public function inTransaction(): bool { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->inTransaction(); $this->profiler->finish(); @@ -525,7 +525,7 @@ public function isConnected(): bool */ public function lastInsertId(?string $name = null): string|false { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->lastInsertId($name); $this->profiler->finish(); @@ -550,7 +550,7 @@ public function lastInsertId(?string $name = null): string|false */ public function perform(string $statement, array $values = []): PDOStatement { - $this->autoConnect(); + $this->establishConnection(); $sth = $this->prepareWithValues($statement, $values); $this->profiler->start(__FUNCTION__); $sth->execute(); @@ -574,7 +574,7 @@ public function perform(string $statement, array $values = []): PDOStatement */ public function prepare(string $query, array $options = []): PDOStatement|false { - $this->autoConnect(); + $this->establishConnection(); $sth = $this->pdo->prepare($query, $options); return $sth; } @@ -610,7 +610,7 @@ public function prepareWithValues(string $statement, array $values = []): PDOSta return $this->prepare($statement); } - $this->autoConnect(); + $this->establishConnection(); // rebuild the statement and values $parser = clone $this->parser; @@ -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->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $sth = $this->pdo->query($query, $fetchMode, ...$fetch_mode_args); $this->profiler->finish($sth->queryString); @@ -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->autoConnect(); + $this->establishConnection(); $value = $value ?? ''; @@ -742,7 +742,7 @@ public function quoteSingleName(string $name): string */ public function rollBack(): bool { - $this->autoConnect(); + $this->establishConnection(); $this->profiler->start(__FUNCTION__); $result = $this->pdo->rollBack(); $this->profiler->finish(); @@ -992,7 +992,7 @@ protected function setQuoteName(string $driver): void */ public function getAttribute(int $attribute): bool|int|string|array|null { - $this->autoConnect(); + $this->establishConnection(); return $this->pdo->getAttribute($attribute); } @@ -1006,7 +1006,7 @@ public function getAttribute(int $attribute): bool|int|string|array|null */ public function setAttribute(int $attribute, mixed $value): bool { - $this->autoConnect(); + $this->establishConnection(); return $this->pdo->setAttribute($attribute, $value); } } diff --git a/src/DecoratedPdo.php b/src/DecoratedPdo.php index b3798796..0eb683fa 100644 --- a/src/DecoratedPdo.php +++ b/src/DecoratedPdo.php @@ -51,7 +51,7 @@ public function __construct(PDO $pdo, ?ProfilerInterface $profiler = null) * @return void * */ - public function autoConnect(): void + public function establishConnection(): void { // already connected } diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index 62d2744f..0a00335f 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -94,7 +94,7 @@ public static function connect( ?ProfilerInterface $profiler = null ): static { $pdo = new static($dsn, $username, $password, $options ?? [], $queries, $profiler); - $pdo->autoConnect(); + $pdo->establishConnection(); return $pdo; } @@ -104,7 +104,7 @@ public static function connect( * * @return void */ - public function autoConnect(): void + public function establishConnection(): void { if ($this->pdo) { return; @@ -169,7 +169,7 @@ public function __debugInfo(): array */ public function getPdo(): PDO { - $this->autoConnect(); + $this->establishConnection(); return $this->pdo; } } diff --git a/src/ExtendedPdoInterface.php b/src/ExtendedPdoInterface.php index 369fc28e..4313185b 100644 --- a/src/ExtendedPdoInterface.php +++ b/src/ExtendedPdoInterface.php @@ -28,7 +28,7 @@ interface ExtendedPdoInterface extends PdoInterface * Connects to the database. * */ - public function autoConnect(): void; + public function establishConnection(): void; /** * From 33ce60248408f11d5ce0b776aea751798a0d5728 Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Fri, 15 Nov 2024 10:07:33 +0000 Subject: [PATCH 3/5] Add PHP 8.4 to tests --- .github/workflows/continuous-integration.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index b781be2a..d535b0e9 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -18,6 +18,7 @@ jobs: - '8.1' - '8.2' - '8.3' + - '8.4' steps: - name: Checkout uses: actions/checkout@v1 From 92df92269e1961304d7c46ef0046d71b6f0f3fec Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Fri, 15 Nov 2024 11:13:26 +0000 Subject: [PATCH 4/5] Extra changes for PHP 8.4 --- .github/workflows/continuous-integration.yml | 3 --- composer.json | 2 +- src/ExtendedPdo.php | 10 ++-------- tests/ExtendedConnectPdoTest.php | 11 +++++++++++ 4 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 tests/ExtendedConnectPdoTest.php diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index d535b0e9..1895cb97 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -15,9 +15,6 @@ jobs: operating-system: - ubuntu-latest php-version: - - '8.1' - - '8.2' - - '8.3' - '8.4' steps: - name: Checkout diff --git a/composer.json b/composer.json index 66f142dd..54ad5515 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "require": { - "php": ">=8.1", + "php": ">=8.4", "psr/log": "^1.0 || ^2.0 || ^3.0", "ext-pdo": "*" }, diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index 0a00335f..bb6ff658 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -93,9 +93,7 @@ public static function connect( array $queries = [], ?ProfilerInterface $profiler = null ): static { - $pdo = new static($dsn, $username, $password, $options ?? [], $queries, $profiler); - $pdo->establishConnection(); - return $pdo; + return new static($dsn, $username, $password, $options ?? [], $queries, $profiler); } /** @@ -113,11 +111,7 @@ public function establishConnection(): void // connect $this->profiler->start(__FUNCTION__); list($dsn, $username, $password, $options, $queries) = $this->args; - 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->pdo = PDO::connect($dsn, $username, $password, $options); $this->profiler->finish(); // connection-time queries diff --git a/tests/ExtendedConnectPdoTest.php b/tests/ExtendedConnectPdoTest.php new file mode 100644 index 00000000..25d5269d --- /dev/null +++ b/tests/ExtendedConnectPdoTest.php @@ -0,0 +1,11 @@ + Date: Fri, 15 Nov 2024 11:38:05 +0000 Subject: [PATCH 5/5] Update composer php requirement --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 54ad5515..e4412c13 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "require": { - "php": ">=8.4", + "php": "^8.4", "psr/log": "^1.0 || ^2.0 || ^3.0", "ext-pdo": "*" },