Files
camelia-orm/src/Connection/ConnectionFactory.php
T
dgarcia 0e756e45c7
Tests / PHP 8.4 (push) Failing after 5m34s
refactor!: require PHP 8.4
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs
only on 8.4.

- composer.json: require php >=8.4.
- mago.toml: php-version = "8.4", matching the minimum supported version.
- phpstan.neon: phpVersion 80400.
- Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the
  8.4 formatter now emits.
- Fix the type errors PHPStan 2 reports: class-string<static> variance,
  optional array offsets in fromEnvironment, list vs array for PdoConnection::$lru,
  ReflectionClass<Model> docblocks, Repository made covariant in T, and a redundant
  null comparison in validateRelationPath.
- CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php,
  which failed with a TLS handshake timeout on the runner.

BREAKING CHANGE: PHP 8.4 is now required.
2026-11-15 10:58:11 -03:00

119 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Camelia\Orm\Connection;
use Camelia\Orm\Exception\ConfigurationException;
use PDO;
/**
* Builds connections from an options array, environment variables or an existing PDO.
*
* @api
*/
final class ConnectionFactory
{
private const REQUIRED_ENVIRONMENT = [
'CAMELIA_DB_HOST',
'CAMELIA_DB_USER',
'CAMELIA_DB_NAME',
'CAMELIA_DB_PASS',
];
/**
* @param array<string, mixed> $options
*/
public static function fromArray(array $options): Connection
{
foreach (['host', 'user', 'database'] as $required) {
if (!isset($options[$required]) || trim((string) $options[$required]) === '') {
throw new ConfigurationException(sprintf(
'Connection option "%s" is required.',
$required,
));
}
}
$charset = isset($options['charset']) ? (string) $options['charset'] : 'utf8mb4';
$port = isset($options['port']) ? (int) $options['port'] : 3306;
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
(string) $options['host'],
$port,
(string) $options['database'],
$charset,
);
$pdoOptions = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
if (isset($options['sslCa'])) {
$pdoOptions[PDO::MYSQL_ATTR_SSL_CA] = (string) $options['sslCa'];
}
$pdo = new PDO(
$dsn,
(string) $options['user'],
(string) ($options['password'] ?? ''),
$pdoOptions,
);
return new PdoConnection($pdo);
}
public static function fromEnvironment(): Connection
{
/** @var array<string, string> $values */
$values = [];
$missing = [];
foreach (self::REQUIRED_ENVIRONMENT as $name) {
$value = self::readEnvironment($name);
if ($value === null) {
$missing[] = $name;
continue;
}
$values[$name] = $value;
}
if ($missing !== []) {
throw ConfigurationException::missingEnvironment($missing);
}
return self::fromArray([
'host' => $values['CAMELIA_DB_HOST'],
'user' => $values['CAMELIA_DB_USER'],
'database' => $values['CAMELIA_DB_NAME'],
'password' => $values['CAMELIA_DB_PASS'],
'port' => self::readEnvironment('CAMELIA_DB_PORT') ?? 3306,
'charset' => self::readEnvironment('CAMELIA_DB_CHARSET') ?? 'utf8mb4',
]);
}
public static function fromPdo(PDO $pdo): Connection
{
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return new PdoConnection($pdo);
}
private static function readEnvironment(string $name): ?string
{
$value = getenv($name);
if ($value === false) {
return null;
}
return $value;
}
}