How to use Composer without Packagist like a Newbie

Say for instance you want to use a bundle from someone else, however that someone has not taken the time to update her repository with a composer.json neither has she taken the time to register her repository in packagist.org site. You don’t have time to wait for a PR to be accepted and then waiting for the developer to register whenever. So you take the thing in your own hands and register it yourself on packagist within your own vendor namespace? Wrong!

Here is how it is done, this is indeed a real example:

{
    "name": "symfony/framework-standard-edition",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.2",
        "symfony/symfony": "2.1.*",
        "doctrine/orm": "2.2.0",
        "doctrine/doctrine-bundle": "dev-master",
        "twig/extensions": "dev-master",
        "symfony/assetic-bundle": "dev-master",
        "symfony/swiftmailer-bundle": "dev-master",
        "symfony/monolog-bundle": "dev-master",
        "sensio/distribution-bundle": "dev-master",
        "sensio/framework-extra-bundle": "dev-master",
        "sensio/generator-bundle": "dev-master",
        "jms/security-extra-bundle": "1.1.0",
        "jms/di-extra-bundle": "1.0.1",
        "cordoval/quizbundle": "1.0"
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "cordoval/quizbundle",
                "version": "1.0",
                "dist": {
                    "url": "https://github.com/cordoval/SmirikQuizBundle/zipball/master",
                    "type": "zip"
                },
                "autoload": {
                    "psr-0": { "Smirik\\QuizBundle": "" }
                },
                "target-dir": "Smirik/QuizBundle"
            }
        }
    ],
    "scripts": {
        "post-install-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets"
        ],
        "post-update-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web"
    }
}

Now make sure you focus on the “cordoval/quizbundle” definition of the bundle. This is directing composer to install the dep from my fork of the bundle on github and not the original main repository one.

Note: When downloading from github or other url, if we instruct composer to do:

composer install --prefer-source

What it will do is to always download the source version of the releases so the source will have .git folder in it. If we don’t tell it and we instruct the version for the package to be different than dev-master then composer will always download the release distribution version which do not contain .git folder.

8 thoughts on “How to use Composer without Packagist like a Newbie

  1. Great!

    … but is there a way download straight from git (not zipped package). Looked in composer documentation, but that doesn’t seem to work.

    "repositories": [
            {
                "type": "git",
                "url": "http://myserver.com/MasteringBundle.git",
                "branch": "master"
            }
        ],
    • Figured that out.

      If you don’t have composer.json in your bundle:

      "repositories": [
              {
                  "type": "package",
                  "package": {
                      "name": "VendorName/mastering-bundle",
                      "version": "master",
                      "autoload": { "psr-0": { "VendorName\MasteringBundle": "" } },
                      "target-dir": "VendorName/MasteringBundle",
                      "source": {
                          "url": "http://myserver.com/MasteringBundle.git",
                          "type": "git",
                          "reference": "master"
                      }
                  }
              }
          ],

      and add to “require” section:

      "VendorName/mastering-bundle": "*"

      Hope it helps!
      … and thanks for nice blog 🙂

  2. This was much more helpful than composer’s documentation because it showed the actual repositories lines in the context of everything else in the json. Very helpful. Thank you.

  3. To add a GITHUB repo, very simple :

    repositories”: [
    {
    “type”: “vcs”,
    “url”: “https://github.com/igorw/monolog”
    }
    ],

  4. Thx you save me, i was declared my autoload and targetdir in my composer.json vendor, not in repository configuration in my composer.json project…

Leave a Reply

Your email address will not be published. Required fields are marked *