Brian F Love
Learn from a Google Developer Expert focused on Angular, Web Technologies, and Node.js from Portland, OR.
Ad·ultimatecourses.com
Learn Angular the right way with Ultimate Courses

Add Git Composer Dependency

It's really easy to add a dependency to your PHP project using composer when the package is available in the packagist library.

However, sometimes you may need to include an open-source project or library that is not in the packagist library, and the project does not explicitly provide a composer.json. For example, I wanted to try out an Open Graph meta tag parser project on GitHub. However, Scott doesn't have a composer.json defined in his project.

First, I added a require for the project.

"require" : {
  "scottmac/opengraph" : "*"
}

Then, I added a new repository source for the project files on GitHub. Since there are no specific releases or versions, I just put in "0.0.1" as the version. I also copied the zip file download path from GitHub for the distribution URL.

"repositories" : [
  ...,
  {
    "type": "package",
    "package": {
      "name": "scottmac/opengraph",
      "version": "0.0.1",
      "source": {
        "type" : "git",
        "url" : "git://github.com/scottmac/opengraph.git",
        "reference" : "0.0.1"
      },
      "dist": {
        "url": "https://github.com/scottmac/opengraph/archive/master.zip",
        "type": "zip"
      }
    }
  }
]

Lastly, when I run composer update, the latest release of the project is downloaded and placed in my Vendors folder. Now, I can go ahead and test out the library in my project.

$ composer update

Complete Example

Here is the complete example code for including a library from GitHub that does not have a composer.json file.

{
  "name": "com.brianflove",
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "pear-pear.cakephp.org/CakePHP",
        "version": "2.4.1",
        "source": {
          "type" : "git",
          "url" : "git://github.com/cakephp/cakephp.git",
          "reference" : "2.4.1"
        },
        "repositories": [
          {
            "type": "pear",
            "url": "http://pear.cakephp.org"
          }
        ],
        "bin" : ["lib/Cake/Console/cake"]
      }
    },
    {
      "type": "package",
      "package": {
        "name": "scottmac/opengraph",
        "version": "0.0.1",
        "source": {
          "type" : "git",
          "url" : "git://github.com/scottmac/opengraph.git",
          "reference" : "0.0.1"
        },
        "dist": {
                  "url": "https://github.com/scottmac/opengraph/archive/master.zip",
                  "type": "zip"
              }
      }
    }
  ],

  "config": {
        "vendor-dir": "Vendor/"
    },

  "extra": {
    "installer-paths": {
      "www/Plugin/DebugKit": ["cakephp/debug_kit"],
      "www/Plugin/BoostCake": ["slywalker/boost_cake"]
    }
  },

  "require" :
  {
    "php": ">=5.3",
    "pear-pear.cakephp.org/CakePHP": ">=2.4.0",
    "cakephp/debug_kit": "2.2.*",
    "scottmac/opengraph" : "*"
  }
}

Brian F Love

Hi, I'm Brian. I am interested in TypeScript, Angular and Node.js. I'm married to my best friend Bonnie, I live in Portland and I ski (a lot).