Documentation

ConfigurationBuilder
in package

FinalYes

Class ConfigurationBuilder.

Tags
author

Cristiano Cinotti cristianocinotti@gmail.com

Table of Contents

Constants

CACHE_FILE  = 'susina_config_builder.cache'

Properties

$afterParams  : array<string, mixed>
$beforeParams  : array<string, mixed>
$cacheDirectory  : string
$configurationClass  : string
$definition  : ConfigurationInterface|null
$directories  : array<string|int, string>
$files  : array<string|int, string>
$initMethod  : string
$keepFirstXmlTag  : bool
$replaces  : array<string, string>

Methods

addDirectory()  : $this
Add one or more directories where to find the configuration files.
addFile()  : $this
Add one or more file names to the array of configuration files to load.
create()  : static
Static constructor.
getConfiguration()  : object
Return a populated configuration object.
getConfigurationArray()  : array<string|int, mixed>
Return the loaded and processed configuration parameters as an associative array.
keepFirstXmlTag()  : $this
Keep also the first tag of a xml configuration.
populateContainer()  : void
Populate a container object with the configuration values.
setAfterParams()  : $this
Set an array of additional parameters to merge after loading the configuration files.
setBeforeParams()  : $this
Set an array of additional parameters to merge before loading the configuration files.
setCacheDirectory()  : $this
Set the cache directory or the CacheInterface object
setConfigurationClass()  : $this
Set the full class name of the configuration object to instantiate.
setDefinition()  : $this
Set the object to process the configuration parameters.
setDirectories()  : $this
Set the name of the directories where to find the configuration files to load.
setFiles()  : $this
Set the name of the configuration files to load.
setInitMethod()  : $this
Set the method to use to initialize the configuration object.
setReplaces()  : $this
Set an array of parameters to replace.
getDotArray()  : void
Transform an array in dotted notation.
loadConfiguration()  : array<string|int, mixed>
Process and validate the configuration.
loadFromCache()  : array<string|int, mixed>
Load the configuration from cache.
loadParameters()  : array<string|int, mixed>
Load parameters from the configuration files.

Constants

CACHE_FILE

public string CACHE_FILE = 'susina_config_builder.cache'

The name of the cache file.

Properties

$afterParams

private array<string, mixed> $afterParams = []

Additional array of parameters to merge AFTER loading the configuration files.

$beforeParams

private array<string, mixed> $beforeParams = []

Additional array of parameters to merge BEFORE loading the configuration files.

$configurationClass

private string $configurationClass = ''

The configuration class to build.

$definition

private ConfigurationInterface|null $definition = null

The definition object to process the configuration parameters.

$directories

private array<string|int, string> $directories = []

The directories where to find the configuration files.

$initMethod

private string $initMethod = ''

The name of the method to initialize the configuration object. If empty, the builder passes the array of parameters to the constructor.

$keepFirstXmlTag

private bool $keepFirstXmlTag = false

If keep the first xml tag in an xml configuration file.

$replaces

private array<string, string> $replaces = []

An array of key => values elements to replace into the configuration, before parameters resolving and validating.

Methods

addDirectory()

Add one or more directories where to find the configuration files.

public addDirectory(string|SplFileInfo ...$dirs) : $this

Add one or more directories where to find the configuration files. The parameters can contain:

  • the full path name of the directory
  • SplFileInfo object representing a directory where to find the configuration files

This method check if the passed directories are existent and readable, otherwise throws a ConfigurationBuilderException.

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;

$builder = new ConfigurationBuilder();
$builder->addDirectory(__DIR__ . '/app/config', getcwd());
Parameters
$dirs : string|SplFileInfo

The directories to add.

Tags
throws
ConfigurationBuilderException

If a directory does not exist or it's not writeable.

Return values
$this

addFile()

Add one or more file names to the array of configuration files to load.

public addFile(string|SplFileInfo ...$files) : $this

The parameters can contain:

  • the name of the configuration file to load
  • the full path name of the configuration file to load
  • SplFileInfo object representing the configuration file to load

Use this method to add one or more elements to the list of configuration files to load. I.e.:

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;

