Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Factories caching bug #8037

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Factories
*/
public static function define(string $component, string $alias, string $classname): void
{
$component = strtolower($component);

if (isset(self::$aliases[$component][$alias])) {
if (self::$aliases[$component][$alias] === $classname) {
return;
Expand Down Expand Up @@ -130,12 +132,14 @@ public static function define(string $component, string $alias, string $classnam
*/
public static function __callStatic(string $component, array $arguments)
{
$component = strtolower($component);

// First argument is the class alias, second is options
$alias = trim(array_shift($arguments), '\\ ');
$options = array_shift($arguments) ?? [];

// Determine the component-specific options
$options = array_merge(self::getOptions(strtolower($component)), $options);
$options = array_merge(self::getOptions($component), $options);

if (! $options['getShared']) {
if (isset(self::$aliases[$component][$alias])) {
Expand Down Expand Up @@ -394,6 +398,8 @@ public static function getOptions(string $component): array
*/
public static function setOptions(string $component, array $values): array
{
$component = strtolower($component);

// Allow the config to replace the component name, to support "aliases"
$values['component'] = strtolower($values['component'] ?? $component);

Expand Down Expand Up @@ -424,19 +430,19 @@ public static function reset(?string $component = null)
{
if ($component) {
unset(
static::$options[$component],
static::$aliases[$component],
static::$instances[$component],
static::$updated[$component]
self::$options[$component],
MGatner marked this conversation as resolved.
Show resolved Hide resolved
self::$aliases[$component],
self::$instances[$component],
self::$updated[$component]
);

return;
}

static::$options = [];
static::$aliases = [];
static::$instances = [];
static::$updated = [];
self::$options = [];
self::$aliases = [];
self::$instances = [];
self::$updated = [];
}

/**
Expand All @@ -452,8 +458,9 @@ public static function reset(?string $component = null)
*/
public static function injectMock(string $component, string $alias, object $instance)
{
// Force a configuration to exist for this component
$component = strtolower($component);

// Force a configuration to exist for this component
self::getOptions($component);

$class = get_class($instance);
Expand Down Expand Up @@ -493,15 +500,17 @@ public static function getBasename(string $alias): string
*/
public static function getComponentInstances(string $component): array
{
if (! isset(static::$aliases[$component])) {
if (! isset(self::$aliases[$component])) {
return [
'options' => [],
'aliases' => [],
'instances' => [],
];
}

return [
'aliases' => static::$aliases[$component],
'options' => self::$options[$component],
'aliases' => self::$aliases[$component],
'instances' => self::$instances[$component],
];
}
Expand All @@ -513,8 +522,10 @@ public static function getComponentInstances(string $component): array
*/
public static function setComponentInstances(string $component, array $data): void
{
static::$aliases[$component] = $data['aliases'];
self::$options[$component] = $data['options'];
self::$aliases[$component] = $data['aliases'];
self::$instances[$component] = $data['instances'];

unset(self::$updated[$component]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/system/Config/FactoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public function testGetComponentInstances()
public function testSetComponentInstances(array $data)
{
$before = Factories::getComponentInstances('config');
$this->assertSame(['aliases' => [], 'instances' => []], $before);
$this->assertSame(['options' => [], 'aliases' => [], 'instances' => []], $before);

Factories::setComponentInstances('config', $data);

Expand Down
Loading