PDO Factory
The PDO Factory plugin centralizes PDO connection creation, eliminating duplicated database configuration logic across application containers.
Three database backends are supported:
- SqlitePdoFactory creates connections to SQLite databases.
- MysqlPdoFactory creates connections to MySQL databases.
- PostgresqlPdoFactory creates connections to PostgreSQL databases.
Why Use It
Applications need a PDO instance for the SQL Driver and Transaction plugins. Without this plugin, each application's container.php duplicates the same switch/match logic to build DSN strings from environment variables. The PDO Factory extracts this into a single, testable component.
Usage
The factory receives environment configuration as an array and returns a configured PDO instance:
The database.type key selects the backend (Sqlite, Mysql, or Postgresql). Unknown values fall back to SQLite.
Container Configuration
PdoFactoryInterface::class => fn (): PdoFactoryInterface => new PdoFactory($_ENV),
PDO::class => fn (ContainerInterface $c): PDO => $c->get(PdoFactoryInterface::class)->create(),
Environment Variables
SQLite
| Variable | Default |
|---|---|
database.type | Sqlite |
database.sqlite.fullpath | var/database/sqlite.db |
MySQL
| Variable | Default |
|---|---|
database.type | Mysql |
database.mysql.host | (required) |
database.mysql.port | 3306 |
database.mysql.dbname | (required) |
database.mysql.charset | utf8mb4 |
database.mysql.user | (required) |
database.mysql.password | (required) |
PostgreSQL
| Variable | Default |
|---|---|
database.type | Postgresql |
database.postgresql.host | (required) |
database.postgresql.port | 5432 |
database.postgresql.dbname | (required) |
database.postgresql.user | (required) |
database.postgresql.password | (required) |
Testing
The factory is testable by injecting a plain array instead of $_ENV: