Config
, (*1)
Config component of the SlaxWeb\Framework handles loading and parsing of
configuration options from multiple sources. Currently only file based
resources are supported, and can be in 3 formats, PHP, XML, or Yaml., (*2)
Requirements
- PHP 7.0+
- desperado/xml-bundle 0.1.* package - for XML configuration handler
- symfony/yaml 3.0.* package - for Yaml configuration handler
- pimple/pimple 3.0.* package - to use the provided service provider
Installation
Easiest form of installation is through composer,
just require the package in your composer.json file:, (*3)
{
"require": {
"slaxweb/config": "~0.1"
}
}
And this should get you started to use the Config component, with the PHP
configuration handler. If you want to use XML or Yaml configuration providers,
you need to install their respective required packages with composer (see
Requirements)., (*4)
Usage
The Config component provides you a Factory class for easier instantiation.
To get started, simply call the init static method of the Factory with
the correct constant for the configuration handler you want to use, and the path
to your configuration file location. Configuration handler constants:
* \SlaxWeb\Config\Container::PHP_CONFIG_HANDLER
* \SlaxWeb\Config\Container::XML_CONFIG_HANDLER
* \SlaxWeb\Config\Container::YAML_CONFIG_HANDLER, (*5)
$config = \SlaxWeb\Config\Factory::init(
\SlaxWeb\Config\Container::PHP_CONFIG_HANDLER,
"/path/to/configuration/"
);
The factory will automatically instantiate the correct configuration handler
and inject it to the Config class, along with your provided configuration
resource location., (*6)
Manipulating configuration
The Container class implements ArrayAccess and must be used as such.
Retrieving, setting, removing and checking for existence, is done like if it
were on an array. For loading of new configuration resources, the load
method is provided. Example PHP configuration file:, (*7)
<?php
$configuration["foo"] = "bar";
$configuration["baz"] = true;
The PHP configuration file requires the $configuration array, and all of your
configuration items must be set to it., (*8)
To load the configuration file, just place it inside your
/path/to/configuration and call load method with the file name:, (*9)
$config->load("myconfig.php");
After the configuration file has been loaded you can normally do operations on
$config as if it is a simple array:, (*10)
if (isset($config["foo"])) {
$foo = $config["foo"];
}
$config["foo"] = "baz";
unset($config["baz"]);
Using the Provider
If you are using the Pimple\Pimple Dependency Injection Container, you can use
the provided Service Provider. Make sure that before you are going to use the
config.service, that you have set the configResourceLocation, and the
configHandler properties in your container. To use the provider, simply
register it with your container:, (*11)
<?php
use Pimple\Container;
$container = new Container;
$container->register(new \SlaxWeb\Config\Service\Provider);
$container["configHandler"] = \SlaxWeb\Config\Container::PHP_CONFIG_HANDLER;
$container["configResourceLocation"] = "/path/to/configuration";
$container["service.provider"]->load("myconfig.php");