jameslevi / silhouette
Is a simple configuration management library that supports multiple configuration file formats.
v1.0.1
2021-05-06 18:05 UTC
Requires
- php: >=5.3.0
- jameslevi/objectify: ^1.0
- jameslevi/string: ^1.0
This package is not auto-updated.
Last update: 2025-05-02 14:07:02 UTC
README
Is a simple configuration management library that supports multiple configuration file formats.
Features
- Supports php array and json format.
- Facade implementation to beautifully access your configuration data.
- Easy integration with other frameworks or even without framework at all.
Installation
- You can install via composer.
composer require jameslevi/silhouette
- Include the autoloader at the upper part of your code.
require_once __DIR__.'/vendor/autoload.php';
Getting Started
- Import the config class into your project.
use Graphite\Component\Silhouette\Config;
- Create a new config file in php or json. But for this example let's try a php file that returns an array.
<?php return [ 'enable' => true, 'port' => 3306, 'host' => 'localhost', 'username' => 'root', 'password' => 'abcd', 'database' => 'users', 'max-rows' => 10000, ];
- Instantiate a new config object.
$db = new Config(__DIR__ . "/config/db.php");
- You can now get data using "get" method.
$db->get('enable'); // This will return true.
You can also get data using object keys.
$db->username; // This will return "root".
- You can also set add new value into your config.
$db->add('charset', 'utf-8'); // This will add "charset" as new config data.
- You can edit config data using "set" method.
$db->set('database', 'photos'); // This will change the value of the key database.
You can also set data using object keys.
$db->database = 'photos'; // This will change the value of database from "users" to "photos".
- You can check if keyword exists using "has" method.
$db->has('password'); // Returns true.
- You can remove a configuration data using "remove" method.
$db->remove('enable'); // This will remove "enable" from your configuration object.
- You can return your configuration data in array.
$db->toArray();
- You can also return json format of your configuration data.
$db->toJson();
Config Injection
You can also make a config object without loading a config file.
$db = new Config([ 'enable' => true, 'port' => 3306, 'host' => 'localhost', 'username' => 'root', 'password' => 'abcd', 'database' => 'users', 'max-rows' => 10000, ]);
Facade
- Create a class that extends silhouette facade class then override the parent constructor by passing the config path.
<?php namespace App\Config; use Graphite\Component\Silhouette\Facade; class DB extends Facade { public function __construct() { parent::__construct('config/db.php'); } }
- You can now get each configurations by calling it's static methods.
DB::enable() // Returns the value of enable property.
You must call properties that are in snake case to camel case.
DB::maxRows() // Returns the value 10000.
- You can edit configuration value by providing the first argument of the method called.
DB::enable(false) // Change the value of "enable" to false.
- You can add, edit or delete configuration data using config "context".
DB::context()->add('min_rows', 100); // This will add new configuration property. DB::context()->set('min_rows', 110); // This will set the value of "min_rows". DB::context()->remove('min-rows'); // This will remove "min_rows" from the data object.
Muted Configuration Object
Configuration objects that only returns data.
// Create a new database configuration object. $config = new Config(__DIR__ . '/config/db.php', true); // You cannot add new data to your muted configuration. $config->add('driver', 'mysql');
You can also mute config object in facade.
<?php namespace App\Config; use Graphite\Component\Silhouette\Facade; class DB extends Facade { public function __construct() { parent::__construct('config/db.php', true); } }
Contribution
For issues, concerns and suggestions, you can email James Crisostomo via nerdlabenterprise@gmail.com.
License
This package is an open-sourced software licensed under MIT License.