You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
4.4 KiB
178 lines
4.4 KiB
<?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
|
|
*/
|
|
|
|
/**
|
|
* 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 Registry
|
|
{
|
|
private static $_registry = [];
|
|
|
|
/**
|
|
* Protected constructor to stop instances from being created.
|
|
*/
|
|
protected function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
|
|
// 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");
|
|
} |