Skip to content

Commit 42f90a2

Browse files
committed
fix: Factories do not return shared instance
If it is called by different aliase like `'App'` and `App::class`. But if they are exactly the same FQCN, the shared instance should be returned.
1 parent 936d298 commit 42f90a2

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

system/Config/Factories.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public static function __callStatic(string $component, array $arguments)
142142
return new $class(...$arguments);
143143
}
144144

145+
// Try to locate the class
145146
if ($class = self::locateClass($options, $alias)) {
146147
return new $class(...$arguments);
147148
}
@@ -179,6 +180,7 @@ public static function __callStatic(string $component, array $arguments)
179180
*/
180181
private static function getDefinedInstance(array $options, string $alias, array $arguments)
181182
{
183+
// The alias is already defined.
182184
if (isset(self::$aliases[$options['component']][$alias])) {
183185
$class = self::$aliases[$options['component']][$alias];
184186

@@ -195,6 +197,21 @@ private static function getDefinedInstance(array $options, string $alias, array
195197
}
196198
}
197199

200+
// Try to locate the class
201+
if (! $class = self::locateClass($options, $alias)) {
202+
return null;
203+
}
204+
205+
// Need to verify if the shared instance matches the request
206+
if (self::verifyInstanceOf($options, $class)) {
207+
// Check for an existing instance for the class
208+
if (isset(self::$instances[$options['component']][$class])) {
209+
self::$aliases[$options['component']][$alias] = $class;
210+
211+
return self::$instances[$options['component']][$class];
212+
}
213+
}
214+
198215
return null;
199216
}
200217

0 commit comments

Comments
 (0)