parent
6bb6b01104
commit
ad784311c1
@ -1,123 +1,163 @@ |
|||||||
<?php |
<?php
|
||||||
|
/** |
||||||
|
* Neato Auto loader PSR4 |
||||||
|
* |
||||||
|
* PHP version 8.3 |
||||||
|
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author http://php-fig.org/ <info@php-fig.org> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md |
||||||
|
*/ |
||||||
|
|
||||||
/** |
/** |
||||||
* @author http://php-fig.org/ <info@php-fig.org> |
* Auto loader, uses namespace to guess where class file live. |
||||||
* @site https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md |
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author http://php-fig.org/ <info@php-fig.org> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md |
||||||
*/ |
*/ |
||||||
class Psr4AutoloaderClass { |
class Neato_Auto_Loader |
||||||
|
{ |
||||||
|
|
||||||
/** |
/** |
||||||
* An associative array where the key is a namespace prefix and the value |
* An associative array where the key is a namespace prefix and the value |
||||||
* is an array of base directories for classes in that namespace. |
* is an array of base directories for classes in that namespace. |
||||||
* |
* |
||||||
* @var array |
* @var array |
||||||
*/ |
*/ |
||||||
protected $prefixes = []; |
protected $prefixes = []; |
||||||
|
|
||||||
/** |
/** |
||||||
* Register loader with SPL autoloader stack. |
* Register loader with SPL autoloader stack. |
||||||
* |
* |
||||||
* @return void |
* @return void |
||||||
*/ |
*/ |
||||||
public function register() { |
public function register() |
||||||
spl_autoload_register(array($this, 'load_class')); |
{ |
||||||
} |
spl_autoload_register(array($this, 'loadClass')); |
||||||
|
} |
||||||
|
|
||||||
public function is_loaded(string $prefix): bool { |
/** |
||||||
$prefix = trim($prefix, '\\') . '\\'; |
* Is class loaded, yet? |
||||||
return (isset($this->prefixes[$prefix])) ? true : false; |
* |
||||||
} |
* @param string $prefix holds global data of namespaces |
||||||
|
* |
||||||
public function get_list(): array { |
* @return bool |
||||||
return $this->prefixes; |
*/ |
||||||
} |
public function isLoaded(string $prefix): bool |
||||||
|
{ |
||||||
|
$prefix = trim($prefix, '\\') . '\\'; |
||||||
|
return (isset($this->prefixes[$prefix])) ? true : false; |
||||||
|
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Adds a base directory for a namespace prefix. |
* Get List of loaded prefixes containing namespace/class. |
||||||
* |
* |
||||||
* @param string $prefix The namespace prefix. |
* @return array |
||||||
* @param string $base_dir A base directory for class files in the |
*/ |
||||||
* namespace. |
public function getList(): array |
||||||
* @param bool $prepend If true, prepend the base directory to the stack |
{ |
||||||
* instead of appending it; this causes it to be searched first rather |
return $this->prefixes; |
||||||
* than last. |
|
||||||
* @return void |
|
||||||
*/ |
|
||||||
public function add_namespace(string $prefix, string $base_dir, bool $prepend = false): void { |
|
||||||
$prefix = trim($prefix, '\\') . '\\'; |
|
||||||
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; |
|
||||||
if (isset($this->prefixes[$prefix]) === false) { |
|
||||||
$this->prefixes[$prefix] = array(); |
|
||||||
} |
} |
||||||
if ($prepend) { |
|
||||||
array_unshift($this->prefixes[$prefix], $base_dir); |
/** |
||||||
} else { |
* Adds a base directory for a namespace prefix. |
||||||
array_push($this->prefixes[$prefix], $base_dir); |
* |
||||||
|
* @param string $prefix The namespace prefix. |
||||||
|
* @param string $base_dir A base directory for class files in the |
||||||
|
* namespace. |
||||||
|
* @param bool $prepend If true, prepend the base directory to the stack |
||||||
|
* instead of appending it; this causes it to be |
||||||
|
* searched first rather than last. |
||||||
|
* |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
public function addNamespace(string $prefix, string $base_dir, bool $prepend = false): void |
||||||
|
{ |
||||||
|
$prefix = trim($prefix, '\\') . '\\'; |
||||||
|
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; |
||||||
|
if (isset($this->prefixes[$prefix]) === false) { |
||||||
|
$this->prefixes[$prefix] = array(); |
||||||
|
} |
||||||
|
if ($prepend) { |
||||||
|
array_unshift($this->prefixes[$prefix], $base_dir); |
||||||
|
} else { |
||||||
|
array_push($this->prefixes[$prefix], $base_dir); |
||||||
|
} |
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Loads the class file for a given class name. |
|
||||||
* |
|
||||||
* @param string $class The fully-qualified class name. |
|
||||||
* @return mixed The mapped file name on success, or boolean false on |
|
||||||
* failure. |
|
||||||
*/ |
|
||||||
public function load_class(string $class) { |
|
||||||
/** |
/** |
||||||
* Semi-Fix for non-namespaced classes |
* Loads the class file for a given class name. |
||||||
|
* |
||||||
|
* @param string $class The fully-qualified class name. |
||||||
|
* |
||||||
|
* @return mixed The mapped file name on success, or boolean false on |
||||||
|
* failure. |
||||||
*/ |
*/ |
||||||
if (! strrpos($class, '\\')) { |
public function loadClass(string $class) |
||||||
return ($this->load_mapped_file($class . '\\', $class)); |
{ |
||||||
} |
/** |
||||||
$prefix = $class; |
* Semi-Fix for non-namespaced classes |
||||||
while (false !== $pos = strrpos($prefix, '\\')) { |
*/ |
||||||
$prefix = substr($class, 0, $pos + 1); |
if (! strrpos($class, '\\')) { |
||||||
$relative_class = substr($class, $pos + 1); |
return ($this->loadMappedFile($class . '\\', $class)); |
||||||
$mapped_file = $this->load_mapped_file($prefix, $relative_class); |
} |
||||||
if ($mapped_file) { |
$prefix = $class; |
||||||
return $mapped_file; |
while (false !== $pos = strrpos($prefix, '\\')) { |
||||||
} |
$prefix = substr($class, 0, $pos + 1); |
||||||
$prefix = rtrim($prefix, '\\'); |
$relative_class = substr($class, $pos + 1); |
||||||
|
$mapped_file = $this->loadMappedFile($prefix, $relative_class); |
||||||
|
if ($mapped_file) { |
||||||
|
return $mapped_file; |
||||||
|
} |
||||||
|
$prefix = rtrim($prefix, '\\'); |
||||||
|
} |
||||||
|
return false; |
||||||
} |
} |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Load the mapped file for a namespace prefix and relative class. |
* Load the mapped file for a namespace prefix and relative class. |
||||||
* |
* |
||||||
* @param string $prefix The namespace prefix. |
* @param string $prefix The namespace prefix. |
||||||
* @param string $relative_class The relative class name. |
* @param string $relative_class The relative class name. |
||||||
* @return mixed Boolean false if no mapped file can be loaded, or the |
* |
||||||
* name of the mapped file that was loaded. |
* @return mixed Boolean false if no mapped file can be loaded, or the |
||||||
*/ |
* name of the mapped file that was loaded. |
||||||
protected function load_mapped_file(string $prefix, string $relative_class) { |
*/ |
||||||
if (isset($this->prefixes[$prefix]) === false) { |
protected function loadMappedFile(string $prefix, string $relative_class) |
||||||
return false; |
{ |
||||||
} |
if (isset($this->prefixes[$prefix]) === false) { |
||||||
foreach ($this->prefixes[$prefix] as $base_dir) { |
return false; |
||||||
$file = $base_dir |
} |
||||||
. str_replace('\\', '/', $relative_class) |
foreach ($this->prefixes[$prefix] as $base_dir) { |
||||||
. '.php'; |
$file = $base_dir |
||||||
if ($this->require_file($file)) { |
. str_replace('\\', '/', $relative_class) |
||||||
return $file; |
. '.php'; |
||||||
} |
if ($this->requireFile($file)) { |
||||||
|
return $file; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
} |
} |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* If a file exists, require it from the file system. |
* If a file exists, require it from the file system. |
||||||
* |
* |
||||||
* @param string $file The file to require. |
* @param string $file The file to require. |
||||||
* @return bool True if the file exists, false if not. |
* |
||||||
*/ |
* @return bool True if the file exists, false if not. |
||||||
protected function require_file(string $file): bool { |
*/ |
||||||
if (file_exists($file)) { |
protected function requireFile(string $file): bool |
||||||
require $file; |
{ |
||||||
return true; |
if (file_exists($file)) { |
||||||
|
include $file; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
} |
} |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,75 +1,105 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
function getTermColors($input, $options) { |
/** |
||||||
|
* ANSI Colors for the Terminal Console |
||||||
|
* |
||||||
|
* PHP version 8.3 |
||||||
|
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author Robert S. <tips@technowizardbob.com> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
|
*/ |
||||||
|
|
||||||
$colored_string = ""; |
/** |
||||||
|
* Function getTermColors make ANSI color codes |
||||||
|
* |
||||||
|
* @param array|string $input text to display |
||||||
|
* @param mixed $options colors to use |
||||||
|
* |
||||||
|
* @return string ANSI text |
||||||
|
*/ |
||||||
|
function getTermColors(array|string $input, $options): string |
||||||
|
{ |
||||||
|
|
||||||
$styles = [ |
$colored_string = ""; |
||||||
'normal' => '0', // reset |
|
||||||
'bold' => '1', |
|
||||||
'dim' => '2', |
|
||||||
'underlined' => '4', |
|
||||||
'blinking' => '5' |
|
||||||
]; |
|
||||||
|
|
||||||
$fg_colors = [ |
$styles = [ |
||||||
'black' => '0;30', |
'normal' => '0', // reset |
||||||
'dark_gray' => '1;30', |
'bold' => '1', |
||||||
'blue' => '0;34', |
'dim' => '2', |
||||||
'light_blue' => '1;34', |
'underlined' => '4', |
||||||
'green' => '0;32', |
'blinking' => '5' |
||||||
'light_green' => '1;32', |
]; |
||||||
'cyan' => '0;36', |
|
||||||
'light_cyan' => '1;36', |
|
||||||
'red' => '0;31', |
|
||||||
'light_red' => '1;31', |
|
||||||
'purple' => '0;35', |
|
||||||
'light_purple' => '1;35', |
|
||||||
'brown' => '0;33', |
|
||||||
'yellow' => '1;33', |
|
||||||
'light_gray' => '0;37', |
|
||||||
'white' => '1;37' |
|
||||||
]; |
|
||||||
|
|
||||||
$bg_colors = [ |
$fg_colors = [ |
||||||
'black' => '40', |
'black' => '0;30', |
||||||
'red' => '41', |
'dark_gray' => '1;30', |
||||||
'green' => '42', |
'blue' => '0;34', |
||||||
'yellow' => '43', |
'light_blue' => '1;34', |
||||||
'blue' => '44', |
'green' => '0;32', |
||||||
'magenta' => '45', |
'light_green' => '1;32', |
||||||
'cyan' => '46', |
'cyan' => '0;36', |
||||||
'light_gray' => '47' |
'light_cyan' => '1;36', |
||||||
]; |
'red' => '0;31', |
||||||
|
'light_red' => '1;31', |
||||||
|
'purple' => '0;35', |
||||||
|
'light_purple' => '1;35', |
||||||
|
'brown' => '0;33', |
||||||
|
'yellow' => '1;33', |
||||||
|
'light_gray' => '0;37', |
||||||
|
'white' => '1;37' |
||||||
|
]; |
||||||
|
|
||||||
$style = (isset($options['style'])) ? strtolower($options['style']) : ''; |
$bg_colors = [ |
||||||
$color = (isset($options['color'])) ? strtolower($options['color']) : ''; |
'black' => '40', |
||||||
$fg_color = (isset($options['fg_color'])) ? strtolower($options['fg_color']) : $color; |
'red' => '41', |
||||||
$bg_color = (isset($options['bg_color'])) ? strtolower($options['bg_color']) : ''; |
'green' => '42', |
||||||
|
'yellow' => '43', |
||||||
if ($style !== '' && isset($styles[$style])) { |
'blue' => '44', |
||||||
$colored_string .= "\033[" . $styles[$style] . "m"; |
'magenta' => '45', |
||||||
} |
'cyan' => '46', |
||||||
if ($fg_color !== '' && isset($fg_colors[$fg_color])) { |
'light_gray' => '47' |
||||||
$colored_string .= "\033[" . $fg_colors[$fg_color] . "m"; |
]; |
||||||
} |
|
||||||
if ($bg_color !== '' && isset($bg_colors[$bg_color])) { |
$style = (isset($options['style'])) ? strtolower($options['style']) : ''; |
||||||
$colored_string .= "\033[" . $bg_colors[$bg_color] . "m"; |
$color = (isset($options['color'])) ? strtolower($options['color']) : ''; |
||||||
} |
$fg_color = (isset($options['fg_color'])) ? strtolower($options['fg_color']) : $color; |
||||||
|
$bg_color = (isset($options['bg_color'])) ? strtolower($options['bg_color']) : ''; |
||||||
|
|
||||||
|
if ($style !== '' && isset($styles[$style])) { |
||||||
|
$colored_string .= "\033[" . $styles[$style] . "m"; |
||||||
|
} |
||||||
|
if ($fg_color !== '' && isset($fg_colors[$fg_color])) { |
||||||
|
$colored_string .= "\033[" . $fg_colors[$fg_color] . "m"; |
||||||
|
} |
||||||
|
if ($bg_color !== '' && isset($bg_colors[$bg_color])) { |
||||||
|
$colored_string .= "\033[" . $bg_colors[$bg_color] . "m"; |
||||||
|
} |
||||||
|
|
||||||
$str = ''; |
$str = ''; |
||||||
if (is_array($input)) { |
if (is_array($input)) { |
||||||
foreach ($input as $s) { |
foreach ($input as $s) { |
||||||
$str .= $s . PHP_EOL; |
$str .= $s . PHP_EOL; |
||||||
} |
} |
||||||
} else { |
} else { |
||||||
$str = $input; |
$str = $input; |
||||||
} |
} |
||||||
|
|
||||||
$colored_string .= $str . "\033[0m"; |
$colored_string .= $str . "\033[0m"; |
||||||
return $colored_string; |
return $colored_string; |
||||||
} |
} |
||||||
|
|
||||||
function ANSI($data, $a) { |
/** |
||||||
getTermColors($data, $a); |
* Alias to getTermColors |
||||||
|
* |
||||||
|
* @param array|string $data text to display |
||||||
|
* @param mixed $a options for colors |
||||||
|
* |
||||||
|
* @return string of ANSI |
||||||
|
*/ |
||||||
|
function ANSI(array|string $data, $a): string |
||||||
|
{ |
||||||
|
return getTermColors($data, $a); |
||||||
} |
} |
||||||
|
|||||||
@ -1,111 +1,152 @@ |
|||||||
<?php |
<?php |
||||||
/** @phpstan-ignore-next-line Variable $cwd might not be defined */ |
/** |
||||||
|
* File Logger |
||||||
|
* |
||||||
|
* PHP version 8.3 |
||||||
|
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author Robert S. <tips@technowizardbob.com> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* $cwd is defined in neato.php |
||||||
|
* |
||||||
|
* @phpstan-ignore-next-line Variable $cwd might not be defined |
||||||
|
*/ |
||||||
define('PROJECT_LOGS_DIR', $cwd. '/log'); |
define('PROJECT_LOGS_DIR', $cwd. '/log'); |
||||||
|
|
||||||
class logger { |
/** |
||||||
|
* Logs display data |
||||||
|
|
||||||
|
* @param mixed $input text to display |
||||||
|
* @param mixed $options colors to use |
||||||
|
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author Robert S. <tips@technowizardbob.com> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
|
* |
||||||
|
* @return string ANSI text |
||||||
|
*/ |
||||||
|
|
||||||
private $handle; |
class Logger |
||||||
|
{ |
||||||
|
|
||||||
|
private $_handle; |
||||||
|
|
||||||
/** |
/** |
||||||
* Get Filename for writing to Log file |
* Get Filename for writing to Log file |
||||||
* @param string $filename for log |
* |
||||||
* @param int $max_count of lines before file wipe, to keep small logs. |
* @param string $filename for log |
||||||
*/ |
* @param int $max_count of lines before file wipe, to keep small logs. |
||||||
public function __construct(string $filename = 'neato_Installer', int $max_count = 1000) { |
*/ |
||||||
if (strpos($filename, "..") !== false) { |
public function __construct(string $filename = 'neato_Installer', int $max_count = 1000) |
||||||
$this->handle = false; // Too dangerious, so return false |
{ |
||||||
} else { |
if (strpos($filename, "..") !== false) { |
||||||
$filename = str_replace("php", "", $filename); |
$this->_handle = false; // Too dangerious, so return false |
||||||
if (! is_dir(PROJECT_LOGS_DIR)){ |
} else { |
||||||
//Directory does not exist, so lets create it. |
$filename = str_replace("php", "", $filename); |
||||||
mkdir(PROJECT_LOGS_DIR, 0775); |
if (! is_dir(PROJECT_LOGS_DIR)) { |
||||||
} |
//Directory does not exist, so lets create it. |
||||||
|
mkdir(PROJECT_LOGS_DIR, 0775); |
||||||
|
} |
||||||
|
|
||||||
$filename = preg_replace("|[^A-Za-z0-9_]|", "", $filename); |
$filename = preg_replace("|[^A-Za-z0-9_]|", "", $filename); |
||||||
$filename = escapeshellcmd($filename); |
$filename = escapeshellcmd($filename); |
||||||
$file = PROJECT_LOGS_DIR . '/' . $filename . ".log.txt"; |
$file = PROJECT_LOGS_DIR . '/' . $filename . ".log.txt"; |
||||||
|
|
||||||
if ($max_count > 1) { |
if ($max_count > 1) { |
||||||
if ($this->get_lines($file) > $max_count) { |
if ($this->getLines($file) > $max_count) { |
||||||
unlink($file); |
unlink($file); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
$success = touch($file); |
$success = touch($file); |
||||||
if ($success === false) { |
if ($success === false) { |
||||||
$this->handle = false; |
$this->_handle = false; |
||||||
throw new \Exception('Unable to touch file:' . $file); |
throw new \Exception('Unable to touch file:' . $file); |
||||||
} |
} |
||||||
chmod($file, 0660); |
chmod($file, 0660); |
||||||
if (! is_writable($file)) { |
if (! is_writable($file)) { |
||||||
$this->handle = false; |
$this->_handle = false; |
||||||
throw new \Exception('Unable to write to file:' . $file); |
throw new \Exception('Unable to write to file:' . $file); |
||||||
} |
} |
||||||
$this->handle = fopen($file, 'a'); |
$this->_handle = fopen($file, 'a'); |
||||||
|
} |
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Count number of lines in Log File |
* Count number of lines in Log File |
||||||
* @param string $file |
* |
||||||
* @return int line count |
* @param string $file filename to check how big it is |
||||||
*/ |
* |
||||||
public function get_lines(string $file): int { |
* @return int line count |
||||||
// No such file, so return zero for length. |
*/ |
||||||
if (! file_exists($file)) { |
public function getLines(string $file): int |
||||||
return 0; |
{ |
||||||
} |
// No such file, so return zero for length. |
||||||
|
if (! file_exists($file)) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
$f = fopen($file, 'rb'); |
$f = fopen($file, 'rb'); |
||||||
$lines = 0; |
$lines = 0; |
||||||
|
|
||||||
if ($f === false || !is_resource($f)) { |
if ($f === false || !is_resource($f)) { |
||||||
return 0; |
return 0; |
||||||
} |
} |
||||||
|
|
||||||
while (!feof($f)) { |
while (!feof($f)) { |
||||||
$line = fread($f, 8192); |
$line = fread($f, 8192); |
||||||
if ($line === false) { |
if ($line === false) { |
||||||
return 0; |
return 0; |
||||||
} |
} |
||||||
$lines += substr_count($line, "\n"); |
$lines += substr_count($line, "\n"); |
||||||
} |
} |
||||||
|
|
||||||
fclose($f); |
fclose($f); |
||||||
|
|
||||||
return $lines; |
return $lines; |
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Write to Log File |
|
||||||
* @param string $message to save |
|
||||||
* @return bool able to write to log file |
|
||||||
*/ |
|
||||||
public function write(string $message): bool { |
|
||||||
if ( $this->handle === false || ! is_resource($this->handle) ) { |
|
||||||
return false; |
|
||||||
} |
} |
||||||
$tz = configure::get('logger_time_zone'); |
|
||||||
if ($tz !== false && !empty($tz)) { |
/** |
||||||
$tz_obj = new \DateTimeZone($tz); |
* Write to Log File |
||||||
$dt = new \DateTime(); |
* |
||||||
$dt->setTimezone($tz_obj); |
* @param string $message to save |
||||||
$now = $dt->format('g:i A \o\n l jS F Y'); |
* |
||||||
} else { |
* @return bool able to write to log file |
||||||
$dt = new \DateTime(); |
*/ |
||||||
$now = $dt->format('g:i A \o\n l jS F Y'); |
public function write(string $message): bool |
||||||
|
{ |
||||||
|
if ($this->_handle === false || ! is_resource($this->_handle) ) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
$tz = configure::get('logger_time_zone'); |
||||||
|
if ($tz !== false && !empty($tz)) { |
||||||
|
$tz_obj = new \DateTimeZone($tz); |
||||||
|
$dt = new \DateTime(); |
||||||
|
$dt->setTimezone($tz_obj); |
||||||
|
$now = $dt->format('g:i A \o\n l jS F Y'); |
||||||
|
} else { |
||||||
|
$dt = new \DateTime(); |
||||||
|
$now = $dt->format('g:i A \o\n l jS F Y'); |
||||||
|
} |
||||||
|
fwrite($this->_handle, $now . ' - ' . print_r($message, true) . "\n"); |
||||||
|
return true; |
||||||
} |
} |
||||||
fwrite($this->handle, $now . ' - ' . print_r($message, true) . "\n"); |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Close Log File Handle |
* Close Log File Handle |
||||||
*/ |
*/ |
||||||
public function __destruct() { |
public function __destruct() |
||||||
if ($this->handle !== false && is_resource($this->handle)) { |
{ |
||||||
fclose($this->handle); |
if ($this->_handle !== false && is_resource($this->_handle)) { |
||||||
|
fclose($this->_handle); |
||||||
|
} |
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
} // end of Logger |
} // end of Logger |
||||||
|
|||||||
@ -1,69 +1,178 @@ |
|||||||
<?php
|
<?php
|
||||||
|
/** |
||||||
|
* Neato Auto loader PSR4 |
||||||
|
* |
||||||
|
* PHP version 8.3 |
||||||
|
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author Robert S. <tips@technowizardbob.com> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
|
*/ |
||||||
|
|
||||||
final class registry { |
/** |
||||||
private static $registry = []; |
* Class Registry to hold di |
||||||
protected function __construct() { } |
* |
||||||
|
* @category Util |
||||||
|
* @package Neato |
||||||
|
* @author Robert S. <tips@technowizardbob.com> |
||||||
|
* @license https://mit-license.org/ MIT License |
||||||
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
|
*/ |
||||||
|
final class Neato_Registry |
||||||
|
{ |
||||||
|
private static $_registry = []; |
||||||
|
|
||||||
|
/** |
||||||
|
* Protected constructor to stop instances from being created. |
||||||
|
*/ |
||||||
|
protected function __construct() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
public static function get(string $name, $key = false) { |
/** |
||||||
if (isset(self::$registry[strtolower($name)])) { |
* Retrieves data from the registry. |
||||||
$a = self::$registry[strtolower($name)]; |
* |
||||||
if ($key === false) { |
* @param string $name item label to fetch |
||||||
return $a; |
* @param string|false $key is like a subkey for another item |
||||||
} |
* |
||||||
if (isset($a[$key])) { |
* @return mixed |
||||||
return $a[$key]; |
*/ |
||||||
} |
public static function get(string $name, string|false $key = false) |
||||||
|
{ |
||||||
|
if (isset(self::$_registry[strtolower($name)])) { |
||||||
|
$a = self::$_registry[strtolower($name)]; |
||||||
|
if ($key === false) { |
||||||
|
return $a; |
||||||
|
} |
||||||
|
if (isset($a[$key])) { |
||||||
|
return $a[$key]; |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
} |
} |
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
public static function set(string $name, $value): bool { |
/** |
||||||
if (array_key_exists(strtolower($name), self::$registry)) { |
* Assigns data into the registry to be used anywhere in the app. |
||||||
return false; |
* |
||||||
|
* @param string $name label for item to set into registry. |
||||||
|
* @param mixed $value data to put into registry. |
||||||
|
* |
||||||
|
* @return bool |
||||||
|
*/ |
||||||
|
public static function set(string $name, mixed $value): bool |
||||||
|
{ |
||||||
|
if (array_key_exists(strtolower($name), self::$_registry)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
self::$_registry[strtolower($name)] = $value; |
||||||
|
return true; |
||||||
} |
} |
||||||
self::$registry[strtolower($name)] = $value; |
|
||||||
return true; |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
final class di { |
/** |
||||||
protected $services = []; |
* Class Di dependency injection. |
||||||
|
* |
||||||
public function register(string $service_name, callable $callable): void { |
* @category Util |
||||||
$this->services[$service_name] = $callable; |
* @package Neato |
||||||
} |
* @author Robert S. <tips@technowizardbob.com> |
||||||
// Note args may be an object or an array maybe more...! |
* @license https://mit-license.org/ MIT License |
||||||
public function get_service(string $service_name, $args = []) { |
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy |
||||||
if (!array_key_exists($service_name, $this->services)) { |
*/ |
||||||
throw new \Exception("The Service: {$service_name} does not exists."); |
|
||||||
|
final class Di |
||||||
|
{ |
||||||
|
/** |
||||||
|
* Holds protected services in an array. |
||||||
|
* |
||||||
|
* @var array $services holds everything for di |
||||||
|
*/ |
||||||
|
protected array $services = []; |
||||||
|
/** |
||||||
|
* Set's registry up for useage. |
||||||
|
* |
||||||
|
* @param string $service_name label for callable code |
||||||
|
* @param callable $callable method to use |
||||||
|
* |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
public function register(string $service_name, callable $callable): void |
||||||
|
{ |
||||||
|
$this->services[$service_name] = $callable; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get service data |
||||||
|
* Note args may be an object or an array maybe more...! |
||||||
|
* |
||||||
|
* @param string $service_name label for service |
||||||
|
* @param mixed $args the parameters to use |
||||||
|
* |
||||||
|
* @return mixed code injected for later use. |
||||||
|
* @throws \Exception |
||||||
|
*/ |
||||||
|
public function getService(string $service_name, $args = []) |
||||||
|
{ |
||||||
|
if (!array_key_exists($service_name, $this->services)) { |
||||||
|
throw new \Exception("The Service: {$service_name} does not exists."); |
||||||
|
} |
||||||
|
return $this->services[$service_name]($args); |
||||||
} |
} |
||||||
return $this->services[$service_name]($args); |
|
||||||
} |
|
||||||
|
|
||||||
public function __set(string $service_name, callable $callable): void { |
/** |
||||||
$this->register($service_name, $callable); |
* Majic setter, service to be registered. |
||||||
} |
* |
||||||
|
* @param string $service_name label for service |
||||||
public function __get(string $service_name) { |
* @param callable $callable code injected for later use |
||||||
return $this->get_service($service_name); |
* |
||||||
} |
* @return void |
||||||
|
*/ |
||||||
|
public function __set(string $service_name, callable $callable): void |
||||||
|
{ |
||||||
|
$this->register($service_name, $callable); |
||||||
|
} |
||||||
|
|
||||||
public function list_services_as_array(): array { |
/** |
||||||
return array_keys($this->services); |
* Magic getter method same as get service. |
||||||
} |
* |
||||||
|
* @param string $service_name label for service |
||||||
|
* |
||||||
|
* @return mixed code injected for later use. |
||||||
|
*/ |
||||||
|
public function __get(string $service_name) |
||||||
|
{ |
||||||
|
return $this->getService($service_name); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Lists all services as an array. |
||||||
|
* |
||||||
|
* @return array of services that can be used |
||||||
|
*/ |
||||||
|
public function listServicesAsArray(): array |
||||||
|
{ |
||||||
|
return array_keys($this->services); |
||||||
|
} |
||||||
|
|
||||||
public function list_services_as_string(): string { |
/** |
||||||
return implode(',', array_keys($this->services)); |
* Lists all services as a string. |
||||||
} |
* |
||||||
|
* @return string of services |
||||||
|
*/ |
||||||
|
public function listServicesAsString(): string |
||||||
|
{ |
||||||
|
return implode(',', array_keys($this->services)); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
// Initialize our Dependency Injector |
// Initialize our Dependency Injector |
||||||
registry::set('di', new di()); |
Neato_Registry::set('di', new Di()); |
||||||
|
|
||||||
// Setup php for working with Unicode data, if possible |
// Setup php for working with Unicode data, if possible |
||||||
if (extension_loaded('mbstring')) { |
if (extension_loaded('mbstring')) { |
||||||
mb_internal_encoding('UTF-8'); |
mb_internal_encoding('UTF-8'); |
||||||
mb_http_output('UTF-8'); |
mb_http_output('UTF-8'); |
||||||
mb_language('uni'); |
mb_language('uni'); |
||||||
setlocale(LC_ALL, "en_US.UTF-8"); |
setlocale(LC_ALL, "en_US.UTF-8"); |
||||||
} |
} |
||||||
Loading…
Reference in new issue