Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 2.68 KB

README.md

File metadata and controls

94 lines (70 loc) · 2.68 KB

Yohns\Core\Config

Base configuration class that stores the value from returning arrays in php files.

Methods

Name Description
__construct Config constructor.
get Retrieves a configuration value.
getAll Retrieve all configuration values for file.
getCustom Retrieves a custom configuration value.
reload Reloads configurations from a specified directory.
set Sets a configuration value.

Tip

Add, Edit, and Create Configs

  • Create new config files (for new repos that may get added?),
  • Add new key => value pairs to a config file already found.
  • Edit values for predefined configs, you have
    • You have to set the allow override option to true, default is false Removed editing because it doubles up the same key.

Methods

Name Description
addToConfig Adds key-value pairs to a configuration array if they do not already exist in the specified configuration file. If the file does not exist, it creates a new configuration file with the provided data.


Put all config files in 1 directory and then call that directory and it'll load all the config files to the variable

Check out the Example File

Use composers autoload or include path to the Core/Config.php file

Example using Config

use Yohns\Core\Config;

include('vendor/autoload.php');

$dir = __DIR__.'/lib/Config';

// Initialize Config with a specific directory
new Config($dir);

// Get a configuration value
echo Config::get('users', 'db_tables').PHP_EOL;

// Set a custom configuration value
Config::set('api_key', '12345');

// Retrieve a custom configuration value
echo Config::getCustom('api_key').PHP_EOL;

Example ConfigEditor

use Yohns\Core\Config;
use Yohns\Core\ConfigEditor;

include('vendor/autoload.php');

$dir = __DIR__.'/lib/Config';

// Initialize Config with a specific directory
new Config($dir);

// Editor class allows us to append key=>values to the config files, or create a new config file if not found.
ConfigEditor::addToConfig(
	['add-new' => 'value'],
	'default',
	// only set to true if you want to "edit" the value if found in config file already.
	// default is false.
	true);
Config::reload($dir);

// get from the 'default' configs do not need to mention the file in get()
echo Config::get('add-new').PHP_EOL;

Example code uses the config/ directory found in this repo.

config/default.php:

<?php
return [
	'siteName' => 'Testing'
];