Skip to content

Commit d6cb6d6

Browse files
committed
~ fix case sensitive name storing
~ fix incorrect non-class files handling + add tests for all cases Signed-off-by: Christoph Potas <[email protected]>
1 parent a568650 commit d6cb6d6

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

system/Autoloader/FileLocator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public function getClassname(string $file) : string
194194
}
195195
}
196196

197+
if( empty( $class_name ) ) return "";
198+
197199
return $namespace .'\\'. $class_name;
198200
}
199201

system/Config/Config.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2828
* THE SOFTWARE.
2929
*
30-
* @package CodeIgniter
31-
* @author CodeIgniter Dev Team
32-
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
33-
* @license https://opensource.org/licenses/MIT MIT License
34-
* @link https://codeigniter.com
35-
* @since Version 3.0.0
30+
* @package CodeIgniter
31+
* @author CodeIgniter Dev Team
32+
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
33+
* @license https://opensource.org/licenses/MIT MIT License
34+
* @link https://codeigniter.com
35+
* @since Version 3.0.0
3636
* @filesource
3737
*/
3838

@@ -51,56 +51,72 @@ class Config
5151
*/
5252
static private $instances = [];
5353

54+
//--------------------------------------------------------------------
55+
5456
/**
55-
* @param string $name
56-
* @param bool $getShared
57+
* Create new configuration instances or return
58+
* a shared instance
59+
*
60+
* @param string $name Configuration name
61+
* @param boolean $getShared Use shared instance
5762
*
5863
* @return mixed|null
5964
*/
6065
public static function get(string $name, bool $getShared = true)
6166
{
6267
$class = $name;
63-
if( ($pos = strrpos($name, '\\')) !== -1 )
68+
if (($pos = strrpos($name, '\\')) !== false)
6469
{
65-
$class = substr($name, $pos+1);
70+
$class = substr($name, $pos + 1);
6671
}
6772

73+
$class = strtolower($class);
74+
6875
if (! $getShared)
6976
{
7077
return self::createClass($name);
7178
}
7279

73-
if( !isset( self::$instances[$class] ) )
80+
if (! isset( self::$instances[$class] ))
7481
{
7582
self::$instances[$class] = self::createClass($name);
7683
}
7784
return self::$instances[$class];
7885
}
7986

87+
//--------------------------------------------------------------------
88+
8089
/**
81-
* @param string $name
90+
* Find configuration class and create instance
91+
*
92+
* @param string $name Classname
8293
*
8394
* @return mixed|null
8495
*/
8596
private static function createClass(string $name)
8697
{
87-
if( class_exists($name))
98+
if (class_exists($name))
8899
{
89100
return new $name();
90101
}
91102

92103
$locator = Services::locator();
93-
$file = $locator->locateFile($name,'Config');
104+
$file = $locator->locateFile($name, 'Config');
94105

95106
if (empty($file))
96107
{
97108
return null;
98109
}
99110

100-
$classname = $locator->getClassname($file);
111+
$name = $locator->getClassname($file);
101112

102-
return new $classname();
103-
}
113+
if (empty($name))
114+
{
115+
return null;
116+
}
104117

118+
return new $name();
119+
}
105120

106-
}
121+
//--------------------------------------------------------------------
122+
}

tests/system/Config/ConfigTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace CodeIgniter\Config;
2+
3+
use Config\Email;
4+
5+
class ConfigTest extends \CIUnitTestCase
6+
{
7+
8+
public function testCreateSingleInstance()
9+
{
10+
$Config = Config::get('email', false);
11+
$UpperConfig = Config::get('Email', false);
12+
$NamespaceConfig = Config::get('Config\\Email', false);
13+
14+
$this->assertInstanceOf(Email::class, $Config);
15+
$this->assertInstanceOf(Email::class, $UpperConfig);
16+
$this->assertInstanceOf(Email::class, $NamespaceConfig);
17+
}
18+
19+
public function testCreateInvalidInstance()
20+
{
21+
$Config = Config::get('gfnusvjai', false);
22+
23+
$this->assertNull($Config);
24+
}
25+
26+
public function testCreateSharedInstance()
27+
{
28+
$Config = Config::get('email' );
29+
$Config2 = Config::get('Config\\Email');
30+
31+
$this->assertTrue($Config === $Config2);
32+
}
33+
34+
public function testCreateNonConfig()
35+
{
36+
$Config = Config::get('constants', false);
37+
38+
$this->assertNull($Config);
39+
}
40+
}

0 commit comments

Comments
 (0)