Skip to content

Commit

Permalink
refactor: extract getDefinedInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 26, 2023
1 parent e3c223b commit 244c5fb
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,18 @@ public static function __callStatic(string $component, array $arguments)
}

// Check for an existing definition
if (isset(self::$aliases[$options['component']][$alias])) {
$class = self::$aliases[$options['component']][$alias];

// Need to verify if the shared instance matches the request
if (self::verifyInstanceOf($options, $class)) {
// Check for an existing instance
if (isset(self::$instances[$options['component']][$class])) {
return self::$instances[$options['component']][$class];
}

self::$instances[$options['component']][$class] = new $class(...$arguments);

return self::$instances[$options['component']][$class];

}
$instance = self::getDefinedInstance($options, $alias, $arguments);
if ($instance !== null) {
return $instance;
}

// Check for an existing Config definition with basename.
if (self::isConfig($options['component'])) {
$basename = self::getBasename($alias);

if (isset(self::$aliases[$options['component']][$basename])) {
$class = self::$aliases[$options['component']][$basename];

// Need to verify if the shared instance matches the request
if (self::verifyInstanceOf($options, $class)) {
// Check for an existing instance
if (isset(self::$instances[$options['component']][$class])) {
return self::$instances[$options['component']][$class];
}

self::$instances[$options['component']][$class] = new $class(...$arguments);

return self::$instances[$options['component']][$class];
}
$instance = self::getDefinedInstance($options, $basename, $arguments);
if ($instance !== null) {
return $instance;
}
}

Expand All @@ -195,6 +172,32 @@ public static function __callStatic(string $component, array $arguments)
return self::$instances[$options['component']][$class];
}

/**
* Gets the defined instance. If not exists, creates new one.
*
* @return object|null
*/
private static function getDefinedInstance(array $options, string $alias, array $arguments)
{
if (isset(self::$aliases[$options['component']][$alias])) {
$class = self::$aliases[$options['component']][$alias];

// Need to verify if the shared instance matches the request
if (self::verifyInstanceOf($options, $class)) {
// Check for an existing instance
if (isset(self::$instances[$options['component']][$class])) {
return self::$instances[$options['component']][$class];
}

self::$instances[$options['component']][$class] = new $class(...$arguments);

return self::$instances[$options['component']][$class];
}
}

return null;
}

/**
* Is the component Config?
*
Expand Down

0 comments on commit 244c5fb

Please sign in to comment.