Class luya\Config

Inheritanceluya\Config
Available since version1.0.21
Source Code https://github.com/luyadev/luya/blob/master/core/Config.php

Configuration array Helper.

The {{luya\Config}} allows you to create the configuration for different hosts and difference between web and console config.

$config = new Config('myapp', dirname(__DIR__), [
    'siteTitle' => 'My LUYA Project',
    'defaultRoute' => 'cms',
    // other application level configurations
]);

// define global components which works either for console or web runtime

$config->component('mail', [
    'host' => 'xyz',
    'from' => 'from@luya.io',
]);

$config->component('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=prod_db',
    'username' => 'foo',
    'password' => 'bar',
]);

// define components which are only for web or console runtime:

$config->webComponent('request', [
    'cookieValidationKey' => 'xyz',
]);

// which is equals to, but the above is better to read and structure in the config file

$config->component('request', [
    'cookieValidationKey' => 'xyz',
])->webRuntime();

// adding modules

$config->module('admin', [
    'class' => 'luya\admin\Module',
    'secureLogin' => true,
]);

$config->module('cms', 'luya\cms\frontend\Module'); // which is equals to $config->module('cms', ['class' => 'luya\cms\frontend\Module']);

// export and generate the config for a given enviroment or environment independent.

return $config->toArray(); // returns the config not taking care of enviroment variables like prod, env

return $config->toArray([Config::ENV_PROD]);

Runtime

Each method returns an {{luya\ConfigDefinition}} object and can therefore be configured for different runtimes (console/web). The given example will add a console command only for console applications:

$config->application([
    'controllerMap' => [
        's3' => 'luya\aws\commands\S3Command',
    ]
])->consoleRuntime();

Envs

Switching between envs can be usefull if certain configurations should only apply on a certain environment. Therefore you can add env() behind componenets, applications and modules.

$config->component('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=local_db',
    'username' => 'foo',
    'password' => 'bar',
])->env(Config::ENV_LOCAL);

$config->component('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=dev_db',
    'username' => 'foo',
    'password' => 'bar',
])->env(Config::ENV_DEV);

$config->component('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=prod_db',
    'username' => 'foo',
    'password' => 'bar',
])->env(Config::ENV_PROD);

return $config->toArray(Config::ENV_PROD); // would only return the prod env db component

When mergin varaibles, the later will always override the former. If arrays are involved the values will be added, not replaced! Example: 'foo' => 'bar', 'values' => [1] and 'foo' => 'baz', 'values' => [2] will be merged to: 'foo' => 'baz', 'values' => [1,2].

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Constructor luya\Config
application() Register application level config luya\Config
bootstrap() Register one or more bootstrap entries into the bootstrap section. luya\Config
callback() Run a callable functions for the defined env when toArray() is called. luya\Config
component() Register a component luya\Config
consoleComponent() Register a console runtime component. luya\Config
env() Assign the env to each component, module or application that defined inside the callback. luya\Config
isCliRuntime() Whether runtime is cli or not luya\Config
module() Register a module. luya\Config
setCliRuntime() Setter method for runtime. luya\Config
toArray() Export the given configuration as array for certain envs. luya\Config
webComponent() Register a web runtime component. luya\Config

Constants

Hide inherited constants

Constant Value Description Defined By
ENV_ALL 'all' luya\Config
ENV_CI 'ci' luya\Config
ENV_DEV 'dev' luya\Config
ENV_LOCAL 'local' luya\Config
ENV_PREP 'prep' luya\Config
ENV_PROD 'prod' luya\Config
RUNTIME_ALL 0 luya\Config
RUNTIME_CONSOLE 1 luya\Config
RUNTIME_WEB 2 luya\Config

Method Details

Hide inherited methods

__construct() public method

Constructor

public void __construct ( $id, $basePath, array $applicationConfig = [] )
$id string
$basePath string
$applicationConfig array

                public function __construct($id, $basePath, array $applicationConfig = [])
{
    $applicationConfig['id'] = $id;
    $applicationConfig['basePath'] = $basePath;
    $this->application($applicationConfig);
}

            
application() public method

Register application level config

public luya\ConfigDefinition application ( array $config )
$config array

The array to configure

                public function application(array $config)
{
    return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_APPLICATIONS, 'application', $config));
}

            
bootstrap() public method

Register one or more bootstrap entries into the bootstrap section.

public luya\ConfigDefinition bootstrap ( array $config )
$config array

