etu/php-tools

This package is abandoned and no longer maintained. No replacement package was suggested.

Some tools for personal projects

dev-master 2015-03-25 14:50 UTC

This package is not auto-updated.

Last update: 2022-01-22 03:36:06 UTC


README

https://travis-ci.org/etu/php-tools.svg?branch=master

Install with composer:

composer.json

{
    "require": {
        "etu/php-tools": "dev-master"
    }
}

Example usage of Config and Db

config.ini

[@]
db.dsn = 'sqlite::memory:'
db.username =
db.password =

[example.com]
db.username = live

[*.dev.example.com]
db.username = dev

or

config.json

{
    "@": {
        "db": {
            "dsn": "sqlite::memory:",
            "username": "",
            "password": ""
        }
    },

    "example.com": {
        "db": {
            "username": "live"
        }
    },

    "*.dev.example.com": {
        "db": {
            "username": "dev"
        }
    }
}

test.php

<?php
require('vendor/autoload.php');

// If using INI-file:
// (Optional) Second parameter matches title in ini-file
$config = new Etu\PhpTools\IniConfig('config.ini', 'optional.example.com');

// If using JSON-file
// (Optional) Second parameter matches first level of objects in json-file
$config = new Etu\PhpTools\JsonConfig('config.json', 'optional.example.com');

// Then load Db
$db = new Etu\PhpTools\Db($config, new Etu\PhpTools\Db\PdoBackend());

$db->query('CREATE TABLE names(id INTEGER PRIMARY KEY, name VARCHAR(10));');
$db->query('INSERT INTO names(id, name) VALUES (NULL, "Alice");');
$db->query('INSERT INTO names(id, name) VALUES (NULL, "Bob");');

print_r(
    $db->query(
        'SELECT * FROM names WHERE name = :name',
        [':name' => 'Alice']
    )->fetchAll()
);

Advanced features of JsonConfig

  • Modules
  • Default settings
  • Environments
  • Overwrites of default settings depending on environment
  • Overwrites of modules

Modules

Example usage of modules and overwriting including defaults:

{
    "__modules__": {
        "my-db-module": {
            "db": {
                "username": "default",
                "password": "default"
            }
        }
    },

    "@": {
        "default": "values"
    },

    "example.com": {
        "__loaded_modules__": [
            "my-db-module"
        ],

        "db": {
            "username": "example.com"
        }
    }
}

This will result in a config that looks like this (if loading the environment example.com):

[config:Etu\PhpTools\JsonConfig:private] => stdClass Object
    (
        [db] => stdClass Object
            (
                [username] => example.com
                [password] => default
            )

        [default] => values
    )