diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 4849b295e7..1a301aa035 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -151,6 +151,56 @@ class FileLocator //-------------------------------------------------------------------- + /** + * Examines a file and returns the fully qualified domain name. + * + * @param string $file + * + * @return string + */ + public function getClassname(string $file) : string + { + $php = file_get_contents($file); + $tokens = token_get_all($php); + $count = count($tokens); + $dlm = false; + $namespace = ''; + $class_name = ''; + + for ($i = 2; $i < $count; $i++) + { + 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)) + { + if (! $dlm) + { + $namespace = 0; + } + if (isset($tokens[$i][1])) + { + $namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1]; + $dlm = true; + } + } + elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING)) + { + $dlm = false; + } + if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass")) + && $tokens[$i-1][0] == T_WHITESPACE + && $tokens[$i][0] == T_STRING) + { + $class_name = $tokens[$i][1]; + break; + } + } + + if( empty( $class_name ) ) return ""; + + return $namespace .'\\'. $class_name; + } + + //-------------------------------------------------------------------- + /** * Searches through all of the defined namespaces looking for a file. * Returns an array of all found locations for the defined file. diff --git a/system/Common.php b/system/Common.php index 6fceec13f4..72cbe76ed9 100644 --- a/system/Common.php +++ b/system/Common.php @@ -85,6 +85,24 @@ if ( ! function_exists('cache')) //-------------------------------------------------------------------- +if ( ! function_exists('config')) +{ + /** + * More simple way of getting config instances + * + * @param string $name + * @param bool $getShared + * + * @return mixed + */ + function config(string $name, bool $getShared = true) + { + return \CodeIgniter\Config\Config::get($name, $getShared); + } +} + +//-------------------------------------------------------------------- + if ( ! function_exists('view')) { diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index 1fa59aaa83..b8ffa3b27c 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -36,6 +36,8 @@ * @filesource */ +use CodeIgniter\Autoloader\FileLocator; + /** * Services Configuration file. * @@ -191,7 +193,7 @@ class BaseService // Get instances of all service classes and cache them locally. foreach ($files as $file) { - $classname = static::getClassName($file); + $classname = $locator->getClassname($file); if (! in_array($classname, ['Config\\Services', 'CodeIgniter\\Config\\Services'])) { @@ -214,50 +216,4 @@ class BaseService } } } - - /** - * Examines a file and returns the fully qualified domain name. - * - * @param string $file - * - * @return string - */ - private static function getClassname(string $file) - { - $php = file_get_contents($file); - $tokens = token_get_all($php); - $count = count($tokens); - $dlm = false; - $namespace = ''; - $class_name = ''; - - for ($i = 2; $i < $count; $i++) - { - 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)) - { - if (! $dlm) - { - $namespace = 0; - } - if (isset($tokens[$i][1])) - { - $namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1]; - $dlm = true; - } - } - elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING)) - { - $dlm = false; - } - if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass")) - && $tokens[$i-1][0] == T_WHITESPACE - && $tokens[$i][0] == T_STRING) - { - $class_name = $tokens[$i][1]; - break; - } - } - - return $namespace .'\\'. $class_name; - } } diff --git a/system/Config/Config.php b/system/Config/Config.php new file mode 100644 index 0000000000..0cf79dd3e7 --- /dev/null +++ b/system/Config/Config.php @@ -0,0 +1,122 @@ +locateFile($name, 'Config'); + + if (empty($file)) + { + return null; + } + + $name = $locator->getClassname($file); + + if (empty($name)) + { + return null; + } + + return new $name(); + } + + //-------------------------------------------------------------------- +} diff --git a/tests/system/Config/ConfigTest.php b/tests/system/Config/ConfigTest.php new file mode 100644 index 0000000000..b1e8397fff --- /dev/null +++ b/tests/system/Config/ConfigTest.php @@ -0,0 +1,40 @@ +assertInstanceOf(Email::class, $Config); + $this->assertInstanceOf(Email::class, $UpperConfig); + $this->assertInstanceOf(Email::class, $NamespaceConfig); + } + + public function testCreateInvalidInstance() + { + $Config = Config::get('gfnusvjai', false); + + $this->assertNull($Config); + } + + public function testCreateSharedInstance() + { + $Config = Config::get('email' ); + $Config2 = Config::get('Config\\Email'); + + $this->assertTrue($Config === $Config2); + } + + public function testCreateNonConfig() + { + $Config = Config::get('constants', false); + + $this->assertNull($Config); + } +} diff --git a/user_guide_src/source/general/configuration.rst b/user_guide_src/source/general/configuration.rst index af63b1b05d..cd3860602e 100644 --- a/user_guide_src/source/general/configuration.rst +++ b/user_guide_src/source/general/configuration.rst @@ -11,11 +11,21 @@ create an instance of the class and all your settings are there for you. Accessing Config Files ====================== -You can access config files within your classes by creating a new instance. All of the properties +You can access config files within your classes by creating a new instance or using the config function. All of the properties are public, so you access the settings like any other property:: + // Creating new class by hand $config = new \Config\EmailConfig(); + // Creating new class with config function + $config = config( 'EmailConfig', false ); + + // Get shared instance with config function + $config = config( 'EmailConfig' ); + + // Access config class with namespace + $config = config( 'Config\\EmailConfig' ); + // Access settings as class properties $protocol = $config->protocol; $mailpath = $config->mailpath;