samluvanda / system
A PHP utility library for environment variables, user context, and system information.
Requires
- php: >=8.0
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:
- Fork the repository
- Create a new branch (
git checkout -b feature-name
) - Commit your changes (
git commit -m "Add feature"
) - Push to the branch (
git push origin feature-name
) - 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.