Giter Site home page Giter Site logo

yiiliveext / composer-config-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yiisoft/composer-config-plugin

0.0 1.0 0.0 415 KB

Composer plugin for config assembling

Home Page: https://www.yiiframework.com/

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

composer-config-plugin's Introduction

Composer Config Plugin


Composer plugin for config assembling.

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage

This Composer plugin provides assembling of configurations distributed with composer packages. It allows putting configuration needed to use a package right inside of the package thus implementing a plugin system. The package becomes a plugin holding both the code and its configuration.

How it works?

  • Scans installed packages for config-plugin extra option in their composer.json
  • Loads .env files to set $_ENV variables
  • Requires constants files to set constants
  • Requires params files
  • Requires config files
  • Options collected during earlier steps could and should be used in later steps, e.g. $_ENV should be used for constants and parameters, which in turn should be used for configs
  • File processing order is crucial to achieve expected behavior: options in root package have priority over options from included packages. It is described below in File processing order section.
  • Collected configs are written as PHP files in vendor/yiisoft/composer-config-plugin-output directory along with information needed to rebuild configs on demand
  • Then assembled configs are ready to be loaded into application using require

Read more about the general idea behind this plugin in English or Russian.

Installation

composer require "yiisoft/composer-config-plugin"

Out of the box this plugin supports configs in PHP and JSON formats.

To enable additional formats require:

Usage

List your config files in composer.json like the following:

"extra": {
    "config-plugin-output-dir": "path/relative-to-composer-json",
    "config-plugin": {
        "params": [
            "config/params.php",
            "?config/params-local.php"
        ],
        "common": "config/common.php",
        "web": [
            "$common",
            "config/web.php"
        ],
        "other": "config/other.php"
    }
},

? marks optional files. Absence of files not marked with it will cause exception.

$common is inclusion - common config will be merged into web.

Define your configs like the following:

return [
    'components' => [
        'db' => [
            'class' => \my\Db::class,
            'name' => $params['db.name'],
            'password' => $params['db.password'],
        ],
    ],
];

To load assembled configs in your application use require:

$config = require Yiisoft\Composer\Config\Builder::path('web');

Refreshing config

Plugin uses composer POST_AUTOLOAD_DUMP event i.e. composer runs this plugin on install, update and dump-autoload commands. As the result configs are ready to be used right after package installation or update.

When you make changes to any of configs you may want to reassemble configs manually. In order to do it run:

composer dump-autoload

Above can be shortened to composer du.

If you need to force config rebuildign from your application, you can do it like the following:

// Don't do it in production, assembling takes it's time
if (ENVIRONMENT === 'dev') {
    Yiisoft\Composer\Config\Builder::rebuild();
}

File processing order

Config files are processed in proper order to achieve naturally expected behavior:

  • Options in outer packages override options from inner packages
  • Plugin respects the order your configs are listed in composer.json with
  • Different types of options are processed in the following order:
    • Environment variables from envs
    • Constants from constants
    • Parameters from params
    • Configs are processed last of all

Debugging

There are several ways to debug config building internals.

  • Plugin can show detected package dependencies hierarchy by running:
composer dump-autoload --verbose

Above can be shortened to composer du -v.

  • You can see the list of configs and files that plugin has detected and uses to build configs. It is located in vendor/yiisoft/composer-config-plugin-output/__files.php.

  • You can see the assembled configs in the output directory which is vendor/yiisoft/composer-config-plugin-output by default and can be configured with config-plugin-output-dir extra option in composer.json.

Known issues

This plugin treats configs as simple PHP arrays. No specific structure or semantics are expected and handled. It is simple and straightforward, but I'm in doubt... What about errors and typos? I think about adding config validation rules provided together with plugins. Will it solve all the problems?

Anonymous functions must be used in multiline form only:

return [
    'works' => function () {
        return 'value';
    },
    // this will not work
    'noway' => function () { return 'value'; },
];

License

This project is released under the terms of the BSD-3-Clause license. Read more here.

Copyright © 2016-2020, HiQDev (http://hiqdev.com/) Copyright © 2020, Yiisoft (https://www.yiiframework.com/)

composer-config-plugin's People

Contributors

edgardmessias avatar hiqsol avatar jomonkj avatar jseliga avatar loveorigami avatar machour avatar marclaporte avatar noname007 avatar roxblnfk avatar samdark avatar silverfire avatar thenotsoft avatar xepozz avatar

Watchers

 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.