lts/microdeps-pdo

A micro dependency that provides a PDO connection factory

dev-master 2021-11-15 16:54 UTC

This package is auto-updated.

Last update: 2024-04-15 22:09:01 UTC


README

MicroDeps are all about very small pieces of code that do a single small thing well

This MicroDep is for PDO

Simply enough it provides a factory for creating PDO instances

Currently this supports MySQL and SQLite Databases, more can be added easily enough, pull requests gratefully received!

Installing

You can use Composer if you want to quickly test out the idea, though it is suggested that as this is so small - you might be better just copy/pasting it into your first party code.

composer require lts/microdeps-pdo

Configuration

You have the choice of defining configuration values as constants using define, or you can add these to the $_ENV super global, perhaps using something like https://github.com/vlucas/phpdotenv

Constants are discouraged but catered for should you wish to use them.

You can also pass in configuration manually at run time, though this is really only intended for testing purposes.

You can see all the possible configuration keys defined in ./src/Config.php

The specific keys you need to define will depend on the database type you want to use, for example:

MySQL

DB_SERVER_USERNAME="foo"
DB_SERVER_PASSWORD="foo"
DB_DATABASE="foo"

SQLite

DB_TYPE=sqlite
DB_SQLITE_PATH=/path/to/file

Usage

Assuming you have defined the correct configuration then creating a PDO instance looks like this:

<?php
declare(strict_types=1);
$pdo=(new \MicroDeps\PDO\PDOFactory(new \MicroDeps\PDO\DSNFactory(new \MicroDeps\PDO\Config())))->getConnection();

Developing

This package is intended to both be useful, and also to be an example of how to write modern well tested code utilising the latest QA tools to enforce a high standard. You are encouraged to clone the repo and have a play with it and see how it all works.

PHP QA CI

This package is using PHP QA CI for the quality assurance and continuous integration. You can read more about that here: https://github.com/LongTermSupport/php-qa-ci

To run QA process locally

To run the full QA process locally, simply run:

./bin/qa

Long Term Support

This package was brought to you by Long Term Support LTD, a company run and founded by Joseph Edmonds

You can get in touch with Joseph at https://joseph.edmonds.contact/

Check out Joseph's recent book The Art of Modern PHP 8