Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide configurable scripts with PHP composer

I want to provide a script that can be installed with PHP's package manager composer and that must be configurable.

With composer, we can easily define vendor binaries.

However, I don't see any possibility to configure them.

One could include a configuration file from within the package. However, name and location of the vendor directory are configurable, so this will not be very reliable.

For comparison: With Python's package manager pip, we can use environment variables for the configuration. We can set the environment variables while activating the virtual environment, e.g. by using tools like the virtualenvwrapper.

There must be at least experiments with similar approaches in the PHP community.

Addendum: The story behind

I have scripts that synchronize the databases and user generated files for test versions of websites.

For Django sites, I'm always using the same scripts, they rely on environment variables that I define with the virtualenvwrapper.

For Drupal sites, one can achieve a lot with drush.

But for Wordpress, I cannot find a simple and clean tool. It should

  • live outside the folders available to the public
  • be installable with composer, eventually in combination with other general-purpose-PHP-tools
  • use a simple and robust way to detect the settings.

To be honest, I'm missing the virtualenvwrapper in PHP. The virtualenvwrapper is a linux script that basically does two things:

  1. It activates a Python virtualenvironment. Translated to PHP that would mean that everything that follows uses the autoloader of a certain composer package.
  2. It executes a script when activating or deactivating the virtualenvironment. There, one has the possibility to define environment variables a do any other useful things.
like image 705
Philipp Zedler Avatar asked Jan 08 '18 21:01

Philipp Zedler


1 Answers

I think, what are you looking for is probably something like a library https://packagist.org/packages/hiqdev/composer-config-plugin

It is a quite popular library and managed also by a creator/contributor of the Yii/2 framework Samdark https://packagist.org/users/samdark/ or the creator of the asset-packagist hiqdev https://packagist.org/users/hiqdev/

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.

Usage:

"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"
    }
},
like image 112
FantomX1 Avatar answered Oct 20 '22 14:10

FantomX1