refactor step 1 of Loader

v4
fadrian06 7 days ago
parent 7b1e414717
commit 6784507bf8

@ -5,6 +5,5 @@ includes:
parameters: parameters:
level: 6 level: 6
paths: paths:
- flight - src
- index.php
treatPhpDocTypesAsCertain: false treatPhpDocTypesAsCertain: false

@ -4,14 +4,12 @@ declare(strict_types=1);
namespace flight\core; namespace flight\core;
use Closure;
use Exception; use Exception;
/** /**
* The Loader class is responsible for loading objects. It maintains a list of * 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 * reusable class instances and can generate a new class instances with custom
* initialization parameters. It also performs class autoloading. * initialization parameters. It also performs class autoloading.
*
* @license MIT, https://docs.flightphp.com/license/ * @license MIT, https://docs.flightphp.com/license/
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
*/ */
@ -19,50 +17,39 @@ class Loader
{ {
/** /**
* Registered classes. * Registered classes.
* * @var array<string, array{class-string|callable(): object, array<int, mixed>, ?callable(object): void}>
* @var array<string, array{class-string|Closure(): object, array<int, mixed>, ?callable}> $classes
*/ */
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 array<int, string> Autoload directories */
* Autoload directories.
*
* @var array<int, string>
*/
protected static array $dirs = []; protected static array $dirs = [];
/** /**
* Registers a class. * Registers a class.
* * @template T of object
* @param string $name Registry name * @param string $name Registry name
* @param class-string<T>|Closure(): T $class Class name or function to instantiate class * @param class-string<T>|callable(): T $class Class name or function to instantiate class
* @param array<int, mixed> $params Class initialization parameters * @param array<int, mixed> $params Class initialization parameters
* @param ?Closure(T $instance): void $callback $callback Function to call after object instantiation * @param ?callable(T): void $callback $callback Function to call after object instantiation
*
* @template T of object
*/ */
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 Registry name
*/ */
public function unregister(string $name): void public function unregister(string $name): void
@ -72,12 +59,9 @@ class Loader
/** /**
* Loads a registered class. * Loads a registered class.
* * @param string $name Method name
* @param string $name Method name * @param bool $shared Shared instance
* @param bool $shared Shared instance
*
* @throws Exception * @throws Exception
*
* @return ?object Class instance * @return ?object Class instance
*/ */
public function load(string $name, bool $shared = true): ?object public function load(string $name, bool $shared = true): ?object
@ -85,14 +69,13 @@ class Loader
$obj = null; $obj = null;
if (isset($this->classes[$name])) { if (isset($this->classes[$name])) {
[0 => $class, 1 => $params, 2 => $callback] = $this->classes[$name]; [$class, $params, $callback] = $this->classes[$name];
$exists = isset($this->instances[$name]); $exists = isset($this->instances[$name]);
if ($shared) { if ($shared) {
$obj = ($exists) ? $obj = $exists
$this->getInstance($name) : ? $this->getInstance($name)
$this->newInstance($class, $params); : $this->newInstance($class, $params);
if (!$exists) { if (!$exists) {
$this->instances[$name] = $obj; $this->instances[$name] = $obj;
@ -103,7 +86,7 @@ class Loader
if ($callback && (!$shared || !$exists)) { if ($callback && (!$shared || !$exists)) {
$ref = [&$obj]; $ref = [&$obj];
\call_user_func_array($callback, $ref); call_user_func_array($callback, $ref);
} }
} }
@ -112,9 +95,7 @@ class Loader
/** /**
* Gets a single instance of a class. * Gets a single instance of a class.
*
* @param string $name Instance name * @param string $name Instance name
*
* @return ?object Class instance * @return ?object Class instance
*/ */
public function getInstance(string $name): ?object public function getInstance(string $name): ?object
@ -124,53 +105,43 @@ 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
* @param array<int, string> $params Class initialization parameters
*
* @template T of object * @template T of object
* * @param class-string<T>|callable(): T $class Class name or callback function to instantiate class
* @param array<int, string> $params Class initialization parameters
* @throws Exception * @throws Exception
*
* @return T Class instance * @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 call_user_func_array($class, $params);
} }
return new $class(...$params); return new $class(...$params);
} }
/** /**
* Gets a registered callable * Gets a registered callable.
*
* @param string $name Registry name * @param string $name Registry name
* * @return ?array{class-string|callable(): object, array<int, mixed>, ?callable(object): void}
* @return mixed Class information or null if not registered * Class information or null if not registered
*/ */
public function get(string $name) public function get(string $name): ?array
{ {
return $this->classes[$name] ?? null; return $this->classes[$name] ?? null;
} }
/** /** Resets the object to the initial state */
* Resets the object to the initial state.
*/
public function reset(): void public function reset(): void
{ {
$this->classes = []; $this->classes = [];
$this->instances = []; $this->instances = [];
} }
// Autoloading Functions
/** /**
* 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|iterable<int, string> $dirs Autoload directories
*/ */
public static function autoload(bool $enabled = true, $dirs = []): void public static function autoload(bool $enabled = true, $dirs = []): void
{ {
@ -187,14 +158,13 @@ class Loader
/** /**
* Autoloads classes. * Autoloads classes.
*
* Classes are not allowed to have underscores in their names. * Classes are not allowed to have underscores in their names.
* *
* @param string $class Class name * @param string $class Class name
*/ */
public static function loadClass(string $class): void public static function loadClass(string $class): void
{ {
$replace_chars = self::$v2ClassLoading === true ? ['\\', '_'] : ['\\']; $replace_chars = self::$v2ClassLoading ? ['\\', '_'] : ['\\'];
$classFile = str_replace($replace_chars, '/', $class) . '.php'; $classFile = str_replace($replace_chars, '/', $class) . '.php';
foreach (self::$dirs as $dir) { foreach (self::$dirs as $dir) {
@ -202,6 +172,7 @@ class Loader
if (file_exists($filePath)) { if (file_exists($filePath)) {
require_once $filePath; require_once $filePath;
return; return;
} }
} }
@ -209,17 +180,16 @@ class Loader
/** /**
* Adds a directory for autoloading classes. * Adds a directory for autoloading classes.
*
* @param string|iterable<int, string> $dir Directory path * @param string|iterable<int, string> $dir Directory path
*/ */
public static function addDirectory($dir): void public static function addDirectory($dir): void
{ {
if (\is_array($dir) || \is_object($dir)) { if (is_array($dir) || is_object($dir)) {
foreach ($dir as $value) { foreach ($dir as $value) {
self::addDirectory($value); self::addDirectory($value);
} }
} elseif (\is_string($dir)) { } elseif (is_string($dir)) {
if (!\in_array($dir, self::$dirs, true)) { if (!in_array($dir, self::$dirs, true)) {
self::$dirs[] = $dir; self::$dirs[] = $dir;
} }
} }
@ -228,10 +198,7 @@ class Loader
/** /**
* Sets the value for V2 class loading. * Sets the value for V2 class loading.
*
* @param bool $value The value to set for V2 class loading. * @param bool $value The value to set for V2 class loading.
*
* @return void
*/ */
public static function setV2ClassLoading(bool $value): void public static function setV2ClassLoading(bool $value): void
{ {

Loading…
Cancel
Save