Git Product home page Git Product logo

config-json's Introduction

JSON Configuration Driver for CommonPHP

This library provides a JSON configuration driver, JsonConfigurationDriver, which implements the ConfigurationDriverContract from the CommonPHP Configuration Management library. It enables applications to load and save configurations in JSON format, leveraging the CommonPHP Driver Management system for seamless integration.

Features

  • Load JSON Configurations: Effortlessly read and decode JSON files into associative arrays.
  • Save Configurations as JSON: Serialize and save PHP associative arrays back into JSON format files.
  • JSON Validation: Ensures the integrity of the JSON content during the loading process.
  • Exception Handling: Comprehensive error and exception handling for common JSON parsing issues.

Installation

Use Composer to install the Configuration Manager and this driver:

composer require comphp/config
composer require comphp/config-json

Usage

First, ensure that your DriverManager instance is configured to recognize the JSON driver:

use CommonPHP\Drivers\DriverManager;
use CommonPHP\Configuration\Drivers\JsonConfigurationDriver\JsonConfigurationDriver;

$driverManager = new DriverManager();
$driverManager->enable(JsonConfigurationDriver::class);

Then, when using the Configuration Manager to load or save a JSON file, the JsonConfigurationDriver will automatically be utilized for .json extensions, thanks to the #[ConfigurationDriverAttribute('json')] attribute.

Loading a Configuration File

$configManager->loadDriver(JsonConfigurationDriver::class);
$config = $configManager->get('path/to/configuration.json');

Saving a Configuration File

Ensure the driver has been loaded as shown above, then:

$config->data['newKey'] = 'newValue';
$config->save(); // Saves the modifications back to 'path/to/configuration.json'

Exception Handling

  • JsonException: Thrown if the JSON file contains invalid JSON.
  • Exception: General exceptions for file read/write failures or JSON decoding issues.

This driver provides a simple yet powerful means to work with JSON-based configurations within the CommonPHP framework, ensuring flexibility and ease of use for developers.

config-json's People

Contributors

tlmcclatchey avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.