Search code examples
phpgitcomposer-phpphpstorm

What is the best practice to develop Composer's library and main project simultaneously?


I am trying hard to understand what is the best practice to use Git with main project development and library development. I will try to explain: I am developing a project and a library simultaneously. Both of them are on the GitHub. And here is my current workflow:

  1. Make a change in the library
  2. Commit and push it to the GitHub, make a release and update on Packagist
  3. Run composer update in the main project, so that the new version of the library will be downloaded
  4. Make a test with the new library version

That is too complicated and not right. What is a workout for this problem? I have an option:

  • Define a library as a local repository and place this definition in the main project's composer.json file. But this is strange because then if I upload composer.json to the GitHub, it will contain local repo definition, which is not right.

Maybe there is some smart and cool method to get rid of this problem, but I don't know about it. Please explain how to do this. Thanks in advance.

P.S. I am using PhpStorm, maybe it has an option that may help us to solve the problem.


Solution

  • If you often update the library that your main project uses you should consider hosting it as a local repository.

    You can add a repository using composer CLI (see docs) but it's much more simple to just update the composer.json file manually.

    Open your composer.json file and find the repositories attribute (if it's not there create it in the root scope).

    It should be an array of objects and every object should have a type (vcs for Version Control System) and an url.

    Also add your library to require. You could use git version tags if you desire, but pointing to dev-master is a very common practice.

    {
        "name": "mainproject",
        "description": "Main project development",
        "repositories": [{
            "type": "vcs",
            "url": "ssh://[email protected]/library.git"
        }],
        "require": {
            "library/library": "dev-master"
        }
    }
    

    When you have updated your library (and pushed the changes to the repository) you can simply run composer update library/library from your main development project to get all changes from your library.