refactor Loader@load with better names and docblock

loader-rework
fadrian06 3 days ago
parent 1519e0cf30
commit 712792006f

@ -64,39 +64,39 @@ class Loader
} }
/** /**
* Loads a registered class. * Gets an instance of a registered class.
* *
* @param string $name Method name. * @param string $name Class alias.
* @param bool $shared Shared instance. * @param bool $shared Whether to return a shared instance or create a new one.
* @return ?object Class instance * @return ?object
* @throws Throwable * @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])) {
return null; return null;
} }
[$class, $params, $callback] = $this->classes[$name]; [$factory, $constructorArguments, $onAfterInstantiating] = $this->classes[$name];
$exists = isset($this->instances[$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);
$this->instances[$name] ??= $obj; $this->instances[$name] ??= $instance;
} else { } else {
$obj = $this->newInstance($class, $params); $instance = $this->newInstance($factory, $constructorArguments);
} }
if ($callback && (!$shared || !$exists)) { if ($onAfterInstantiating && (!$shared || !$exists)) {
$callback($obj); $onAfterInstantiating($instance);
} }
return $obj; return $instance;
} }
/** /**

Loading…
Cancel
Save