-1

I'm working with two libraries:

This is what I'm executing:

<?php
require_once "/var/local/entrop/vendor/autoload.php";
include_once '/var/local/entrop/inc/Functions.php';
include_once '/var/local/entrop/model/Contract.php';

use Spatie\Async\Pool;

try {
    $aConn = Functions::getConnection();
    $contracts = Contract::getContracts();

    $pool = Pool::create();
    
    foreach ($contracts as $contract){
        $pool->add(function () use ($aConn, $contract) {
            include_once '/var/local/entrop/config/DataConfig.php';
            $next_contract = Contract::getNextContract($aConn);
        })
        ->then(function ($output) {
        })
        ->catch(function ($exception) {
            Functions::write_log($exception);
        })
        ->timeout(function () {
            Functions::write_log("timeout");
        });
    }

    await($pool);

    $aConn->close();
}
catch (Exception $e){
    Functions::write_log($e);
}

This is the DataConfig definition:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL); 

require_once "/var/local/entrop/vendor/autoload.php";

$dotenv = Dotenv\Dotenv::createImmutable("/var/local/entrop/");
$dotenv->load();

define('DB_HOST', $_ENV['DB_HOST']);
define('DB_USER', $_ENV['DB_USER']);
define('DB_PASS', $_ENV['DB_PASS']);
define('DB_DBMS', $_ENV['DB_DBMS']);

class DataConfig {
    static $db_host = DB_HOST;
    static $db_user = DB_USER;
    static $db_pass = DB_PASS;
    static $db_dbms = DB_DBMS;
}

And this is my composer.json file:

{
    "require": {
        "vlucas/phpdotenv": "^5.2",
        "spatie/async": "^1.5"
    },
    "autoload": {
        "classmap": [
            "/var/local/entrop/model/Contract.php"
        ]
    }
}

Now, the error I'm getting is:

[2022-08-04 10:38:38]local.INFO: Spatie\Async\Output\ParallelError: PHP Notice: Undefined index: DB_HOST in /var/local/entropia/config/DataConfigEntropia.php on line 12 PHP Notice: Undefined index: DB_USER in /var/local/entropia/config/DataConfigEntropia.php on line 13 PHP Notice: Undefined index: DB_PASS in /var/local/entropia/config/DataConfigEntropia.php on line 14 PHP Notice: Undefined index: DB_DBMS in /var/local/entropia/config/DataConfigEntropia.php on line 15

What I tried so far is autoload my Dataconfig file, include it inside the callback function. It's not working. What could I do?

Dexter Naru
  • 213
  • 2
  • 5
  • 17

1 Answers1

0

phpdotenv is not good at error reporting (and to my taste is also not properly relying on standards nor best practices which can lead to many side-effects including the notices you get - it's just I'm pretty sure theirs authors consider this a feature not bugs, therefore I have to admit I'm not judging they about it, at the end of the day the user needs to do the work and know what they do).

For your very specific problems, if you use a library, learn about how to configure it. Even my previous annotation may signal to you it might not be fitting, it infact - and that is where phpdotenv shines - is doing a lot of the gruntwork and you can control most parts of it --- just not the file-format.

your issue is not the file-format. so configure your application properly to consume parameters from any environment and handle the error cases (e.g. throw on unexpected values for a start). this should also reveal where you made an error using this or that library for a start and then address the issue in your code.

hakre
  • 193,403
  • 52
  • 435
  • 836