2017 © Pedro Peláez
 

library configurator

Inject configuration data into your classes

image

codezero/configurator

Inject configuration data into your classes

  • Thursday, February 5, 2015
  • by codezero
  • Repository
  • 2 Watchers
  • 1 Stars
  • 4,927 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 8 % Grown

The README.md

Class Configurator

Build Status Latest Stable Version Total Downloads License, (*1)

This package allows you to easily inject configuration files into you own classes., (*2)

Installation

Download this package or install it through Composer:, (*3)

"require": {
    "codezero/configurator": "3.*"
}

Usage

Define a configuration

Specify an array..., (*4)

$config = [
    'my_setting' => 'some value',
    'my_other_setting' => 'some other value'
];

Or refer to a configuration file..., (*5)

$config = '/path/to/configFile.php';

That configuration file could look like this:, (*6)

<?php
return [
    'my_setting' => 'some value',
    'my_other_setting' => 'some other value'
];

Use Configurator in your class

Inject a Configurator implementation in your class. If none is supplied, the default one will be instantiated. The $configurator->load() method will return a Configuration object or throw a ConfigurationException if no valid array could be loaded., (*7)

use CodeZero\Configurator\Configurator;
use CodeZero\Configurator\DefaultConfigurator;

class MyClass {

    private $config;

    public function __construct($config, Configurator $configurator = null)
    {
        $configurator = $configurator ?: new DefaultConfigurator();
        $this->config = $configurator->load($config);
    }
}

Instantiate your class

Create an instance of your class, passing it the configuration array or file:, (*8)

$myClass = new MyClass($config);

Use the Configuration in your class

Get configuration values:, (*9)

$mySetting = $this->config->get('my_setting');
$myOtherSetting = $this->config->get('my_other_setting');

Set configuration values at runtime:, (*10)

$this->config->set('my_setting', 'some new value');

And that's all there is to it..., (*11)

Laravel 5 Usage

IoC binding

If you use Laravel, then you can setup a binding that resolves your class with its configuration automatically. Let's say you have a class Acme\MyApp\MyClass:, (*12)

App::bind('Acme\MyApp\MyClass', function($app)
{
    // Specify an array...
    $config = [
        'my_setting' => 'some value',
        'my_other_setting' => 'some other value'
    ];

    // Or refer to a configuration file...
    $config = '/path/to/configFile.php';

    return new \Acme\MyApp\MyClass($config);
});

Use Laravel's Config infrastructure

What if you don't want to hardcode an array or a file path in your bindings, but instead you want to make use of laravel's Config infrastructure? Let's imagine that you create a Laravel configuration file at config/myapp.php. You could then use this in your binding:, (*13)

$config = $app['config']->get("myapp");

Simple as that..., (*14)


Analytics, (*15)

The Versions

05/02 2015

dev-master

9999999-dev

Inject configuration data into your classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel php configuration config loader configurator

05/02 2015

3.0.1

3.0.1.0

Inject configuration data into your classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel php configuration config loader configurator

03/02 2015

3.0.0

3.0.0.0

Inject configuration data into your classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel php configuration config loader configurator

03/02 2015

2.0.0

2.0.0.0

Inject configuration data into your classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel php configuration config loader configurator

26/08 2014

1.0.0

1.0.0.0

Inject configuration data into your classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel php configuration config loader configurator