$builder = new ConfigurationBuilder();
$builder->addFile('my-project-config.yaml.dist', 'my-project-config-yml');
Parameters
$files : string|SplFileInfo

The files to load.

Return values
$this

getConfigurationArray()

Return the loaded and processed configuration parameters as an associative array.

public getConfigurationArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

keepFirstXmlTag()

Keep also the first tag of a xml configuration.

public keepFirstXmlTag([bool $keep = true ]) : $this

When loading XML files, it keeps the first xml tag as part of the configuration. Consider the following xml:

<?xml version='1.0' standalone='yes'?>
<properties>
  <foo>bar</foo>
  <bar>baz</bar>
</properties>

it usually results in the following array:

<?php
    [
        'foo' => 'bar',
        'bar' => 'baz'
    ];

If you call keepFirstXmTag then the resulted array is the following:

<?php
    [
        'properties' => [
            'foo' => 'bar',
            'bar' => 'baz'
        ]
    ];
Parameters
$keep : bool = true
Return values
$this

populateContainer()

Populate a container object with the configuration values.

public populateContainer(object $container, string $method) : void

This method populates a dependency injection container $container with the loaded configuration parameters. You can acces the loaded parameters dot acces key reference (i.e. database.connection.dsn).

Parameters
$container : object

The container object

$method : string

The container method to add a parameter (i.e. set for Php-Di or setParameter for Symfony Dependency Injection).

setAfterParams()

Set an array of additional parameters to merge after loading the configuration files.

public setAfterParams(array<string, mixed> $afterParams) : $this

Set an array of parameters to merge into your configuration after loading the files. These parameters are processed and validated via Symfony\Config, so they must be compatible with the definition class specified in $definition property.

The value of these parameters could overwrite the ones loaded from the configuration files.

Parameters
$afterParams : array<string, mixed>

The array of parameters to be merged.

Return values
$this

setBeforeParams()

Set an array of additional parameters to merge before loading the configuration files.

public setBeforeParams(array<string, mixed> $beforeParams) : $this

Set an array of parameters to merge into your configuration before loading the files. These parameters are processed and validated via Symfony\Config, so they must be compatible with the definition class specified in $definition property.

The value of these parameters could be overwritten by the ones loaded from the configuration files.

Parameters
$beforeParams : array<string, mixed>

The array of parameters to be merged.

Return values
$this

setCacheDirectory()

Set the cache directory or the CacheInterface object

public setCacheDirectory(string $cache) : $this
Parameters
$cache : string
Tags
throws
ConfigurationBuilderException

If the directory does not exist or it's not readable.

Return values
$this

setConfigurationClass()

Set the full class name of the configuration object to instantiate.

public setConfigurationClass(string $configurationClass) : $this

Set the configuration class to populate with the processed parameters. If the class does not exist, a ConfigurationBuilderException is thrown. This method expects to pass an array of parameters to the class constructor.

Parameters
$configurationClass : string

The configuration full classname.

Tags
throws
ConfigurationBuilderException

If the class does not exist.

Return values
$this

setDefinition()

Set the object to process the configuration parameters.

public setDefinition(ConfigurationInterface $definition) : $this

Add an instance of Symfony\Component\Config\Definition\ConfigurationInterface to process the configuration parameters. For further information about Symfony Config and how to define a ConfigurationInterface class, please see the official Symfony documentation.

Parameters
$definition : ConfigurationInterface

The configuration definition object.

Tags
see
https://symfony.com/doc/current/components/config/definition.html
Return values
$this

setDirectories()

Set the name of the directories where to find the configuration files to load.

public setDirectories(array<string|int, mixed>|IteratorAggregate $dirs) : $this

This method receives an array of strings or SplFileInfo objects and sets the list of the directories where to find the configuration files to load. It removes all the previously added directories.

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;

$builder = new ConfigurationBuilder();
$dirs = [__DIR__ . '/app/config', getcwd()];
$builder->setDirectories($dirs);

This method can also accept an iterator, containing strings or SplFileInfo, so you can pass also an instance of a finder object, i.e. Symfony Finder:

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;
use Symfony\Component\Finder\Finder;