An array with bootstrap entries, its common to use the module name

                public function bootstrap(array $config)
{
    return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_BOOTSTRAPS, 'bootstrap', $config));
}

            
callback() public method (available since version 1.0.23)

Run a callable functions for the defined env when toArray() is called.

public luya\ConfigDefinition callback ( callable $fn )
$fn callable

The function to run, the first argument of the closure is the {{luya\Config}} object.

                public function callback(callable $fn)
{
    return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_CALLABLE, false, $fn));
}

            
component() public method

Register a component

public luya\ConfigDefinition component ( $id, $config, $runtime self::RUNTIME_ALL )
$id string

The id of the component

$config string|array

The configuration for the given module. If a string is given this will be taken as class property.

$runtime string

The runtime for the component: all, web or console

                public function component($id, $config, $runtime = self::RUNTIME_ALL)
{
    return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_COMPONENTS, $id, $config))->runtime($runtime);
}

            
consoleComponent() public method

Register a console runtime component.

public luya\ConfigDefinition consoleComponent ( $id, $config )
$id string

The id of the component

$config string|array

The configuration for the given module. If a string is given this will be taken as class property.

                public function consoleComponent($id, $config)
{
    return $this->component($id, $config, self::RUNTIME_CONSOLE);
}

            
env() public method

Assign the env to each component, module or application that defined inside the callback.

Callback function has one parameter with the current {{luya\Config}} object.

An example using env to wrap multiple configuration lines into a single environment:

$config->env(Config::ENV_LOCAL, function($config) {

    $config->callback(function() {
        define('YII_DEBUG', true);
        define('YII_ENV', 'local');
    });

    $config->component('db', [
        'dsn' => 'mysql:host=luya_db;dbname=luya_kickstarter',
        'username' => 'luya',
        'password' => 'luya',
        'enableSchemaCache' => false,
    ]);

    $config->module('debug', [
        'class' => 'yii\debug\Module',
        'allowedIPs' => ['*'],
    ]);

    $config->bootstrap(['debug']);
});
public $this env ( $env, callable $callback )
$env string

The environment to assigne inside the callback.

$callback callable

Function(\luya\Config $config)

                public function env($env, callable $callback)
{
    $this->_env = $env;
    
    try {
        call_user_func($callback, $this);
    } finally {
        $this->_env = null;
    }
    
    return $this;
}

            
isCliRuntime() public method

Whether runtime is cli or not

public boolean isCliRuntime ( )

                public function isCliRuntime()
{
    if ($this->_isCliRuntime === null) {
        $this->_isCliRuntime = strtolower(php_sapi_name()) === 'cli';
    }
    return $this->_isCliRuntime;
}

            
module() public method

Register a module.

public luya\ConfigDefinition module ( $id, $config )
$id string

The module identifier.

$config string|array

The configuration for the given module. If a string is given this will be taken as class property.

                public function module($id, $config)
{
    return $this->addDefinition(new ConfigDefinition(ConfigDefinition::GROUP_MODULES, $id, $config));
}

            
setCliRuntime() public method

Setter method for runtime.

This method is mainly used for unit testing.

public void setCliRuntime ( $value )
$value boolean

                public function setCliRuntime($value)
{
    $this->_isCliRuntime = $value;
}

            
toArray() public method

Export the given configuration as array for certain envs.

public array toArray ( $envs = [] )
$envs array|string

A list of environments to export. if nothing is given all enviroments will be returned. A string will be threated as array with 1 entry.

return array

The configuration array

                public function toArray($envs = [])
{
    $config = [];
    $envs = (array) $envs;
    $envs = array_merge($envs, [self::ENV_ALL]);
    foreach ($this->_definitions as $definition) { /** @var ConfigDefinition $definition */
        // validate if current export env is in the list of envs
        if (!$definition->validateEnvs($envs)) {
            continue;
        }
        // validate runtime circumstances
        if ($definition->validateRuntime(self::RUNTIME_ALL)) {
            $this->appendConfig($config, $definition);
        } elseif ($this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_CONSOLE)) {
            $this->appendConfig($config, $definition);
        } elseif (!$this->isCliRuntime() && $definition->validateRuntime(self::RUNTIME_WEB)) {
            $this->appendConfig($config, $definition);
        }
    }
    return $config;
}

            
webComponent() public method

Register a web runtime component.

public luya\ConfigDefinition webComponent ( $id, $config )
$id string

The id of the component

$config string|array

The configuration for the given module. If a string is given this will be taken as class property.

                public function webComponent($id, $config)
{
    return $this->component($id, $config, self::RUNTIME_WEB);
}