Commit version 24.12.13800
This commit is contained in:
@ -1,53 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Flight: An extensible micro-framework.
|
||||
*
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @license MIT, http://flightphp.com/license
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace flight\core;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* The Loader class is responsible for loading objects. It maintains
|
||||
* a list of reusable class instances and can generate a new class
|
||||
* instances with custom initialization parameters. It also performs
|
||||
* class autoloading.
|
||||
*
|
||||
* @license MIT, http://flightphp.com/license
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
*/
|
||||
class Loader {
|
||||
class Loader
|
||||
{
|
||||
/**
|
||||
* Registered classes.
|
||||
*
|
||||
* @var array
|
||||
* @var array<string, array{class-string|Closure(): object, array<int, mixed>, ?callable}> $classes
|
||||
*/
|
||||
protected $classes = array();
|
||||
protected array $classes = [];
|
||||
|
||||
/**
|
||||
* If this is disabled, classes can load with underscores
|
||||
*/
|
||||
protected static bool $v2ClassLoading = true;
|
||||
|
||||
/**
|
||||
* Class instances.
|
||||
*
|
||||
* @var array
|
||||
* @var array<string, object>
|
||||
*/
|
||||
protected $instances = array();
|
||||
protected array $instances = [];
|
||||
|
||||
/**
|
||||
* Autoload directories.
|
||||
*
|
||||
* @var array
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected static $dirs = array();
|
||||
protected static array $dirs = [];
|
||||
|
||||
/**
|
||||
* Registers a class.
|
||||
*
|
||||
* @param string $name Registry name
|
||||
* @param string|callable $class Class name or function to instantiate class
|
||||
* @param array $params Class initialization parameters
|
||||
* @param callback $callback Function to call after object instantiation
|
||||
* @param class-string<T>|Closure(): T $class Class name or function to instantiate class
|
||||
* @param array<int, mixed> $params Class initialization parameters
|
||||
* @param ?Closure(T $instance): void $callback $callback Function to call after object instantiation
|
||||
*
|
||||
* @template T of object
|
||||
*/
|
||||
public function register($name, $class, array $params = array(), $callback = null) {
|
||||
public function register(string $name, $class, array $params = [], ?callable $callback = null): void
|
||||
{
|
||||
unset($this->instances[$name]);
|
||||
|
||||
$this->classes[$name] = array($class, $params, $callback);
|
||||
$this->classes[$name] = [$class, $params, $callback];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,23 +66,27 @@ class Loader {
|
||||
*
|
||||
* @param string $name Registry name
|
||||
*/
|
||||
public function unregister($name) {
|
||||
public function unregister(string $name): void
|
||||
{
|
||||
unset($this->classes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a registered class.
|
||||
*
|
||||
* @param string $name Method name
|
||||
* @param bool $shared Shared instance
|
||||
* @return object Class instance
|
||||
* @throws \Exception
|
||||
* @param string $name Method name
|
||||
* @param bool $shared Shared instance
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return ?object Class instance
|
||||
*/
|
||||
public function load($name, $shared = true) {
|
||||
public function load(string $name, bool $shared = true): ?object
|
||||
{
|
||||
$obj = null;
|
||||
|
||||
if (isset($this->classes[$name])) {
|
||||
list($class, $params, $callback) = $this->classes[$name];
|
||||
[0 => $class, 1 => $params, 2 => $callback] = $this->classes[$name];
|
||||
|
||||
$exists = isset($this->instances[$name]);
|
||||
|
||||
@ -79,18 +94,17 @@ class Loader {
|
||||
$obj = ($exists) ?
|
||||
$this->getInstance($name) :
|
||||
$this->newInstance($class, $params);
|
||||
|
||||
|
||||
if (!$exists) {
|
||||
$this->instances[$name] = $obj;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$obj = $this->newInstance($class, $params);
|
||||
}
|
||||
|
||||
if ($callback && (!$shared || !$exists)) {
|
||||
$ref = array(&$obj);
|
||||
call_user_func_array($callback, $ref);
|
||||
$ref = [&$obj];
|
||||
\call_user_func_array($callback, $ref);
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,78 +115,70 @@ class Loader {
|
||||
* Gets a single instance of a class.
|
||||
*
|
||||
* @param string $name Instance name
|
||||
* @return object Class instance
|
||||
*
|
||||
* @return ?object Class instance
|
||||
*/
|
||||
public function getInstance($name) {
|
||||
return isset($this->instances[$name]) ? $this->instances[$name] : null;
|
||||
public function getInstance(string $name): ?object
|
||||
{
|
||||
return $this->instances[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a new instance of a class.
|
||||
*
|
||||
* @param string|callable $class Class name or callback function to instantiate class
|
||||
* @param array $params Class initialization parameters
|
||||
* @return object Class instance
|
||||
* @throws \Exception
|
||||
* @param class-string<T>|Closure(): class-string<T> $class Class name or callback function to instantiate class
|
||||
* @param array<int, string> $params Class initialization parameters
|
||||
*
|
||||
* @template T of object
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return T Class instance
|
||||
*/
|
||||
public function newInstance($class, array $params = array()) {
|
||||
if (is_callable($class)) {
|
||||
return call_user_func_array($class, $params);
|
||||
public function newInstance($class, array $params = [])
|
||||
{
|
||||
if (\is_callable($class)) {
|
||||
return \call_user_func_array($class, $params);
|
||||
}
|
||||
|
||||
switch (count($params)) {
|
||||
case 0:
|
||||
return new $class();
|
||||
case 1:
|
||||
return new $class($params[0]);
|
||||
case 2:
|
||||
return new $class($params[0], $params[1]);
|
||||
case 3:
|
||||
return new $class($params[0], $params[1], $params[2]);
|
||||
case 4:
|
||||
return new $class($params[0], $params[1], $params[2], $params[3]);
|
||||
case 5:
|
||||
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
|
||||
default:
|
||||
try {
|
||||
$refClass = new \ReflectionClass($class);
|
||||
return $refClass->newInstanceArgs($params);
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new \Exception("Cannot instantiate {$class}", 0, $e);
|
||||
}
|
||||
}
|
||||
return new $class(...$params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a registered callable
|
||||
*
|
||||
* @param string $name Registry name
|
||||
*
|
||||
* @return mixed Class information or null if not registered
|
||||
*/
|
||||
public function get($name) {
|
||||
return isset($this->classes[$name]) ? $this->classes[$name] : null;
|
||||
public function get(string $name)
|
||||
{
|
||||
return $this->classes[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the object to the initial state.
|
||||
*/
|
||||
public function reset() {
|
||||
$this->classes = array();
|
||||
$this->instances = array();
|
||||
public function reset(): void
|
||||
{
|
||||
$this->classes = [];
|
||||
$this->instances = [];
|
||||
}
|
||||
|
||||
/*** Autoloading Functions ***/
|
||||
// Autoloading Functions
|
||||
|
||||
/**
|
||||
* Starts/stops autoloader.
|
||||
*
|
||||
* @param bool $enabled Enable/disable autoloading
|
||||
* @param array $dirs Autoload directories
|
||||
* @param bool $enabled Enable/disable autoloading
|
||||
* @param string|iterable<int, string> $dirs Autoload directories
|
||||
*/
|
||||
public static function autoload($enabled = true, $dirs = array()) {
|
||||
public static function autoload(bool $enabled = true, $dirs = []): void
|
||||
{
|
||||
if ($enabled) {
|
||||
spl_autoload_register(array(__CLASS__, 'loadClass'));
|
||||
}
|
||||
else {
|
||||
spl_autoload_unregister(array(__CLASS__, 'loadClass'));
|
||||
spl_autoload_register([__CLASS__, 'loadClass']);
|
||||
} else {
|
||||
spl_autoload_unregister([__CLASS__, 'loadClass']); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (!empty($dirs)) {
|
||||
@ -183,15 +189,20 @@ class Loader {
|
||||
/**
|
||||
* Autoloads classes.
|
||||
*
|
||||
* Classes are not allowed to have underscores in their names.
|
||||
*
|
||||
* @param string $class Class name
|
||||
*/
|
||||
public static function loadClass($class) {
|
||||
$class_file = str_replace(array('\\', '_'), '/', $class).'.php';
|
||||
public static function loadClass(string $class): void
|
||||
{
|
||||
$replace_chars = self::$v2ClassLoading === true ? ['\\', '_'] : ['\\'];
|
||||
$classFile = str_replace($replace_chars, '/', $class) . '.php';
|
||||
|
||||
foreach (self::$dirs as $dir) {
|
||||
$file = $dir.'/'.$class_file;
|
||||
if (file_exists($file)) {
|
||||
require $file;
|
||||
$filePath = "$dir/$classFile";
|
||||
|
||||
if (file_exists($filePath)) {
|
||||
require_once $filePath;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -200,16 +211,31 @@ class Loader {
|
||||
/**
|
||||
* Adds a directory for autoloading classes.
|
||||
*
|
||||
* @param mixed $dir Directory path
|
||||
* @param string|iterable<int, string> $dir Directory path
|
||||
*/
|
||||
public static function addDirectory($dir) {
|
||||
if (is_array($dir) || is_object($dir)) {
|
||||
public static function addDirectory($dir): void
|
||||
{
|
||||
if (\is_array($dir) || \is_object($dir)) {
|
||||
foreach ($dir as $value) {
|
||||
self::addDirectory($value);
|
||||
}
|
||||
}
|
||||
else if (is_string($dir)) {
|
||||
if (!in_array($dir, self::$dirs)) self::$dirs[] = $dir;
|
||||
} elseif (\is_string($dir)) {
|
||||
if (!\in_array($dir, self::$dirs, true)) {
|
||||
self::$dirs[] = $dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the value for V2 class loading.
|
||||
*
|
||||
* @param bool $value The value to set for V2 class loading.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setV2ClassLoading(bool $value): void
|
||||
{
|
||||
self::$v2ClassLoading = $value;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user