Composer PHP: how to load "repositories" after "autoload"

Asked

Viewed 34 times

-4

I have a question/problem regarding php Composer. Come on, my PHP projects are now mostly using Composer, to "modularize" some parts I started using the property repositories on Composer.json.

I’m trying to use that package Simplecrudphp. I clone the repository in a folder called modules in my project and in composer.json colloquial:

    "repositories": [
        {
            "type": "path",
            "url": "./modules/SimpleCrudPhp"
        }
    ],
    "autoload": {
        "psr-4": {
            "App\\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\": "src",
        },
        "files": [
            "env.php"
        ]
    },
    "require": {
        "simplephp/simple-crud": "dev-master"
    }

The problem is that the Simplecrudphp package needs to run the file Config.php that takes the environment variables loaded in the autoload in the file env.php, but how Simplecrudphp performs before the env.php it does not take the environment variables and ends up not working.

Obs.: When I put a require __DIR__ . "../../../../env.php"; within the Config.php it works, but I don’t want a solution that has to keep modifying something within the package.

I wonder if you have how to modify the Composer.json of the main project for it to carry this repositories (Simplecrudphp) after the autoload/autoload-dev? Or some approach you can use to make the package work without me having to modify its content every time you create a new project?

From now on, thank you.

  • See if this helps https://stackoverflow.com/questions/36745691/cant-get-composer-path-repository-to-work

  • I took a look, but dev-master is not the problem, the package works, but by loading after env.php, it does not take the value of the variables to define it()

1 answer

0

From what I believe, Composer will always prioritize repositories and then move on to autoload. I never looked for anything in the documentation on the priority of execution.

In this case, your problem is that when adding the Simplecrudphp package, it depends on environment variables that have not yet been defined. I believe that the package itself should have the file . env and not be something external.

However, I believe that perhaps some of these solutions should help:

  • Add inside the repositories, a path to the file . env, so when Simplecrudphp runs, the variables have been defined. (I don’t know if it works)

  • Add in your code, before importing all autoload issues, the file request. env, so before running anything, your program has already loaded the environment variables.

I don’t know if that helps you, but I hope so.

  • thanks for the answer, in fact adding env.php to Simplecrudphp would solve it, but I (or someone else) would have to change the location of it within the package for each project. Adding env before autoload works, but when going up to production (which manages environment variables) without env (which I leave in gitignore) causes the import to generate error, since it imports a file that does not exist there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.