Refactor DI config to be a simple object to match our new config methodology.

This commit is contained in:
Lonnie Ezell 2015-09-30 23:20:19 -05:00
parent 8aa52012a7
commit 6b3425fa6b
3 changed files with 39 additions and 35 deletions

View File

@ -1,37 +1,38 @@
<?php
/**
* -------------------------------------------------------------------
* SERVICES
* -------------------------------------------------------------------
* This file contains a map of name-spaced classes and their aliases.
* These are used by the DI class to provide instances of classes.
*
* The 'alias' (key) is how you will reference the class instance through DI.
* The class name (value) is the fully name-spaced class name to use.
* If you want to substitute a different class in place of the current one,
* just change the name of the class to the fully name-spaced class you
* want to use.
*
* Examples:
* $ci = \CodeIgniter\DI::getInstance();
* $bm = $ci->benchmark;
* $ci->benchmark->mark('some_mark_start');
*/
class ServicesConfig {
$config['services'] = [
/**
* -------------------------------------------------------------------
* SERVICES
* -------------------------------------------------------------------
* This file contains a map of name-spaced classes and their aliases.
* These are used by the DI class to provide instances of classes.
*
* The 'alias' (key) is how you will reference the class instance through DI.
* The class name (value) is the fully name-spaced class name to use.
* If you want to substitute a different class in place of the current one,
* just change the name of the class to the fully name-spaced class you
* want to use.
*
* Examples:
* $ci = \CodeIgniter\DI::getInstance();
* $bm = $ci->benchmark;
* $ci->benchmark->mark('some_mark_start');
*/
public $services = [
// alias class name
//--------------------------------------------------------------------
// alias class name
//--------------------------------------------------------------------
// The core CodeIgniter files
'autoloader' => '\CodeIgniter\Autoloader\Autoloader',
'bmtimer' => '\CodeIgniter\Benchmark\Timer',
'bmiterator' => '\CodeIgniter\Benchmark\Iterator',
'router' => '\CodeIgniter\Router\Router',
'routes' => '\CodeIgniter\Router\RouteCollection'
// The core CodeIgniter files
'autoloader' => '\CodeIgniter\Autoloader\Autoloader',
'bmtimer' => '\CodeIgniter\Benchmark\Timer',
'bmiterator' => '\CodeIgniter\Benchmark\Iterator',
'router' => '\CodeIgniter\Router\Router',
'routes' => '\CodeIgniter\Router\RouteCollection'
// Your custom files can be added here.
];
// Your custom files can be added here.
];
return $config;
}

View File

@ -44,11 +44,12 @@ require_once BASEPATH.'Common.php';
*/
require_once BASEPATH.'DI/DI.php';
require_once APPPATH.'config/services.php';
// This is the only time that services array will need
// to be passed into the class. All other uses can
// simply call getInstance().
$di = CodeIgniter\DI\DI::getInstance(get_config('services'));
$di = CodeIgniter\DI\DI::getInstance(new ServicesConfig());
/*
* ------------------------------------------------------

View File

@ -74,14 +74,16 @@ class DI
* The constructor is kept private to ensure that
* this class can only be used as a singleton DI container.
*/
private function __construct(array $config = [])
private function __construct(\ServicesConfig $config)
{
if (empty($config['services']))
if (empty($config->services))
{
throw new \InvalidArgumentException('The services configuration file does not contain a valid services array.');
}
$this->services = $config['services'];
$this->services = $config->services;
unset($config);
}
//--------------------------------------------------------------------
@ -103,7 +105,7 @@ class DI
* $this->di = $di;
* }
*/
public static function getInstance(array $config = []): self
public static function getInstance(\ServicesConfig $config=null): self
{
if (empty(static::$instance))
{