Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config helper #1073

Merged
merged 5 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,56 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p

//--------------------------------------------------------------------

/**
* 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.
Expand Down
18 changes: 18 additions & 0 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ function cache(string $key = null)

//--------------------------------------------------------------------

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'))
{

Expand Down
50 changes: 3 additions & 47 deletions system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @filesource
*/

use CodeIgniter\Autoloader\FileLocator;

/**
* Services Configuration file.
*
Expand Down Expand Up @@ -191,7 +193,7 @@ protected static function discoverServices(string $name, array $arguments)
// 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']))
{
Expand All @@ -214,50 +216,4 @@ protected static function discoverServices(string $name, array $arguments)
}
}
}

/**
* 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 changes: 122 additions & 0 deletions system/Config/Config.php
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions tests/system/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 11 additions & 1 deletion user_guide_src/source/general/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down