$builder = new ConfigurationBuilder();
$finder = new Finder();
$finder->in(getcwd())->name('config')->directories();

$builder->setDirectories($dirs);
Parameters
$dirs : array<string|int, mixed>|IteratorAggregate

Se the entire directories array.

Tags
throws
ConfigurationBuilderException

If a directory does not exist or it's not writeable.

Return values
$this

setFiles()

Set the name of the configuration files to load.

public setFiles(array<string|int, mixed>|IteratorAggregate $files) : $this

This method receives an array of strings or SplFileInfo objects and sets the list of the configuration files to load. It removes all the files previously added.

<?php declare(strict_types=1);
use Susina\ConfigBuilder\ConfigurationBuilder;

$builder = new ConfigurationBuilder();
$configFiles = ['my-project.dist.xml', 'my-project.xml'];
$builder->setFiles($configFiles);

This method can also accept an iterator, containing strings or SplFileInfo, so you can pass also an instance of a finder object, i.e. Symfony Finder:

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;
use Symfony\Component\Finder\Finder;

$builder = new ConfigurationBuilder();

$finder = new Finder();
$finder->in('app/config')->name('*.json')->files();

$builder->setFiles($finder);
Parameters
$files : array<string|int, mixed>|IteratorAggregate

The files to add.

Return values
$this

setInitMethod()

Set the method to use to initialize the configuration object.

public setInitMethod(string $initMethod) : $this

The configuration class, set via setConfigurationClass method, could be populated via its constructor or via an initialization method, expecting an array as parameter. With setInitMethod we set the method to use to populate the configuration class.

Suppose you have a configuration class, like the following:

<?php declare(strict_types=1);

namespace MyApp\MyNamespace;

class ConfigurationManager
{
     public function setParameters(array $params): void
     {
         //some operations with $params
         ...........
     }

     // some othe methods
     ................
}

The set up of your ConfigurationBuilder should be:

<?php declare(strict_types=1);

use MyApp\MyNamespace\ConfigManager;
use Susina\ConfigBuilder\ConfigurationBuilder;

$config = Configurationuilder::create()
 ->setConfigurationClass(ConfigurationManager::class)
 ->setInitMethod('setParameters');
Parameters
$initMethod : string

The name of the method.

Return values
$this

setReplaces()

Set an array of parameters to replace.

public setReplaces(array<string, mixed> $params) : $this

This values are useful for parameters replacing and they're removed before processing and validating the configuration. In this way, you can write some "standard" parameters in your configuration. I.e.:

cache:
 path: %kernel_dir%/cache/my_app.cache

Now,you can inject the kernel_dir parameter to replace it:

<?php declare(strict_types=1);

use Susina\ConfigBuilder\ConfigurationBuilder;

$config = Configurationuilder::create()
 ->setReplaces(['kernel_dir' => '/my/absolute/path])
 ->getConfigurationArray();

// $config = [
//   'cache' => [
//       'path' =>  '/my/absolute/path/my_app.cache'
//   ]
//]

Note that, after replacing, the kernel_dir parameter is removed from the configuration.

Parameters
$params : array<string, mixed>

The parameters to replace

Return values
$this

getDotArray()

Transform an array in dotted notation.

private getDotArray(array<string|int, mixed> $parameters, array<string|int, mixed> &$output[, string $keyAffix = '' ]) : void

Useful to populate a di-container.

Parameters
$parameters : array<string|int, mixed>

The array to translate in dotted notation.

$output : array<string|int, mixed>

The array to return.

$keyAffix : string = ''

loadFromCache()

Load the configuration from cache.

private loadFromCache() : array<string|int, mixed>
Tags
psalm-suppress

PossiblyInvalidArgument FileLocator::locate() returns a string if the 3rd function argument is not set to false

psalm-suppress

UnresolvableInclude

Return values
array<string|int, mixed>

The configuration

loadParameters()

Load parameters from the configuration files.

private loadParameters() : array<string|int, mixed>
Tags
psalm-suppress

NamedArgumentNotAllowed

Return values
array<string|int, mixed>

        
On this page

Search results