Search code examples
phpsvncomposer-phppackagist

Adding a package with Composer (through SVN)


I created a SVN repository for my personal PHP library, and added a composer.json file at the root level:

{
        "name": "myPersonalLibrary/lib",
        "type": "library",
        "description": "Light MVC framework for PHP 5.4",
        "keywords": ["database","mvc"],
        "homepage": "http://mysite.com",
        "license": "MIT",
        "require": {
                "php": ">=5.3.0",
                "mustache/mustache": "dev-master"
        },
        "autoload": {
                "psr-0": {
                        "bbn": "src"
                }
        }
}

Then I created a project with the following composer.json:

{
    "require": {
        "monolog/monolog": "1.0.*",
        "zerkalica/php-code-sniffer": "dev-master",
        "mustache/mustache": "dev-master",
        "myPersonalLibrary/lib": "*"
    },
    "repositories": [
            {
                    "type": "svn",
                    "url": "https://mysite.com/svn/myPersonalLibrary",
                    "branches-path": false,
                    "tags-path": false,
                    "trunk-path": "src"
            }
    ]
}

And when I try to update my project I get: No valid composer.json was found in any branch or tag of https...

I think the problem is coming from my file's structure but I couldn't manage to find any documentation about this:

/my_repo
  /src
    /lib
      /api
      /db
      /file
      /html
      ....
      /mvc.php
      /obj.php
  /composer.json

I tried to post my URL on packagist.org and got No valid/supported repository was found at the given URL


Solution

  • If you use the officially recommended repository layout with a "project root" (which contains exactly three subdirectories: /trunk, /branches, and /tags) then this should work for you:

    For your PHP library create composer.json in project root in the trunk (and commit it). For example:

    {
        "name": "myProject/myLibrary",
        "description": "My Personal Library",
        "license": "proprietary",
        "require": {
            "php": ">=5.3"
        },
        "autoload": {
            "classmap": ["src/"]
        }
    }
    

    Lets say your library repository is available at http://svn.example.com/path/to/myLibrary. The layout then would be:

    /path/to/myLibrary
      /trunk
        /composer.json
        /src
          ...
      /branches
      /tags
    

    Then in you project, where you want to use your library, create composer.json with the following contents:

    {
        "repositories": [
            {
                "type": "vcs",
                "url": "http://svn.example.com/path/to/myLibrary"
            }
        ],
        "require": {
            "nette/nette": "~2.2",
            "myProject/myLibrary": "@dev"
        }
    }
    

    The key is to use @dev as the required version for your library if you only have composer.json in trunk yet. Once you create a tag from trunk, you can start using version numbers. For example if you svn copy ^/trunk ^/tags/1.0.0, then you can use "myProject/myLibrary": "~1.0" as your version number.