Skip to content

Commit e0501ac

Browse files
authored
Merge pull request #1073 from puschie286/ConfigHelper
Config helper
2 parents 6d74307 + cc45b5a commit e0501ac

File tree

6 files changed

+244
-48
lines changed

6 files changed

+244
-48
lines changed

system/Autoloader/FileLocator.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,56 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
151151

152152
//--------------------------------------------------------------------
153153

154+
/**
155+
* Examines a file and returns the fully qualified domain name.
156+
*
157+
* @param string $file
158+
*
159+
* @return string
160+
*/
161+
public function getClassname(string $file) : string
162+
{
163+
$php = file_get_contents($file);
164+
$tokens = token_get_all($php);
165+
$count = count($tokens);
166+
$dlm = false;
167+
$namespace = '';
168+
$class_name = '';
169+
170+
for ($i = 2; $i < $count; $i++)
171+
{
172+
if ((isset($tokens[$i-2][1]) && ($tokens[$i-2][1] == "phpnamespace" || $tokens[$i-2][1] == "namespace")) || ($dlm && $tokens[$i-1][0] == T_NS_SEPARATOR && $tokens[$i][0] == T_STRING))
173+
{
174+
if (! $dlm)
175+
{
176+
$namespace = 0;
177+
}
178+
if (isset($tokens[$i][1]))
179+
{
180+
$namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1];
181+
$dlm = true;
182+
}
183+
}
184+
elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING))
185+
{
186+
$dlm = false;
187+
}
188+
if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass"))
189+
&& $tokens[$i-1][0] == T_WHITESPACE
190+
&& $tokens[$i][0] == T_STRING)
191+
{
192+
$class_name = $tokens[$i][1];
193+
break;
194+
}
195+
}
196+
197+
if( empty( $class_name ) ) return "";
198+
199+
return $namespace .'\\'. $class_name;
200+
}
201+
202+
//--------------------------------------------------------------------
203+
154204
/**
155205
* Searches through all of the defined namespaces looking for a file.
156206
* Returns an array of all found locations for the defined file.

system/Common.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ function cache(string $key = null)
8585

8686
//--------------------------------------------------------------------
8787

88+
if ( ! function_exists('config'))
89+
{
90+
/**
91+
* More simple way of getting config instances
92+
*
93+
* @param string $name
94+
* @param bool $getShared
95+
*
96+
* @return mixed
97+
*/
98+
function config(string $name, bool $getShared = true)
99+
{
100+
return \CodeIgniter\Config\Config::get($name, $getShared);
101+
}
102+
}
103+
104+
//--------------------------------------------------------------------
105+
88106
if ( ! function_exists('view'))
89107
{
90108

system/Config/BaseService.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* @filesource
3737
*/
3838

39+
use CodeIgniter\Autoloader\FileLocator;
40+
3941
/**
4042
* Services Configuration file.
4143
*
@@ -191,7 +193,7 @@ protected static function discoverServices(string $name, array $arguments)
191193
// Get instances of all service classes and cache them locally.
192194
foreach ($files as $file)
193195
{
194-
$classname = static::getClassName($file);
196+
$classname = $locator->getClassname($file);
195197

196198
if (! in_array($classname, ['Config\\Services', 'CodeIgniter\\Config\\Services']))
197199
{
@@ -214,50 +216,4 @@ protected static function discoverServices(string $name, array $arguments)
214216
}
215217
}
216218
}
217-
218-
/**
219-
* Examines a file and returns the fully qualified domain name.
220-
*
221-
* @param string $file
222-
*
223-
* @return string
224-
*/
225-
private static function getClassname(string $file)
226-
{
227-
$php = file_get_contents($file);
228-
$tokens = token_get_all($php);
229-
$count = count($tokens);
230-
$dlm = false;
231-
$namespace = '';
232-
$class_name = '';
233-
234-
for ($i = 2; $i < $count; $i++)
235-
{
236-
if ((isset($tokens[$i-2][1]) && ($tokens[$i-2][1] == "phpnamespace" || $tokens[$i-2][1] == "namespace")) || ($dlm && $tokens[$i-1][0] == T_NS_SEPARATOR && $tokens[$i][0] == T_STRING))
237-
{
238-
if (! $dlm)
239-
{
240-
$namespace = 0;
241-
}
242-
if (isset($tokens[$i][1]))
243-
{
244-
$namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1];
245-
$dlm = true;
246-
}
247-
}
248-
elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING))
249-
{
250-
$dlm = false;
251-
}
252-
if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass"))
253-
&& $tokens[$i-1][0] == T_WHITESPACE
254-
&& $tokens[$i][0] == T_STRING)
255-
{
256-
$class_name = $tokens[$i][1];
257-
break;
258-
}
259-
}
260-
261-
return $namespace .'\\'. $class_name;
262-
}
263219
}

