samluvanda/system

A PHP utility library for environment variables, user context, and system information.

v1.0.1 2025-06-22 12:30 UTC

This package is auto-updated.

Last update: 2025-06-22 12:35:22 UTC


README

A PHP utility library for accessing environment variables, user context, system settings, and runtime information.

Namespace: SamLuvanda\System

๐Ÿ“ฆ Installation

Install via Composer:

composer require samluvanda/system

๐Ÿš€ Features

  • Get and set environment variables
  • Access OS, hostname, PHP version, current user, working directory
  • Retrieve memory usage, processor count, and tick count
  • CLI argument support

๐Ÿงช Example Usage

use SamLuvanda\System\Environment;

echo Environment::phpVersion();          // "8.3.1"
echo Environment::getEnv('APP_ENV');     // "production"
echo Environment::cwd();                 // "/var/www/html"

๐Ÿงฉ Method Reference

getEnv(string $key, mixed $default = null): mixed

Returns the value of an environment variable or the default value if it's not set.

Example:

Environment::getEnv('APP_ENV', 'production');

setEnv(string $key, string $value): void

Sets an environment variable.

Example:

Environment::setEnv('APP_ENV', 'local');

hasEnv(string $key): bool

Checks whether an environment variable exists.

Example:

Environment::hasEnv('APP_ENV');

allEnv(): array

Returns all environment variables as an associative array.

Example:

Environment::allEnv();

machineName(): string

Returns the name of the current machine/host.

Example:

Environment::machineName();

userName(): string

Returns the name of the user running the PHP script.

Example:

Environment::userName();

userDomainName(): ?string

Returns the user domain name if available (Windows-specific).

Example:

Environment::userDomainName();

osVersion(): string

Returns the full operating system information string.

Example:

Environment::osVersion();

processorCount(): ?int

Returns the number of processor cores.

Example:

Environment::processorCount();

exit(int $code = 0): void

Terminates script execution with the specified exit code.

Example:

Environment::exit(1);

cwd(): string

Returns the current working directory.

Example:

Environment::cwd();

newLine(): string

Returns the newline character(s) for the current OS.

Example:

echo 'Line 1' . Environment::newLine() . 'Line 2';

phpVersion(): string

Returns the current PHP version.

Example:

Environment::phpVersion();

cliArgs(): array

Returns command-line arguments passed to the script.

Example:

print_r(Environment::cliArgs());

workingSet(): int

Returns the amount of memory allocated to the script in bytes.

Example:

Environment::workingSet();

tickCount(): int

Returns the number of milliseconds elapsed since script start.

Example:

Environment::tickCount();

๐Ÿ“ Directory Structure

src/
โ””โ”€โ”€ System/
    โ””โ”€โ”€ Environment.php

๐Ÿค Contributing

Contributions are welcome! To contribute:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature-name)
  3. Commit your changes (git commit -m "Add feature")
  4. Push to the branch (git push origin feature-name)
  5. Open a pull request

Please follow PSR-12 coding standards and ensure all code is tested.

๐Ÿ“„ License

This project is open-sourced under the MIT license.