feat: initial commit of camelia/orm
Attribute-mapped Active Record ORM for MariaDB/MySQL over PDO. - Attribute-driven metadata: TableName, PrimaryKey, AutoIncrement, Column, Guarded, Ignore, Required, Enum, Nullable, Length, Precision, Unique, UniqueGroup, Index, MaxLength. - Relationship mapping: BelongsTo, HasOne, HasMany, BelongsToMany with batched eager loading and a 3-level depth limit. - Orm entry point with fromArray, fromPdo, fromEnv and nested transactions. - Immutable Query with Cond operators and GroupCond grouping. - Repository read and write API, pagination, and typed exception hierarchy. - Declares PHP >=8.3; Mago and PHPStan pin the parser to 8.3. - MIT license.
This commit is contained in:
commit
66e93e3070
97 files changed
+12188
No files matched your search
@@ -0,0 +1,117 @@
|
||||
<?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
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user