oihana/php-system

The Oihana PHP System library

0.0.0 2025-08-13 09:23 UTC

This package is auto-updated.

Last update: 2025-08-23 07:11:30 UTC


README

Oihana PHP System

Provides a standard set of PHP helpers and tools to create web and command-line applications.

Latest Version
Total Downloads
License

๐Ÿ“š Documentation

Full project documentation is available at:
๐Ÿ‘‰ https://bcommebois.github.io/oihana-php-system

๐Ÿ“ฆ Installation

Requires PHP 8.4+ and ext-pdo

Install via Composer:


โœจ Features

  • Bootstrap helpers: initialize timezone, memory limit, error handling, and safe ini_set helpers
  • Configuration: load TOML configuration files with sensible fallbacks
  • Dependency Injection: convenience functions for building a PHP-DI container and loading definitions
  • Logging: PSR-3 compatible lightweight file logger, plus Monolog config enums
  • HTTP helpers: constants for methods, headers and parameter strategies
  • MySQL utilities: DSN builder and a robust PDO connection builder with safe defaults
  • Date utilities: TimeInterval to parse, format and convert durations

๐Ÿš€ Quick start

require __DIR__ . '/vendor/autoload.php';

use function oihana\init\{ initDefaultTimezone, initMemoryLimit, initErrors, initConfig, initDefinitions, initContainer };
use oihana\logging\Logger;
use oihana\db\mysql\MysqlPDOBuilder;

// Bootstrap
initDefaultTimezone('UTC');
initMemoryLimit('256M');
initErrors([
    'display_errors' => '1',
    'error_log'      => 'var/logs/php_errors.log',
], __DIR__);

// Load config and build the DI container
$config       = initConfig(__DIR__ . '/config', 'app.toml');
$definitions  = initDefinitions(__DIR__ . '/definitions');
$container    = initContainer($definitions, ['config' => $config]);

// Logger
$logger = new Logger(__DIR__ . '/var/logs', Logger::DEBUG);
$logger->info('Application started');

// MySQL PDO
$pdo = (new MysqlPDOBuilder([
    'host'     => '127.0.0.1',
    'dbname'   => 'demo',
    'username' => 'root',
    'password' => 'secret',
]))();

๐Ÿงฐ Usage

Init helpers

use function oihana\init\{ initDefaultTimezone, initMemoryLimit, initErrors, setIniIfExists };

initDefaultTimezone('UTC');
initMemoryLimit('512M');
initErrors([
    'display_errors'         => '1',
    'display_startup_errors' => '1',
    'error_log'              => 'var/logs/php_errors.log',
], __DIR__);

// Safe ini_set wrapper (no-op on empty values)
setIniIfExists('upload_max_filesize', '64M');

Configuration and DI container

use function oihana\init\{ initConfig, initDefinitions, initContainer };

$config      = initConfig(__DIR__ . '/config', 'app.toml');
$definitions = initDefinitions(__DIR__ . '/definitions');
$container   = initContainer($definitions, ['config' => $config]);

Logging (PSR-3)

use oihana\logging\Logger;

$logger = new Logger(__DIR__ . '/var/logs', Logger::INFO);
$logger->info('App started');
$logger->error('An error occurred: {error}', ['error' => 'boom']);

// Optional helpers
$logger->clear();             // remove all log files in log directory
$files = $logger->getLogFiles();

MySQL PDO builder

use oihana\db\mysql\MysqlPDOBuilder;

$pdo = (new MysqlPDOBuilder([
    'host'     => 'localhost',
    'dbname'   => 'test_db',
    'username' => 'user',
    'password' => 'secret',
    // 'validate'   => false, // disable validation if needed
    // 'skipDbName' => true,  // build DSN without dbname
]))();

HTTP helpers

use oihana\http\{ HttpMethod, HttpHeaders, HttpParamStrategy };

$method  = HttpMethod::POST;
$header  = HttpHeaders::CONTENT_TYPE;
$strategy = HttpParamStrategy::BOTH; // read from body and query

TimeInterval (durations)

use oihana\date\TimeInterval;

$d = new TimeInterval('1h 2m 5s');
echo $d->humanize();   // 1h 2m 5s
echo $d->formatted();  // 1:02:05
echo $d->toSeconds();  // 3725

โœ… Running Unit Tests

To run all tests:

composer run-script test

To run a specific test file:

composer run test ./tests/oihana/date/TimeIntervalTest.php

๐Ÿค Contributing

Contributions are welcome! Please:

  • Open an issue for discussion before large changes
  • Write tests for new features and bug fixes
  • Run the full test suite locally before submitting a PR

๐Ÿ—’๏ธ Changelog

See CHANGELOG.md for notable changes.

๐Ÿงพ License

This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).

๐Ÿ‘ค About the author

๐Ÿ› ๏ธ Generate the Documentation

We use phpDocumentor to generate the documentation into the ./docs folder.

composer doc

๐Ÿ”— Related packages

  • oihana/php-core โ€“ core helpers and utilities used by this library: https://github.com/BcommeBois/oihana-php-core
  • oihana/php-exceptions โ€“ a curated set of reusable custom exception classes for PHP: https://github.com/BcommeBois/oihana-php-exceptions
  • oihana/php-reflect โ€“ reflection and hydration utilities: https://github.com/BcommeBois/oihana-php-reflect
  • oihana/php-enums โ€“ a collection of strongly-typed constant enumerations for PHP: https://github.com/BcommeBois/oihana-php-enums
  • oihana/php-files โ€“ a versatile PHP library for seamless and portable file and path handling: https://github.com/BcommeBois/oihana-php-files