How to provide configurable scripts with PHP composer

As other users, I'm unsure what your end goal is here but what you have seemed to have outlined appears to break the point of using composer the whole point of the composer is that the code works with any application & required dependencies are obtained on install.

For What your trying to do of creating a setup for "configuration setting" are normally implemented by setting up a configuration interface that the using App has to create and provide for your class / created objects to initialize.

For example, your composer lib requires a login system now it could well be the app it's being used /installed in to already has a login system. No developer ever want two different login tables for a single app in their Database so you should create an interface that developers app can implement and provide the configuration that is needed

Code Example:

Example class to do something (your library entry class)

<?php
namespace my_namespace;

class DoSomething{
    private $config;

    public function __construct(\my_namespace\interface\Config $config)
    {
        $this->config = $config;
    }

    function doLogin()
    {
        // get $username and $password
        $state = $this->config->CheckUserLogin($username, $password);
        if(!is_bool($state)){ throw new Exception("State of CheckUserLogin from class '".get_class($this->config)."' is incorrect expected boolean but '".gettype($state)."' was provided"); }
    }

}

Example Config Interface: (your composer's library config interface)

<?php
namespace my_namespace\interface;

interface Config{

    public function CheckUserLogin($username, $password);

    //....

}

This means i could install your Library via composer and setup like so.

Example my app Config for your library: (the class the using app has to build inheriting your interface)

<?php
namespace myapp;

class DoSomethingConfig implementes \my_namespace\interface\Config{
    public function CheckUserLogin($username, $password)
    {
        // do my login system ($loginState)
        return $loginState;
    }
}

Example my initializing of your library: (the code any app would have to use)

<?php
namespace myapp;

class ConnectToSomething{
    private $lib;

    public function __construct(){
        $config = new \myapp\DoSomethingConfig();
        $this->lib = new \my_namespace\DoSomething($config);
    }

}

This way you give developer free reign to work with your code base. while they should never be wanting to edit your classes because you have done something that would cause duplicate information.

This is a simple example that highlights the point I'm trying to make don't try to run a config script to get the users to setup stuff for your library make them set up the stuff that needed in a way where they can then manage that connection point between your code and there's, this also provides security as they know your not accessing information you should not be and send it to your servers.

As they provide your code with the access it requires to work and nothing else.


I'm not sure what you're trying to achieve, but there are plenty of options if you want to use env vars in Php.

https://github.com/symfony/dotenv is one of them and well maintained.

<?php
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/path/to/.env');

Then you can use those env var the way you want for configuration purposes. E.g. for DB config:

$someSettings = [
    // Db config
    DB_CONNECTION => [
        'host' => getenv('DB_HOST'),
        'username' => getenv('DB_USERNAME'),
        'password' => getenv('DB_PASSWORD'),
        'database' => getenv('DB_DATABASE')
    ]
];

You can add a post-package-install script. With that you can use something like symfony/console to allow the installer to do some sort of configuration.


With a composer you can do something like that (PATH is your environment variable):

{
    "name": "test/test",
    "authors": [
        {
            "name": "Test",
            "email": "[email protected]"
        }
    ],
    "require": {

    },
    "scripts": {
        "post-install-cmd": [
            "@echoPath"
        ],
        "post-update-cmd": [
            "@echoPath"
        ],
        "echoPath": "echo $PATH"
    }
}

Not sure if this is what you want.