system/Config/Config.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php namespace CodeIgniter\Config;
2+
3+
/**
4+
* CodeIgniter
5+
*
6+
* An open source application development framework for PHP
7+
*
8+
* This content is released under the MIT License (MIT)
9+
*
10+
* Copyright (c) 2014-2018 British Columbia Institute of Technology
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in
20+
* all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
* THE SOFTWARE.
29+
*
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
36+
* @filesource
37+
*/
38+
39+
/**
40+
* Class Config
41+
*
42+
* @package CodeIgniter\Config
43+
*/
44+
class Config
45+
{
46+
/**
47+
* Cache for instance of any configurations that
48+
* have been requested as "shared" instance.
49+
*
50+
* @var array
51+
*/
52+
static private $instances = [];
53+
54+
//--------------------------------------------------------------------
55+
56+
/**
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
62+
*
63+
* @return mixed|null
64+
*/
65+
public static function get(string $name, bool $getShared = true)
66+
{
67+
$class = $name;
68+
if (($pos = strrpos($name, '\\')) !== false)
69+
{
70+
$class = substr($name, $pos + 1);
71+
}
72+
73+
$class = strtolower($class);
74+
75+
if (! $getShared)
76+
{
77+
return self::createClass($name);
78+
}
79+
80+
if (! isset( self::$instances[$class] ))
81+
{
82+
self::$instances[$class] = self::createClass($name);
83+
}
84+
return self::$instances[$class];
85+
}
86+
87+
//--------------------------------------------------------------------
88+
89+
/**
90+
* Find configuration class and create instance
91+
*
92+
* @param string $name Classname
93+
*
94+
* @return mixed|null
95+
*/
96+
private static function createClass(string $name)
97+
{
98+
if (class_exists($name))
99+
{
100+
return new $name();
101+
}
102+
103+
$locator = Services::locator();
104+
$file = $locator->locateFile($name, 'Config');
105+
106+
if (empty($file))
107+
{
108+
return null;
109+
}
110+
111+
$name = $locator->getClassname($file);
112+
113+
if (empty($name))
114+
{
115+
return null;
116+
}
117+
118+
return new $name();
119+
}
120+
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+
}

user_guide_src/source/general/configuration.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ create an instance of the class and all your settings are there for you.
1111
Accessing Config Files
1212
======================
1313

14-
You can access config files within your classes by creating a new instance. All of the properties
14+
You can access config files within your classes by creating a new instance or using the config function. All of the properties
1515
are public, so you access the settings like any other property::
1616

17+
// Creating new class by hand
1718
$config = new \Config\EmailConfig();
1819

20+
// Creating new class with config function
21+
$config = config( 'EmailConfig', false );
22+
23+
// Get shared instance with config function
24+
$config = config( 'EmailConfig' );
25+
26+
// Access config class with namespace
27+
$config = config( 'Config\\EmailConfig' );
28+
1929
// Access settings as class properties
2030
$protocol = $config->protocol;
2131
$mailpath = $config->mailpath;

0 commit comments

Comments
 (0)