mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
commit
e0501accf8
@ -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.
|
||||
|
@ -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'))
|
||||
{
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
122
system/Config/Config.php
Normal file
122
system/Config/Config.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php namespace CodeIgniter\Config;
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2018 British Columbia Institute of Technology
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 3.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
*
|
||||
* @package CodeIgniter\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
|
||||
*
|
||||
* @param string $name Configuration name
|
||||
* @param boolean $getShared Use shared instance
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function get(string $name, bool $getShared = true)
|
||||
{
|
||||
$class = $name;
|
||||
if (($pos = strrpos($name, '\\')) !== false)
|
||||
{
|
||||
$class = substr($name, $pos + 1);
|
||||
}
|
||||
|
||||
$class = strtolower($class);
|
||||
|
||||
if (! $getShared)
|
||||
{
|
||||
return self::createClass($name);
|
||||
}
|
||||
|
||||
if (! isset( self::$instances[$class] ))
|
||||
{
|
||||
self::$instances[$class] = self::createClass($name);
|
||||
}
|
||||
return self::$instances[$class];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 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))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$name = $locator->getClassname($file);
|
||||
|
||||
if (empty($name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new $name();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
40
tests/system/Config/ConfigTest.php
Normal file
40
tests/system/Config/ConfigTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php namespace CodeIgniter\Config;
|
||||
|
||||
use Config\Email;
|
||||
|
||||
class ConfigTest extends \CIUnitTestCase
|
||||
{
|
||||
|
||||
public function testCreateSingleInstance()
|
||||
{
|
||||
$Config = Config::get('email', false);
|
||||
$UpperConfig = Config::get('Email', false);
|
||||
$NamespaceConfig = Config::get('Config\\Email', false);
|
||||
|
||||
$this->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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user