Search by

phpkernel / system

phpkernel

System utilities for time, filesystem, and safe root management

Package info

codeberg.org/phpkernel/system

Issues

pkg:composer/phpkernel/system

Statistics

Installs: 3

Dependents: 1

Suggesters: 0

v1.0.0 2026-12-02 20:17 UTC

This package is auto-updated.

Last update: 2026-12-02 17:31:07 UTC


README

Strict, lightweight system utilities for PHP 8.5+ covering atomic filesystem operations, path-traversal-safe root resolution, and immutable clocks.

Requirements

  • PHP 8.5 or higher
  • POSIX operating system (ext-posix)

Installation

composer require phpkernel/foundation.system

Components

1. Filesystem (Filesystem / FilesystemInterface)

Provides safe, context-aware filesystem operations with automatic parent directory creation, atomic file writes, symlink handling, and cross-device move fallbacks.

use PHPKernel\Foundation\System\Filesystem;

$fs = new Filesystem();

// Atomic write via temporary file + rename (default file permissions: 0o644, directories: 0o755)
$fs->fwrite('/var/app/config/settings.json', '{"debug":false}');

// Private file write (automatically grants +x traverse bits to created parent directories: 0o700)
$fs->fwrite('/var/app/secrets/api.key', 'secret-token', 0o600);

// Append content with exclusive lock
$fs->append('/var/app/logs/app.log', "Application started\n");

// Check existence (returns true only for regular files or symlinks pointing to regular files)
if ($fs->exists('/var/app/config/settings.json')) {
    $content = $fs->get('/var/app/config/settings.json');
}

// Create directories idempotently (throws if path is occupied by a file or symlink)
$fs->mkdir('/var/app/cache', 0o750);

// Move files or directories (protects against self-containment and falls back to copy+remove across devices)
$fs->move('/var/app/cache', '/var/app/cache_old');

// Recursively remove files, directories, and symlinks (without deleting symlink targets)
$fs->remove('/var/app/cache_old');

All filesystem failures throw \RuntimeException enriched with the exact operation context and underlying system error message.

2. SafeRoot (SafeRoot / SafeRootInterface)

Lexically resolves and validates relative paths against an existing root directory to prevent directory traversal attacks without requiring target files to exist yet.

use PHPKernel\Foundation\System\SafeRoot;

$root = new SafeRoot('/var/www/storage');

// Access normalized root path
echo $root->resolvedRoot; // '/var/www/storage'

// Normalize relative paths
$relative = $root->resolve('uploads/./2026/../avatars/user.png'); // 'uploads/avatars/user.png'

// Build full safe path
$fullPath = $root->path('uploads/avatars/user.png'); // '/var/www/storage/uploads/avatars/user.png'

Validation rules enforced by SafeRoot (throws \InvalidArgumentException):

  • Root directory must exist and resolve to a directory (not a regular file).
  • Relative paths may only contain [A-Za-z0-9._/-].
  • Leading slashes (/path), trailing slashes (path/), and consecutive slashes (a//b) are rejected.
  • Dot-only segments (..., ....) and paths escaping or collapsing to the root directory (.., a/../.., .) are rejected.

3. Clocks (ClockInterface, LocalClock, UtcClock)

Immutable, readonly clock implementations returning \DateTimeImmutable instances.

use PHPKernel\Foundation\System\Clock\LocalClock;
use PHPKernel\Foundation\System\Clock\UtcClock;

$utcClock = new UtcClock();
$utcNow = $utcClock->now(); // \DateTimeImmutable in UTC

$localClock = new LocalClock('Europe/Moscow'); // Accepts string or \DateTimeZone
$localNow = $localClock->now(); // \DateTimeImmutable in Europe/Moscow

Quality Assurance

The codebase adheres to strict coding standards, maximum static analysis level, and 100% mutation testing coverage:

# Run code style check, PHPStan (level max), and PHPUnit
composer check

# Run Infection mutation testing (100% MSI required)
composer mutate

# Run full CI pipeline
composer ci

License

This library is open-sourced software licensed under the MIT license.