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.
69 lines
1.8 KiB
69 lines
1.8 KiB
<?php
|
|
|
|
final class registry {
|
|
private static $registry = [];
|
|
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];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function set(string $name, $value): bool {
|
|
if (array_key_exists(strtolower($name), self::$registry)) {
|
|
return false;
|
|
}
|
|
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.");
|
|
}
|
|
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);
|
|
}
|
|
|
|
public function list_services_as_array(): array {
|
|
return array_keys($this->services);
|
|
}
|
|
|
|
public function list_services_as_string(): 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");
|
|
} |