Skip to content

Commit 9ac1dda

Browse files
committed
feat: add $factories to save factory methods
1 parent d23acf0 commit 9ac1dda

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

system/Config/BaseService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ class BaseService
150150
*/
151151
protected static $instances = [];
152152

153+
/**
154+
* Factory method list.
155+
*
156+
* @var array<string, (callable(mixed ...$params): object)> [key => callable]
157+
*/
158+
protected static array $factories = [];
159+
153160
/**
154161
* Mock objects for testing which are returned if exist.
155162
*
@@ -295,6 +302,10 @@ public static function locator(bool $getShared = true)
295302
*/
296303
public static function __callStatic(string $name, array $arguments)
297304
{
305+
if (isset(static::$factories[$name])) {
306+
return static::$factories[$name](...$arguments);
307+
}
308+
298309
$service = static::serviceExists($name);
299310

300311
if ($service === null) {
@@ -311,11 +322,14 @@ public static function __callStatic(string $name, array $arguments)
311322
public static function serviceExists(string $name): ?string
312323
{
313324
static::buildServicesCache();
325+
314326
$services = array_merge(self::$serviceNames, [Services::class]);
315327
$name = strtolower($name);
316328

317329
foreach ($services as $service) {
318330
if (method_exists($service, $name)) {
331+
static::$factories[$name] = [$service, $name];
332+
319333
return $service;
320334
}
321335
}
@@ -332,6 +346,7 @@ public static function reset(bool $initAutoloader = true)
332346
{
333347
static::$mocks = [];
334348
static::$instances = [];
349+
static::$factories = [];
335350

336351
if ($initAutoloader) {
337352
static::autoloader()->initialize(new Autoload(), new Modules());

0 commit comments

Comments
 (0)