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> |
||||
* @site https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md |
||||
* Auto loader, uses namespace to guess where class file live. |
||||
* |
||||
* @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 |
||||
* is an array of base directories for classes in that namespace. |
||||
* |
||||
* @var array |
||||
*/ |
||||
protected $prefixes = []; |
||||
/** |
||||
* An associative array where the key is a namespace prefix and the value |
||||
* is an array of base directories for classes in that namespace. |
||||
* |
||||
* @var array |
||||
*/ |
||||
protected $prefixes = []; |
||||
|
||||
/** |
||||
* Register loader with SPL autoloader stack. |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function register() { |
||||
spl_autoload_register(array($this, 'load_class')); |
||||
} |
||||
/** |
||||
* Register loader with SPL autoloader stack. |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function register() |
||||
{ |
||||
spl_autoload_register(array($this, 'loadClass')); |
||||
} |
||||
|
||||
public function is_loaded(string $prefix): bool { |
||||
$prefix = trim($prefix, '\\') . '\\'; |
||||
return (isset($this->prefixes[$prefix])) ? true : false; |
||||
} |
||||
|
||||
public function get_list(): array { |
||||
return $this->prefixes; |
||||
} |
||||
/** |
||||
* Is class loaded, yet? |
||||
* |
||||
* @param string $prefix holds global data of namespaces |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function isLoaded(string $prefix): bool |
||||
{ |
||||
$prefix = trim($prefix, '\\') . '\\'; |
||||
return (isset($this->prefixes[$prefix])) ? true : false; |
||||
} |
||||
|
||||
/** |
||||
* Adds a base directory for a namespace prefix. |
||||
* |
||||
* @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 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(); |
||||
/** |
||||
* Get List of loaded prefixes containing namespace/class. |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getList(): array |
||||
{ |
||||
return $this->prefixes; |
||||
} |
||||
if ($prepend) { |
||||
array_unshift($this->prefixes[$prefix], $base_dir); |
||||
} else { |
||||
array_push($this->prefixes[$prefix], $base_dir); |
||||
|
||||
/** |
||||
* Adds a base directory for a namespace prefix. |
||||
* |
||||
* @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, '\\')) { |
||||
return ($this->load_mapped_file($class . '\\', $class)); |
||||
} |
||||
$prefix = $class; |
||||
while (false !== $pos = strrpos($prefix, '\\')) { |
||||
$prefix = substr($class, 0, $pos + 1); |
||||
$relative_class = substr($class, $pos + 1); |
||||
$mapped_file = $this->load_mapped_file($prefix, $relative_class); |
||||
if ($mapped_file) { |
||||
return $mapped_file; |
||||
} |
||||
$prefix = rtrim($prefix, '\\'); |
||||
public function loadClass(string $class) |
||||
{ |
||||
/** |
||||
* Semi-Fix for non-namespaced classes |
||||
*/ |
||||
if (! strrpos($class, '\\')) { |
||||
return ($this->loadMappedFile($class . '\\', $class)); |
||||
} |
||||
$prefix = $class; |
||||
while (false !== $pos = strrpos($prefix, '\\')) { |
||||
$prefix = substr($class, 0, $pos + 1); |
||||
$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. |
||||
* |
||||
* @param string $prefix The namespace prefix. |
||||
* @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. |
||||
*/ |
||||
protected function load_mapped_file(string $prefix, string $relative_class) { |
||||
if (isset($this->prefixes[$prefix]) === false) { |
||||
return false; |
||||
} |
||||
foreach ($this->prefixes[$prefix] as $base_dir) { |
||||
$file = $base_dir |
||||
. str_replace('\\', '/', $relative_class) |
||||
. '.php'; |
||||
if ($this->require_file($file)) { |
||||
return $file; |
||||
} |
||||
/** |
||||
* Load the mapped file for a namespace prefix and relative class. |
||||
* |
||||
* @param string $prefix The namespace prefix. |
||||
* @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. |
||||
*/ |
||||
protected function loadMappedFile(string $prefix, string $relative_class) |
||||
{ |
||||
if (isset($this->prefixes[$prefix]) === false) { |
||||
return false; |
||||
} |
||||
foreach ($this->prefixes[$prefix] as $base_dir) { |
||||
$file = $base_dir |
||||
. str_replace('\\', '/', $relative_class) |
||||
. '.php'; |
||||
if ($this->requireFile($file)) { |
||||
return $file; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* If a file exists, require it from the file system. |
||||
* |
||||
* @param string $file The file to require. |
||||
* @return bool True if the file exists, false if not. |
||||
*/ |
||||
protected function require_file(string $file): bool { |
||||
if (file_exists($file)) { |
||||
require $file; |
||||
return true; |
||||
/** |
||||
* If a file exists, require it from the file system. |
||||
* |
||||
* @param string $file The file to require. |
||||
* |
||||
* @return bool True if the file exists, false if not. |
||||
*/ |
||||
protected function requireFile(string $file): bool |
||||
{ |
||||
if (file_exists($file)) { |
||||
include $file; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -1,75 +1,105 @@ |
||||
<?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 = [ |
||||
'normal' => '0', // reset |
||||
'bold' => '1', |
||||
'dim' => '2', |
||||
'underlined' => '4', |
||||
'blinking' => '5' |
||||
]; |
||||
$colored_string = ""; |
||||
|
||||
$fg_colors = [ |
||||
'black' => '0;30', |
||||
'dark_gray' => '1;30', |
||||
'blue' => '0;34', |
||||
'light_blue' => '1;34', |
||||
'green' => '0;32', |
||||
'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' |
||||
]; |
||||
$styles = [ |
||||
'normal' => '0', // reset |
||||
'bold' => '1', |
||||
'dim' => '2', |
||||
'underlined' => '4', |
||||
'blinking' => '5' |
||||
]; |
||||
|
||||
$bg_colors = [ |
||||
'black' => '40', |
||||
'red' => '41', |
||||
'green' => '42', |
||||
'yellow' => '43', |
||||
'blue' => '44', |
||||
'magenta' => '45', |
||||
'cyan' => '46', |
||||
'light_gray' => '47' |
||||
]; |
||||
$fg_colors = [ |
||||
'black' => '0;30', |
||||
'dark_gray' => '1;30', |
||||
'blue' => '0;34', |
||||
'light_blue' => '1;34', |
||||
'green' => '0;32', |
||||
'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' |
||||
]; |
||||
|
||||
$style = (isset($options['style'])) ? strtolower($options['style']) : ''; |
||||
$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"; |
||||
} |
||||
$bg_colors = [ |
||||
'black' => '40', |
||||
'red' => '41', |
||||
'green' => '42', |
||||
'yellow' => '43', |
||||
'blue' => '44', |
||||
'magenta' => '45', |
||||
'cyan' => '46', |
||||
'light_gray' => '47' |
||||
]; |
||||
|
||||
$style = (isset($options['style'])) ? strtolower($options['style']) : ''; |
||||
$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 = ''; |
||||
if (is_array($input)) { |
||||
foreach ($input as $s) { |
||||
$str .= $s . PHP_EOL; |
||||
} |
||||
} else { |
||||
$str = $input; |
||||
} |
||||
if (is_array($input)) { |
||||
foreach ($input as $s) { |
||||
$str .= $s . PHP_EOL; |
||||
} |
||||
} else { |
||||
$str = $input; |
||||
} |
||||
|
||||
$colored_string .= $str . "\033[0m"; |
||||
return $colored_string; |
||||
$colored_string .= $str . "\033[0m"; |
||||
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 |
||||
/** @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'); |
||||
|
||||
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 |
||||
* @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) { |
||||
$this->handle = false; // Too dangerious, so return false |
||||
} else { |
||||
$filename = str_replace("php", "", $filename); |
||||
if (! is_dir(PROJECT_LOGS_DIR)){ |
||||
//Directory does not exist, so lets create it. |
||||
mkdir(PROJECT_LOGS_DIR, 0775); |
||||
} |
||||
/** |
||||
* 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. |
||||
*/ |
||||
public function __construct(string $filename = 'neato_Installer', int $max_count = 1000) |
||||
{ |
||||
if (strpos($filename, "..") !== false) { |
||||
$this->_handle = false; // Too dangerious, so return false |
||||
} else { |
||||
$filename = str_replace("php", "", $filename); |
||||
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 = escapeshellcmd($filename); |
||||
$file = PROJECT_LOGS_DIR . '/' . $filename . ".log.txt"; |
||||
$filename = preg_replace("|[^A-Za-z0-9_]|", "", $filename); |
||||
$filename = escapeshellcmd($filename); |
||||
$file = PROJECT_LOGS_DIR . '/' . $filename . ".log.txt"; |
||||
|
||||
if ($max_count > 1) { |
||||
if ($this->get_lines($file) > $max_count) { |
||||
unlink($file); |
||||
} |
||||
} |
||||
if ($max_count > 1) { |
||||
if ($this->getLines($file) > $max_count) { |
||||
unlink($file); |
||||
} |
||||
} |
||||
|
||||
$success = touch($file); |
||||
if ($success === false) { |
||||
$this->handle = false; |
||||
throw new \Exception('Unable to touch file:' . $file); |
||||
} |
||||
chmod($file, 0660); |
||||
if (! is_writable($file)) { |
||||
$this->handle = false; |
||||
throw new \Exception('Unable to write to file:' . $file); |
||||
} |
||||
$this->handle = fopen($file, 'a'); |
||||
$success = touch($file); |
||||
if ($success === false) { |
||||
$this->_handle = false; |
||||
throw new \Exception('Unable to touch file:' . $file); |
||||
} |
||||
chmod($file, 0660); |
||||
if (! is_writable($file)) { |
||||
$this->_handle = false; |
||||
throw new \Exception('Unable to write to file:' . $file); |
||||
} |
||||
$this->_handle = fopen($file, 'a'); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Count number of lines in Log File |
||||
* @param string $file |
||||
* @return int line count |
||||
*/ |
||||
public function get_lines(string $file): int { |
||||
// No such file, so return zero for length. |
||||
if (! file_exists($file)) { |
||||
return 0; |
||||
} |
||||
/** |
||||
* Count number of lines in Log File |
||||
* |
||||
* @param string $file filename to check how big it is |
||||
* |
||||
* @return int line count |
||||
*/ |
||||
public function getLines(string $file): int |
||||
{ |
||||
// No such file, so return zero for length. |
||||
if (! file_exists($file)) { |
||||
return 0; |
||||
} |
||||
|
||||
$f = fopen($file, 'rb'); |
||||
$lines = 0; |
||||
$f = fopen($file, 'rb'); |
||||
$lines = 0; |
||||
|
||||
if ($f === false || !is_resource($f)) { |
||||
return 0; |
||||
} |
||||
if ($f === false || !is_resource($f)) { |
||||
return 0; |
||||
} |
||||
|
||||
while (!feof($f)) { |
||||
$line = fread($f, 8192); |
||||
if ($line === false) { |
||||
return 0; |
||||
} |
||||
$lines += substr_count($line, "\n"); |
||||
} |
||||
while (!feof($f)) { |
||||
$line = fread($f, 8192); |
||||
if ($line === false) { |
||||
return 0; |
||||
} |
||||
$lines += substr_count($line, "\n"); |
||||
} |
||||
|
||||
fclose($f); |
||||
fclose($f); |
||||
|
||||
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; |
||||
return $lines; |
||||
} |
||||
$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'); |
||||
|
||||
/** |
||||
* 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); |
||||
$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 |
||||
*/ |
||||
public function __destruct() { |
||||
if ($this->handle !== false && is_resource($this->handle)) { |
||||
fclose($this->handle); |
||||
/** |
||||
* Close Log File Handle |
||||
*/ |
||||
public function __destruct() |
||||
{ |
||||
if ($this->_handle !== false && is_resource($this->_handle)) { |
||||
fclose($this->_handle); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} // end of Logger |
||||
|
||||
@ -1,69 +1,178 @@ |
||||
<?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 = []; |
||||
protected function __construct() { } |
||||
/** |
||||
* Class Registry to hold di |
||||
* |
||||
* @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)])) { |
||||
$a = self::$registry[strtolower($name)]; |
||||
if ($key === false) { |
||||
return $a; |
||||
} |
||||
if (isset($a[$key])) { |
||||
return $a[$key]; |
||||
} |
||||
/** |
||||
* Retrieves data from the registry. |
||||
* |
||||
* @param string $name item label to fetch |
||||
* @param string|false $key is like a subkey for another item |
||||
* |
||||
* @return mixed |
||||
*/ |
||||
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)) { |
||||
return false; |
||||
/** |
||||
* Assigns data into the registry to be used anywhere in the app. |
||||
* |
||||
* @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 = []; |
||||
|
||||
public function register(string $service_name, callable $callable): void { |
||||
$this->services[$service_name] = $callable; |
||||
} |
||||
// Note args may be an object or an array maybe more...! |
||||
public function get_service(string $service_name, $args = []) { |
||||
if (!array_key_exists($service_name, $this->services)) { |
||||
throw new \Exception("The Service: {$service_name} does not exists."); |
||||
/** |
||||
* Class Di dependency injection. |
||||
* |
||||
* @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 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); |
||||
} |
||||
|
||||
public function __get(string $service_name) { |
||||
return $this->get_service($service_name); |
||||
} |
||||
/** |
||||
* Majic setter, service to be registered. |
||||
* |
||||
* @param string $service_name label for service |
||||
* @param callable $callable code injected for later use |
||||
* |
||||
* @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 |
||||
registry::set('di', new di()); |
||||
Neato_Registry::set('di', new Di()); |
||||
|
||||
// Setup php for working with Unicode data, if possible |
||||
if (extension_loaded('mbstring')) { |
||||
mb_internal_encoding('UTF-8'); |
||||
mb_http_output('UTF-8'); |
||||
mb_language('uni'); |
||||
setlocale(LC_ALL, "en_US.UTF-8"); |
||||
mb_internal_encoding('UTF-8'); |
||||
mb_http_output('UTF-8'); |
||||
mb_language('uni'); |
||||
setlocale(LC_ALL, "en_US.UTF-8"); |
||||
} |
||||
Loading…
Reference in new issue