summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.uk>2025-09-11 08:29:09 +0100
committerGitHub <noreply@github.com>2025-09-11 08:29:09 +0100
commit6c36294561d35ae2510721f37ee57ededcdf93ea (patch)
treef89d67976c5efdb009cd64f3d3fec7e7490e7885 /includes
parentae6f3235397c3e13e835d6eff9d2047ea6acf9f4 (diff)
parent036bc99e974d91af0938544128c32df07eeb6e9a (diff)
downloadkernel-6c36294561d35ae2510721f37ee57ededcdf93ea.tar.gz
kernel-6c36294561d35ae2510721f37ee57ededcdf93ea.tar.bz2
kernel-6c36294561d35ae2510721f37ee57ededcdf93ea.zip
Merge pull request #1 from BitweaverCMS/master
add BitCli and BitLogger
Diffstat (limited to 'includes')
-rw-r--r--includes/classes/BitCli.php110
-rw-r--r--includes/classes/BitLogger.php121
2 files changed, 231 insertions, 0 deletions
diff --git a/includes/classes/BitCli.php b/includes/classes/BitCli.php
new file mode 100644
index 0000000..99d138a
--- /dev/null
+++ b/includes/classes/BitCli.php
@@ -0,0 +1,110 @@
+<?php
+/**
+ * @package kernel
+ * @author spiderr <spiderr@bitweaver.org>
+ * Copyright (c) 2025 bitweaver.org, All Rights Reserved
+ * This source file is subject to the 2.0 GNU GENERAL PUBLIC LICENSE.
+ *
+ * BitCliArgs: Simplified CLI Argument Processing Library
+ * Supports long options, defaults, and help output
+ */
+
+/**
+ * Class to manage CLI arguments
+ */
+class BitCliArgs {
+ private $options = [];
+ private $descriptions = [];
+ private $defaults = [];
+ private $scriptName;
+
+ /**
+ * Initialize with script name
+ * @param string $scriptName Name of the script for usage output
+ */
+ public function __construct(string $scriptName) {
+ $this->scriptName = basename($scriptName);
+ }
+
+ /**
+ * Add an option with long name, description, and optional default value
+ * @param string $long Long option (e.g., 'help')
+ * @param string $description Description for help output
+ * @param mixed $default Default value (null if none)
+ * @param bool $required Whether the option requires a value
+ */
+ public function addOption(string $long, string $description, $default = null, bool $required = false) {
+ $this->options[$long] = $required ? "$long:" : "$long::";
+ $this->descriptions[$long] = $description;
+ $this->defaults[$long] = $default;
+ }
+
+ /**
+ * Parse command-line arguments
+ * @return array Parsed arguments with defaults applied
+ */
+ public function parse(): array {
+ $longOpts = array_values($this->options);
+ $opts = getopt('', $longOpts);
+
+ // Apply defaults for unset options
+ $result = $opts;
+ foreach ($this->defaults as $long => $default) {
+ if (!isset($result[$long])) {
+ $result[$long] = $default;
+ }
+ }
+
+ // Handle help option
+ if (isset($result['help'])) {
+ $this->printHelp();
+ exit(0);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Print usage information
+ */
+ private function printHelp() {
+ echo "Usage: php {$this->scriptName} [options]\n\n";
+ echo "Options:\n";
+ foreach ($this->options as $long => $format) {
+ $optStr = " --$long";
+ if (substr($format, -1) === ':') {
+ $optStr .= " <value>";
+ }
+ $default = $this->defaults[$long] !== null ? "(default: {$this->defaults[$long]})" : '';
+ echo sprintf("%-20s %s %s\n", $optStr, $this->descriptions[$long], $default);
+ }
+ echo "\n";
+ }
+
+ /**
+ * Get environment variables for passing to child processes
+ * @return array Environment variables from $_SERVER
+ */
+ public function getEnvVars(): array {
+ $envVars = [];
+ foreach ($_SERVER as $key => $value) {
+ // Filter out PHP-specific $_SERVER keys
+ if (!in_array($key, ['argc', 'argv', 'SCRIPT_NAME', 'PHP_SELF', 'REQUEST_TIME', 'DOCUMENT_ROOT', 'SERVER_SOFTWARE'])) {
+ if (is_string($value)) {
+ $envVars[$key] = $value;
+ }
+ }
+ }
+ return $envVars;
+ }
+
+ /**
+ * Get all raw command-line arguments (including undefined ones)
+ * @return array Raw arguments from $argv
+ */
+ public function getRawArgs(): array {
+ global $argv;
+ return array_slice($argv, 1); // Skip script name
+ }
+}
+
diff --git a/includes/classes/BitLogger.php b/includes/classes/BitLogger.php
new file mode 100644
index 0000000..28a52af
--- /dev/null
+++ b/includes/classes/BitLogger.php
@@ -0,0 +1,121 @@
+<?php
+/**
+ * @package kernel
+ * @author spiderr <spiderr@bitweaver.org>
+ * Copyright (c) 2025 bitweaver.org, All Rights Reserved
+ * This source file is subject to the 2.0 GNU GENERAL PUBLIC LICENSE.
+ *
+ * BitLogger: Simple logging library for PHP CLI applications
+ * Supports PSR-3 log levels and site instance context
+ */
+
+/**
+ * Class to handle logging with site instance context
+ */
+class BitLogger {
+ private $logFile;
+ private $instance;
+
+ // PSR-3 log levels
+ const EMERGENCY = 'EMERGENCY';
+ const ALERT = 'ALERT';
+ const CRITICAL = 'CRITICAL';
+ const ERROR = 'ERROR';
+ const WARNING = 'WARNING';
+ const NOTICE = 'NOTICE';
+ const INFO = 'INFO';
+ const DEBUG = 'DEBUG';
+
+ /**
+ * Initialize logger with log file path and site instance
+ * @param string $logFile Path to log file
+ * @param string $instance Site instance identifier
+ */
+ public function __construct(string $logFile, string $instance = 'global') {
+ $this->logFile = $logFile;
+ $this->instance = $instance;
+
+ // Ensure log file directory exists
+ $dir = dirname($logFile);
+ if (!is_dir($dir)) {
+ mkdir($dir, 0755, true);
+ }
+ }
+
+ /**
+ * Log a message with the specified level
+ * @param string $level Log level (e.g., INFO, ERROR)
+ * @param string $message Message to log
+ * @param array $context Additional context (optional)
+ */
+ public function log(string $level, string $message, array $context = []) {
+ $validLevels = [
+ self::EMERGENCY,
+ self::ALERT,
+ self::CRITICAL,
+ self::ERROR,
+ self::WARNING,
+ self::NOTICE,
+ self::INFO,
+ self::DEBUG
+ ];
+
+ // Validate log level
+ $level = strtoupper($level);
+ if (!in_array($level, $validLevels)) {
+ $this->log(self::ERROR, "Invalid log level: $level");
+ return;
+ }
+
+ // Format log message
+ $timestamp = date('Y-m-d H:i:s');
+ $contextStr = $context ? ' ' . json_encode($context) : '';
+ $logLine = "[$timestamp] [{$this->instance}] [$level] $message$contextStr\n";
+
+ // Write to log file (thread-safe)
+ file_put_contents($this->logFile, $logLine, FILE_APPEND | LOCK_EX);
+ }
+
+ // Convenience methods for each log level
+ public function emergency(string $message, array $context = []) {
+ $this->log(self::EMERGENCY, $message, $context);
+ }
+
+ public function alert(string $message, array $context = []) {
+ $this->log(self::ALERT, $message, $context);
+ }
+
+ public function critical(string $message, array $context = []) {
+ $this->log(self::CRITICAL, $message, $context);
+ exit;
+ }
+
+ public function error(string $message, array $context = []) {
+ $this->log(self::ERROR, $message, $context);
+ }
+
+ public function warning(string $message, array $context = []) {
+ $this->log(self::WARNING, $message, $context);
+ }
+
+ public function notice(string $message, array $context = []) {
+ $this->log(self::NOTICE, $message, $context);
+ }
+
+ public function info(string $message, array $context = []) {
+ $this->log(self::INFO, $message, $context);
+ }
+
+ public function debug(string $message, array $context = []) {
+ $this->log(self::DEBUG, $message, $context);
+ }
+}
+
+/* Example usage
+if (php_sapi_name() === 'cli' && basename(__FILE__) === 'logger.php') {
+ $logger = new BitLogger('logs/app.log', 'test_instance');
+ $logger->info('Application started');
+ $logger->warning('Low disk space', ['free_space' => '500MB']);
+ $logger->error('Failed to connect to database', ['error_code' => 1001]);
+}
+*/