Skip to content

Commit

Permalink
Switch Config to use Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Oct 5, 2020
1 parent 756d3d9 commit 9191688
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 94 deletions.
6 changes: 3 additions & 3 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @filesource
*/

use CodeIgniter\Config\Config;
use CodeIgniter\Config\Factory;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
Expand Down Expand Up @@ -253,7 +253,7 @@ function command(string $command)
*/
function config(string $name, bool $getShared = true)
{
return Config::get($name, $getShared);
return Factory::config($name, $getShared);
}
}

Expand Down Expand Up @@ -869,7 +869,7 @@ function log_message(string $level, string $message, array $context = [])
*/
function model(string $name, bool $getShared = true, ConnectionInterface &$conn = null)
{
return \CodeIgniter\Config\Factory::model($name, $getShared, $conn);
return Factory::model($name, $getShared, $conn);
}
}

Expand Down
94 changes: 7 additions & 87 deletions system/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,10 @@
/**
* Class Config
*
* @package CodeIgniter\Config
* @deprecated Use CodeIgniter\Config\Factory::config()
*/
class Config
{
/**
* Cache for instance of any configurations that
* have been requested as "shared" instance.
*
* @var array
*/
static private $instances = [];

//--------------------------------------------------------------------

/**
* Create new configuration instances or return
* a shared instance
Expand All @@ -67,95 +57,25 @@ class Config
*/
public static function get(string $name, bool $getShared = true)
{
$class = $name;
if (($pos = strrpos($name, '\\')) !== false)
{
$class = substr($name, $pos + 1);
}

if (! $getShared)
{
return self::createClass($name);
}

if (! isset( self::$instances[$class] ))
{
self::$instances[$class] = self::createClass($name);
}
return self::$instances[$class];
return Factory::config($name, $getShared);
}

//--------------------------------------------------------------------

/**
* Helper method for injecting mock instances while testing.
*
* @param string $class
* @param string $name
* @param object $instance
*/
public static function injectMock(string $class, $instance)
public static function injectMock(string $name, $instance)
{
self::$instances[$class] = $instance;
Factory::injectMock('config', $name, $instance);
}

//--------------------------------------------------------------------

/**
* Resets the instances array
* Resets the static arrays
*/
public static function reset()
{
static::$instances = [];
}

//--------------------------------------------------------------------

/**
* Find configuration class and create instance
*
* @param string $name Classname
*
* @return mixed|null
*/
private static function createClass(string $name)
{
if (class_exists($name))
{
return new $name();
}

$locator = Services::locator();
$file = $locator->locateFile($name, 'Config');

if (empty($file))
{
// No file found - check if the class was namespaced
if (strpos($name, '\\') !== false)
{
// Class was namespaced and locateFile couldn't find it
return null;
}

// Check all namespaces
$files = $locator->search('Config/' . $name);
if (empty($files))
{
return null;
}

// Get the first match (prioritizes user and framework)
$file = reset($files);
}

$name = $locator->getClassname($file);

if (empty($name))
{
return null;
}

return new $name();
Factory::reset('config');
}

//--------------------------------------------------------------------
}
2 changes: 1 addition & 1 deletion system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Factories extends BaseConfig
'component' => null,
'path' => null,
'instanceOf' => null,
'prefersApp' => false,
'prefersApp' => true,
];

/**
Expand Down
25 changes: 23 additions & 2 deletions system/Config/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ class Factory
*/
protected static $configs = [];

/**
* Explicit configuration for the Config
* component to prevent logic loops.
*
* @var array<string, array>
*/
private static $configValues = [
'component' => 'config',
'path' => 'Config',
'instanceOf' => null,
'prefersApp' => true,
];

/**
* Mapping of class basenames (no namespace) to
* their instances.
Expand Down Expand Up @@ -211,8 +224,16 @@ public static function getConfig(string $component): array
return self::$configs[$component];
}

// Load the best Factories config (will include Registrars)
$values = config('Factories')->$component ?? [];
// Handle Config as a special case to prevent logic loops
if ($component === 'config')
{
$values = self::$configValues;
}
// Load values from the best Factories configuration (will include Registrars)
else
{
$values = config('Factories')->$component ?? [];
}

return self::setConfig($component, $values);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testCreateNonConfig()
public function testInjection()
{
Config::reset();
Config::injectMock('Banana', '\stdClass');
Config::injectMock('Banana', new \stdClass());
$this->assertNotNull(Config::get('Banana'));
}

Expand Down

0 comments on commit 9191688

Please sign in to comment.