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 bool $shared Shared instance.
* @return ?object Class instance
* @param string $name Class alias.
* @param bool $shared Whether to return a shared instance or create a new one.
* @return ?object
* @throws Throwable
*/
public function load(string $name, bool $shared = true): ?object
{
$obj = null;
$instance = null;
if (!isset($this->classes[$name])) {
return null;
}
[$class, $params, $callback] = $this->classes[$name];
[$factory, $constructorArguments, $onAfterInstantiating] = $this->classes[$name];
$exists = isset($this->instances[$name]);
if ($shared) {
$obj = $exists
$instance = $exists
? $this->getInstance($name)
: $this->newInstance($class, $params);
: $this->newInstance($factory, $constructorArguments);
$this->instances[$name] ??= $obj;
$this->instances[$name] ??= $instance;
} else {
$obj = $this->newInstance($class, $params);
$instance = $this->newInstance($factory, $constructorArguments);
}
if ($callback && (!$shared || !$exists)) {
$callback($obj);
if ($onAfterInstantiating && (!$shared || !$exists)) {
$onAfterInstantiating($instance);
}
return $obj;
return $instance;
}
/**

Loading…
Cancel
Save