jameslevi/silhouette

Is a simple configuration management library that supports multiple configuration file formats.

v1.0.1 2021-05-06 18:05 UTC

This package is not auto-updated.

Last update: 2024-05-03 09:18:50 UTC


README

Is a simple configuration management library that supports multiple configuration file formats.

Features

  1. Supports php array and json format.
  2. Facade implementation to beautifully access your configuration data.
  3. Easy integration with other frameworks or even without framework at all.

Installation

  1. You can install via composer.
composer require jameslevi/silhouette
  1. Include the autoloader at the upper part of your code.
require_once __DIR__.'/vendor/autoload.php';

Getting Started

  1. Import the config class into your project.
use Graphite\Component\Silhouette\Config;
  1. 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,
];
  1. Instantiate a new config object.
$db = new Config(__DIR__ . "/config/db.php");
  1. 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".
  1. You can also set add new value into your config.
$db->add('charset', 'utf-8'); // This will add "charset" as new config data.
  1. 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".
  1. You can check if keyword exists using "has" method.
$db->has('password'); // Returns true.
  1. You can remove a configuration data using "remove" method.
$db->remove('enable'); // This will remove "enable" from your configuration object.
  1. You can return your configuration data in array.
$db->toArray();
  1. 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

  1. 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');
    }
}
  1. 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.
  1. You can edit configuration value by providing the first argument of the method called.
DB::enable(false) // Change the value of "enable" to false.
  1. 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.