Search code examples
composer-php

How can I recreate composer.json from the content of the vendor directory?


I have a project where I am using composer.

However, I have lost my composer.json.

Is there a way to recreate composer.json from the content of the vendor directory?


Solution

  • Identifying the installed dependencies can be easily achieved by inspecting

    vendor
    └── composer
        └── installed.json
    

    However, you will be faced with the following problems:

    Direct and indirect dependencies

    installed.json lists all installed dependencies, that is, both

    • direct and
    • indirect

    dependencies.

    Direct dependencies are dependencies that are explicitly listed in the require and require-dev sections.

    Indirect dependencies are dependencies of your direct dependencies.

    That is, while you could programmatically parse installed.json and collect the dependencies listed therein, you will need to decide for yourself whether these are direct or indirect dependencies, or in other words, whether you want to explicitly require these dependencies in composer.json.

    Production and development dependencies

    installed.json lists all installed dependencies, that is, depending on whether you ran

    $ composer install
    

    or

    $composer install --no-dev
    

    before you lost your composer.json, installed.json will contain direct and indirect dependencies listed in

    • require and require-dev or
    • require

    sections, respectively.

    That is, you will need to decide for yourself whether these are dependencies which should be listed in the

    • require or
    • require-dev

    sections.

    See

    to find out more about the purpose and the differences between these sections.

    Version Constraints

    installed.json lists all installed dependencies with the exact versions installed.

    However, the composer.json you lost in all likelihood did not list dependencies with exact versions, but using one of the many possibilities for specifying version constraints.

    That is, you will need to decide for yourself whether you want to use exact versions, or relax the constraints, for example, by using

    • the ~ operator
    • the ^ operator
    • etc.

    See

    to find out more about version constraints.