An abstraction layer to get data from array or a file with dot-notation

Overview

Alex Unruh - Config

This library is based on the Laravel config concept. It values performance and was built on top of the library Dflydev Dot Access data.

The difference is that we can also access the data from a file instead of just an array in runtime execution.

How to install:

composer require alex-unruh/dot-notation-config

Usage with files:

You can have as many configuration files as you want

// config/app.php

return [
  'app_name' => 'My App',

  'app_version' => '1.0.0',

  'connection_params' => [
    'host' => 'localhost',
    'dbname' => 'my_database',
    'user' => 'root',
    'password' => '',
    'port' => '3306'
  ]
];

// config/messages.php

return [
  'internal_error' => 'Internal server error',
  400 => 'Bad request'
];

// index.php

use AlexUnruh\Config;

Config::setDir('/config');

// Search data in /config/app.php file
echo Config::get('app.app_name'); // 'My App'
echo Config::get('app.connection_params.host'); // 'localhost'

// Search data in /config/messages.php file
echo Config::get('messages.400'); // 'Bad request'

print_r(Config::get('app')); // Returns all the array data placed in the app file.

Usage with virtual data:

// index.php

use AlexUnruh\Config;

$data = [
  'app_name' => 'My App',

  'app_version' => '1.0.0',

  'connection_params' => [
    'host' => 'localhost',
    'dbname' => 'my_database',
    'user' => 'root',
    'password' => '',
    'port' => '3306'
  ]
];

Config::setData('my_data', $data);

echo Config::get('my_data.app_name'); // 'My App'
echo Config::get('my_data.connection_params.host'); // 'localhost'

print_r(Config::get('my_data')); // Returns all the array data placed in the my_data array.

Methods:

The methods is the same presents in Dflydev Dot Access data. The difference is that the first argument in dot-notations is a file or a virtual config set alias defined on setDir or setData methods described above.

Methods: setDir, setData, get, set, has, remove and append.

// $my_config_dir = '/my-config-dir'
Config::setDir($my_config_dir);

// $my_array = ['app_name' => 'My App', 'app_version' => '1.0.0']
Config::setData($my_array);

// $my_array_search = 'app.app_name'
// $default_if_key_not_exists = 'My App'
Config::get($my_array_search, $default_if_key_not_exists); // 'My App'

// $my_array_item = 'app.name'
// $my_new_value = 'My New App Name'
Config::set($my_array_item, $my_new_value);

// $my_array_data = 'app'
// $the_key_im_looking_for = 'app_version'
Config::has($my_array_data, $the_key_im_looking_for); // true

// $my_array_item = 'app.app_licence'
// $value_to_append = 'MIT'
Config::append($my_array_item, $value_to_append);

// $my_array_item = 'app'
// $value_to_remove = 'app_licence'
Config::remove($my_array_item, $value_to_remove);

Tricks:

As the library only has static methods, you can set the configuration files directory at any time or in any file called before manipulating the data through the class's methods.

Define the config dir in a file like a entry point and don't worry about him anymore...

// public/index.php

$config_path = $_SERVER['DOCUMENT_ROOT'] . $_ENV['BASE_PATH'] . '/config';
Config::setDir($config_path);

// controllers/services/MyService.php

echo Config::get('app.app_name'); // 'My App'
You might also like...
A non-blocking stream abstraction for PHP based on Amp.
A non-blocking stream abstraction for PHP based on Amp.

amphp/byte-stream is a stream abstraction to make working with non-blocking I/O simple. Installation This package can be installed as a Composer depen

A simple artisanal command framework for creating service layer classes

Introdução Este projeto tem como objetivo fornecer alguns comandos adicionais à interface de linha de comando do Laravel para manipular a estrutura da

Sebuah aplikasi file hosting sederhana yang berguna untuk menyimpan berbagai file

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Get started using SELCOM APIs with Laravel framework

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Gallium is a TALL stack starter kit offering a robust set of options enabling you to get up and running in a snap.

Very short description of the package This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention o

This is a solution implementation for the coderbyte question, CLEAN GET REQUEST RESULT.

This is a solution implementation for the coderbyte question, CLEAN GET REQUEST RESULT. Two solutions are proposed, the first is a brute force approach while the other is an improved time complexity solution.

Get the thumbnail of youtube and vimeo videos from the url. The returned information is ID and URL of the thumbnail

Video Thumbnail URL Get the thumbnail of youtube and vimeo videos from the url. The returned information is ID and URL of the thumbnail Installation I

Laravel && ( Swoole || Workerman ) to get 10x faster than php-fpm

Laravoole Laravel on Swoole Or Workerman 10x faster than php-fpm Depends On php =5.5.16 laravel/laravel ^ 5.1 Suggests php =7.0.0 ext-swoole =1.7.2

Get info from any web service or page

Embed PHP library to get information from any web page (using oembed, opengraph, twitter-cards, scrapping the html, etc). It's compatible with any web

Releases(v1.0.0)
Owner
Alexandre Odair
Systems Analyst an full stack developer. Married, father of two, from São Paulo - Brazil.
Alexandre Odair
This project uses dflydev/dot-access-data to provide simple output filtering for cli applications.

FilterViaDotAccessData This project uses dflydev/dot-access-data to provide simple output filtering for applications built with annotated-command / Ro

Consolidation 44 Jul 19, 2022
An abstraction layer for easily implementing industry-standard caching strategies

Laravel Model Repository This package provides an abstraction layer for easily implementing industry-standard caching strategies with Eloquent models.

null 44 Dec 31, 2022
Get estimated read time of an article. Similar to medium.com's "x min read". Multilingual including right-to-left written languages. Supports JSON, Array and String output.

Read Time Calculates the read time of an article. Output string e.g: x min read or 5 minutes read. Features Multilingual translations support. Static

Waqar Ahmed 8 Dec 9, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
Renamify is a package for Laravel used to rename a file before uploaded to prevent replacing exists file which has the same name to this new uploaded file.

Renamify Laravel package for renaming file before uploaded on server. Renamify is a package for Laravel used to rename a file before uploaded to preve

MB'DUSENGE Callixte 2 Oct 11, 2022
Laravel 5 Repositories is used to abstract the data layer, making our application more flexible to maintain.

Laravel 5 Repositories is used to abstract the data layer, making our application more flexible to maintain.

Anderson Andrade 4k Jan 6, 2023
Laravel 5.* package to easily introduce a transformation layer for your data

Laraformer Laraformer is a laravel 5.* package that lets you easily introduce a transformation layer for your data. Laraformer (originated from Larave

Kamran Ahmed 30 Oct 19, 2022
Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Egor Shitikov 87 Nov 26, 2022
An easy way to get vendor and package data from Packagist via API calls

Laravel Packagist Laravel Packagist (LaravelPackagist) is a package for Laravel 5 to interact with the packagist api quickly and easily. Table of cont

Jeremy Kenedy 5 Jul 18, 2022
These are simple array and object collections that provide convinient methods to manipulate them.

Simple Collections These are simple array and object collections that provide convinient methods to manipulate collections; To install this package ty

Artem 4 Nov 19, 2021