Skip to content

Commit 9191688

Browse files
committed
Switch Config to use Factory
1 parent 756d3d9 commit 9191688

File tree

5 files changed

+35
-94
lines changed

5 files changed

+35
-94
lines changed

system/Common.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @filesource
3838
*/
3939

40-
use CodeIgniter\Config\Config;
40+
use CodeIgniter\Config\Factory;
4141
use CodeIgniter\Database\ConnectionInterface;
4242
use CodeIgniter\Files\Exceptions\FileNotFoundException;
4343
use CodeIgniter\HTTP\RedirectResponse;
@@ -253,7 +253,7 @@ function command(string $command)
253253
*/
254254
function config(string $name, bool $getShared = true)
255255
{
256-
return Config::get($name, $getShared);
256+
return Factory::config($name, $getShared);
257257
}
258258
}
259259

@@ -869,7 +869,7 @@ function log_message(string $level, string $message, array $context = [])
869869
*/
870870
function model(string $name, bool $getShared = true, ConnectionInterface &$conn = null)
871871
{
872-
return \CodeIgniter\Config\Factory::model($name, $getShared, $conn);
872+
return Factory::model($name, $getShared, $conn);
873873
}
874874
}
875875

system/Config/Config.php

+7-87
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,10 @@
4242
/**
4343
* Class Config
4444
*
45-
* @package CodeIgniter\Config
45+
* @deprecated Use CodeIgniter\Config\Factory::config()
4646
*/
4747
class Config
4848
{
49-
/**
50-
* Cache for instance of any configurations that
51-
* have been requested as "shared" instance.
52-
*
53-
* @var array
54-
*/
55-
static private $instances = [];
56-
57-
//--------------------------------------------------------------------
58-
5949
/**
6050
* Create new configuration instances or return
6151
* a shared instance
@@ -67,95 +57,25 @@ class Config
6757
*/
6858
public static function get(string $name, bool $getShared = true)
6959
{
70-
$class = $name;
71-
if (($pos = strrpos($name, '\\')) !== false)
72-
{
73-
$class = substr($name, $pos + 1);
74-
}
75-
76-
if (! $getShared)
77-
{
78-
return self::createClass($name);
79-
}
80-
81-
if (! isset( self::$instances[$class] ))
82-
{
83-
self::$instances[$class] = self::createClass($name);
84-
}
85-
return self::$instances[$class];
60+
return Factory::config($name, $getShared);
8661
}
8762

88-
//--------------------------------------------------------------------
89-
9063
/**
9164
* Helper method for injecting mock instances while testing.
9265
*
93-
* @param string $class
66+
* @param string $name
9467
* @param object $instance
9568
*/
96-
public static function injectMock(string $class, $instance)
69+
public static function injectMock(string $name, $instance)
9770
{
98-
self::$instances[$class] = $instance;
71+
Factory::injectMock('config', $name, $instance);
9972
}
10073

101-
//--------------------------------------------------------------------
102-
10374
/**
104-
* Resets the instances array
75+
* Resets the static arrays
10576
*/
10677
public static function reset()
10778
{
108-
static::$instances = [];
109-
}
110-
111-
//--------------------------------------------------------------------
112-
113-
/**
114-
* Find configuration class and create instance
115-
*
116-
* @param string $name Classname
117-
*
118-
* @return mixed|null
119-
*/
120-
private static function createClass(string $name)
121-
{
122-
if (class_exists($name))
123-
{
124-
return new $name();
125-
}
126-
127-
$locator = Services::locator();
128-
$file = $locator->locateFile($name, 'Config');
129-
130-
if (empty($file))
131-
{
132-
// No file found - check if the class was namespaced
133-
if (strpos($name, '\\') !== false)
134-
{
135-
// Class was namespaced and locateFile couldn't find it
136-
return null;
137-
}
138-
139-
// Check all namespaces
140-
$files = $locator->search('Config/' . $name);
141-
if (empty($files))
142-
{
143-
return null;
144-
}
145-
146-
// Get the first match (prioritizes user and framework)
147-
$file = reset($files);
148-
}
149-
150-
$name = $locator->getClassname($file);
151-
152-
if (empty($name))
153-
{
154-
return null;
155-
}
156-
157-
return new $name();
79+
Factory::reset('config');
15880
}
159-
160-
//--------------------------------------------------------------------
16181
}

system/Config/Factories.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Factories extends BaseConfig
6565
'component' => null,
6666
'path' => null,
6767
'instanceOf' => null,
68-
'prefersApp' => false,
68+
'prefersApp' => true,
6969
];
7070

7171
/**

system/Config/Factory.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ class Factory
6060
*/
6161
protected static $configs = [];
6262

63+
/**
64+
* Explicit configuration for the Config
65+
* component to prevent logic loops.
66+
*
67+
* @var array<string, array>
68+
*/
69+
private static $configValues = [
70+
'component' => 'config',
71+
'path' => 'Config',
72+
'instanceOf' => null,
73+
'prefersApp' => true,
74+
];
75+
6376
/**
6477
* Mapping of class basenames (no namespace) to
6578
* their instances.
@@ -211,8 +224,16 @@ public static function getConfig(string $component): array
211224
return self::$configs[$component];
212225
}
213226

214-
// Load the best Factories config (will include Registrars)
215-
$values = config('Factories')->$component ?? [];
227+
// Handle Config as a special case to prevent logic loops
228+
if ($component === 'config')
229+
{
230+
$values = self::$configValues;
231+
}
232+
// Load values from the best Factories configuration (will include Registrars)
233+
else
234+
{
235+
$values = config('Factories')->$component ?? [];
236+
}
216237

217238
return self::setConfig($component, $values);
218239
}

tests/system/Config/ConfigTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testCreateNonConfig()
4444
public function testInjection()
4545
{
4646
Config::reset();
47-
Config::injectMock('Banana', '\stdClass');
47+
Config::injectMock('Banana', new \stdClass());
4848
$this->assertNotNull(Config::get('Banana'));
4949
}
5050

0 commit comments

Comments
 (0)