« Back to Index

PHP: setup Composer with local code

View original Gist on GitHub

Tags: #php #composer #local

PHP - setup Composer with local code.md

Install Composer

Debugging

For debugging use https://github.com/tacnoman/dephpugger (e.g. composer require tacnoman/dephpugger).

You’ll need xdebug too, which has to be installed via pecl and that is only available on PHP when installed via macOS Homebrew.

Also, with latest version of xdebug you’ll find some properties have been updated and so until there is a newer version of dephpugger that supports xdebug 3 you’ll have to manually update vendor/tacnoman/dephpugger/src/Dephpug/Console/CliCommand.php with renamed fields (see https://xdebug.org/docs/errors#CFG-C-CHANGED and the upgrade guide for details).

The summary of those changes being:

- $command = "{$configVar} {$phpPath} -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port={$debuggerPort} -dxdebug.remote_host=127.0.0.1 {$phpFile}";
+ $command = "{$configVar} {$phpPath} -dxdebug.mode=debug -dxdebug.start_with_request=trigger -dxdebug.client_port={$debuggerPort} -dxdebug.client_host=127.0.0.1 {$phpFile}";

Now you can open three shells:

  1. In your code add xdebug_break(); where necessary.
  2. Start the debugger server: $ php vendor/bin/dephpugger debugger.
  3. Run your code using the debugger’s cli client: $ php vendor/bin/dephpugger cli main.php.

Set local code as a project dependency

$ cd ~/path/to/local/package
$ git init
$ echo -e "vendor\ncomposer.lock" > .gitignore
$ git add ./
$ git commit -m "Initial Commit"

Ensure the local package’s composer.json file has a name key defined with an appropriate value, as you’ll need to reference that shortly.

Now go back to your project directory and update the composer.json to reference the local package code as a dependency:

{
    …
    "require": {
        "foo/bar": "dev-main"
    },
    "repositories": [{
        "type": "vcs",
        "url": "/path/to/local/package"
    }]
}

NOTE: The dev- prefix inside the require block denotes a development branch, where typically a semver value (like 1.0.0) would otherwise be required.