pull/723/merge
fadrian06 3 days ago committed by GitHub
commit 650a92a301
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -4,14 +4,12 @@ declare(strict_types=1);
namespace flight\core; namespace flight\core;
use Closure; use Throwable;
use Exception;
/** /**
* The Loader class is responsible for loading objects. It maintains * Responsible for instantiating classes. It maintains a list of reusable
* a list of reusable class instances and can generate a new class * instances and can generate new instances with custom constructor arguments.
* instances with custom initialization parameters. It also performs * It also performs automatic class loading.
* class autoloading.
* *
* @copyright 2011 Mike Cao https://mikecao.com * @copyright 2011 Mike Cao https://mikecao.com
* @license https://docs.flightphp.com/license MIT * @license https://docs.flightphp.com/license MIT
@ -21,50 +19,42 @@ class Loader
/** /**
* Registered classes. * Registered classes.
* *
* @var array<string, array{class-string|Closure(): object, array<int, mixed>, ?callable}> $classes * @var array<string, array{
* class-string<object>|callable(mixed ...$constructorArguments): object,
* mixed[],
* ?callable(object $instance): void,
* }>
*/ */
protected array $classes = []; protected array $classes = [];
/** /** If this is disabled, classes can load with underscores */
* If this is disabled, classes can load with underscores
*/
protected static bool $v2ClassLoading = true; protected static bool $v2ClassLoading = true;
/** /** @var array<string, object> Class instances */
* Class instances.
*
* @var array<string, object>
*/
protected array $instances = []; protected array $instances = [];
/** /** @var string[] Autoload directories */
* Autoload directories.
*
* @var array<int, string>
*/
protected static array $dirs = []; protected static array $dirs = [];
/** /**
* Registers a class. * Registers a class.
* *
* @param string $name Registry name
* @param class-string<T>|(Closure(): T) $class Class name or function to instantiate class
* @param array<int, mixed> $params Class initialization parameters
* @param null|(Closure(T $instance): void) $callback Function to call after object instantiation
*
* @template T of object * @template T of object
* @param string $name Class alias.
* @param class-string<T>|callable(mixed ...$constructorArguments): T $class Class factory.
* @param mixed[] $params Class constructor arguments.
* @param ?callable(T $instance): void $callback After instantiation callable.
*/ */
public function register(string $name, $class, array $params = [], ?callable $callback = null): void public function register(string $name, $class, array $params = [], ?callable $callback = null): void
{ {
unset($this->instances[$name]); unset($this->instances[$name]);
$this->classes[$name] = [$class, $params, $callback]; $this->classes[$name] = [$class, $params, $callback];
} }
/** /**
* Unregisters a class. * Unregisters a class.
* *
* @param string $name Registry name * @param string $name Class alias.
*/ */
public function unregister(string $name): void public function unregister(string $name): void
{ {
@ -72,51 +62,46 @@ class Loader
} }
/** /**
* Loads a registered class. * Gets an instance of a registered class.
*
* @param string $name Method name
* @param bool $shared Shared instance
* *
* @throws Exception * @param string $name Class alias.
* * @param bool $shared Whether to return a shared instance or create a new one.
* @return ?object Class instance * @return ?object
* @throws Throwable
*/ */
public function load(string $name, bool $shared = true): ?object public function load(string $name, bool $shared = true): ?object
{ {
$obj = null; $instance = null;
if (isset($this->classes[$name])) { if (!isset($this->classes[$name])) {
[0 => $class, 1 => $params, 2 => $callback] = $this->classes[$name]; return null;
}
$exists = isset($this->instances[$name]); [$factory, $constructorArguments, $onAfterInstantiating] = $this->classes[$name];
$exists = isset($this->instances[$name]);
if ($shared) { if ($shared) {
$obj = ($exists) ? $instance = $exists
$this->getInstance($name) : ? $this->getInstance($name)
$this->newInstance($class, $params); : $this->newInstance($factory, $constructorArguments);
if (!$exists) { $this->instances[$name] ??= $instance;
$this->instances[$name] = $obj; } else {
} $instance = $this->newInstance($factory, $constructorArguments);
} else { }
$obj = $this->newInstance($class, $params);
}
if ($callback && (!$shared || !$exists)) { if ($onAfterInstantiating && (!$shared || !$exists)) {
$ref = [&$obj]; $onAfterInstantiating($instance);
\call_user_func_array($callback, $ref);
}
} }
return $obj; return $instance;
} }
/** /**
* Gets a single instance of a class. * Gets a single instance.
* *
* @param string $name Instance name * @param string $name Class alias.
* * @return ?object
* @return ?object Class instance
*/ */
public function getInstance(string $name): ?object public function getInstance(string $name): ?object
{ {
@ -126,39 +111,37 @@ class Loader
/** /**
* Gets a new instance of a class. * Gets a new instance of a class.
* *
* @param class-string<T>|Closure(): class-string<T> $class Class name or callback function to instantiate class * @template T of object = object
* @param array<int, string> $params Class initialization parameters * @param class-string<T>|callable(mixed ...$constructorArguments): T $class Class factory.
* * @param mixed[] $params Class constructor arguments.
* @template T of object * @return T
* * @throws Throwable
* @throws Exception
*
* @return T Class instance
*/ */
public function newInstance($class, array $params = []) public function newInstance($class, array $params = []): object
{ {
if (\is_callable($class)) { if (is_callable($class)) {
return \call_user_func_array($class, $params); return $class(...$params);
} }
return new $class(...$params); return new $class(...$params);
} }
/** /**
* Gets a registered callable * Gets a registered class factory, constructor arguments and after instantiation callable.
* *
* @param string $name Registry name * @param string $name Class alias.
* * @return ?array{
* @return mixed Class information or null if not registered * class-string<object>|callable(mixed ...$constructorArguments): object,
* mixed[],
* ?callable(object $instance): void,
* }
*/ */
public function get(string $name) public function get(string $name)
{ {
return $this->classes[$name] ?? null; return $this->classes[$name] ?? null;
} }
/** /** Resets the Loader by clearing registered classes and instances */
* Resets the object to the initial state.
*/
public function reset(): void public function reset(): void
{ {
$this->classes = []; $this->classes = [];
@ -170,8 +153,8 @@ class Loader
/** /**
* Starts/stops autoloader. * Starts/stops autoloader.
* *
* @param bool $enabled Enable/disable autoloading * @param bool $enabled Enable/disable autoloading.
* @param string|iterable<int, string> $dirs Autoload directories * @param string|string[] $dirs Autoload directories.
*/ */
public static function autoload(bool $enabled = true, $dirs = []): void public static function autoload(bool $enabled = true, $dirs = []): void
{ {
@ -211,7 +194,7 @@ class Loader
/** /**
* Adds a directory for autoloading classes. * Adds a directory for autoloading classes.
* *
* @param string|iterable<int, string> $dir Directory path * @param string|string[] $dir Directory path.
*/ */
public static function addDirectory($dir): void public static function addDirectory($dir): void
{ {
@ -219,22 +202,32 @@ class Loader
foreach ($dir as $value) { foreach ($dir as $value) {
self::addDirectory($value); self::addDirectory($value);
} }
} elseif (is_string($dir)) {
$dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dir);
if (!in_array($dir, self::$dirs, true)) { return;
self::$dirs[] = $dir; }
}
if (!is_string($dir)) {
return;
} }
$dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dir);
if (in_array($dir, self::$dirs)) {
return;
}
self::$dirs[] = $dir;
} }
/** /**
* Sets the value for V2 class loading. * Sets v2 class loading mode.
* *
* @param bool $value The value to set for V2 class loading. * When true (default), underscores in class names are converted to directory
* separators (e.g. "Foo_Bar" loads "Foo/Bar.php"). Set to false to disable
* this conversion and treat underscores as literal characters in the filename.
* *
* @return void * @param bool $value True to convert underscores to directory separators (v2 behaviour); false to disable.
*/ */
public static function setV2ClassLoading(bool $value): void public static function setV2ClassLoading(bool $value): void
{ {

Loading…
Cancel
Save