Skip to content

Commit

Permalink
feat: add Factories::get()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 2, 2024
1 parent 5c4aa4b commit 377bdfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ public static function __callStatic(string $component, array $arguments)
return self::$instances[$options['component']][$class];
}

/**
* Simple method to get the shared instance fast.
*/
public static function get(string $component, string $alias)
{
if (isset(self::$aliases[$component][$alias])) {
$class = self::$aliases[$component][$alias];

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

return self::__callStatic($component, [$alias]);
}

/**
* Gets the defined instance. If not exists, creates new one.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/system/Config/FactoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,25 @@ public function testIsUpdated(array $data)

$this->assertFalse(Factories::isUpdated('config'));
}

public function testGet()
{
$config = Factories::config('App');

$this->assertSame($config, Factories::get('config', 'App'));
}

public function testGetNonexistentInstance()
{
$config = Factories::get('config', 'App');

$this->assertSame($config, Factories::config('App'));
}

public function testGetNonexistentClass()
{
$config = Factories::get('config', 'NonexistentInstance');

$this->assertNull($config);
}
}

0 comments on commit 377bdfb

Please sign in to comment.