Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active August 15, 2019 06:28
Show Gist options
  • Save dotherightthing/7f47913b2e5bac0baeff9d7b41744ce8 to your computer and use it in GitHub Desktop.
Save dotherightthing/7f47913b2e5bac0baeff9d7b41744ce8 to your computer and use it in GitHub Desktop.
[Composer - Loading nested dependencies] How to load a development dependency, used by a dependency of your primary project. require-dev vs require #composer #php

Composer - Loading nested dependencies

Problem

My wpdtrt-plugin repository used my wpdtrt-helpers plugin during development.

I documented the dependency by adding wpdtrt-helpers to the Composer file:

{
  "name": "dotherightthing/wpdtrt-plugin",
  "description": "Base classes for a plugin, plugin shortcode, and plugin widget.",
  "type": "library",
  "authors": [
    {
      "name": "Dan Smith",
      "email": "[email protected]"
    }
  ],
  "repositories": [
    {
      "type": "vcs",
      "url": "[email protected]:dotherightthing/wpdtrt-helpers.git"
    }
  ],
  "require-dev": {
    "phpunit/phpunit": "^5.7",
    "phpdocumentor/phpdocumentor": "2.*",
    "dotherightthing/wpdtrt-helpers": "dev-master"
  },
  "require": {
    "php": ">=5.6.30",
    "gamajo/template-loader": "^1.3.0"
  },
  "autoload": {
    "psr-4": {
      "DoTheRightThing\\WPPlugin\\": "src"
    }
  }
}

I required wpdtrt-plugin in wpdtrt-blocks' Composer file:

{
  "name": "dotherightthingnz/wpdtrt-blocks",
  "description": "Demo plugin using wpdtrt-plugin classes",
  "type": "library",
  "minimum-stability": "dev",
  "authors": [
    {
      "name": "Dan Smith",
      "email": "[email protected]"
    }
  ],
  "repositories": [
    {
      "type": "vcs",
      "url": "[email protected]:dotherightthing/wpdtrt-plugin.git"
    }
  ],
  "require-dev": {
    "phpunit/phpunit": "^5.7",
    "phpdocumentor/phpdocumentor": "2.*"
  },
  "require": {
    "php": ">=5.6.30",
    "dotherightthing/wpdtrt-plugin": "dev-master"
  }
}

Running composer update loaded wpdtrt-plugin, but not wpdtrt-helpers.

Solution

As stated in Composer VCS repository not loading dependancies, this is because:

composer install will install dev dependencies of the primary project, and create autoloading for dev - it will never install dev dependencies of any of the packages added via require or require-dev, and not add their autoload-dev.

To resolve this, I copied wpdtrt-helpers into the require-dev object of wpdtrt-blocks' Composer file:

{
  "name": "dotherightthingnz/wpdtrt-blocks",
  "description": "Demo plugin using wpdtrt-plugin classes",
  "type": "library",
  "minimum-stability": "dev",
  "authors": [
    {
      "name": "Dan Smith",
      "email": "[email protected]"
    }
  ],
  "repositories": [
    {
      "type": "vcs",
      "url": "[email protected]:dotherightthing/wpdtrt-helpers.git"
    },
    {
      "type": "vcs",
      "url": "[email protected]:dotherightthing/wpdtrt-plugin.git"
    }
  ],
  "require-dev": {
    "phpunit/phpunit": "^5.7",
    "phpdocumentor/phpdocumentor": "2.*",
    "dotherightthing/wpdtrt-helpers": "dev-master"
  },
  "require": {
    "php": ">=5.6.30",
    "dotherightthing/wpdtrt-plugin": "dev-master"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment