How do I create my own package/project for "Composer" on github?

Asked

Viewed 3,718 times

9

I never distribute a library using a repository with commiserate, I have no experience with this, what I tried to do was to create a folder structure like this:

./
 |-- composer.json
 |-- src/
     |-- Test/
           |-- Foo.php

In the archive composer.json from the repository I added the following content:

{
    "name": "[vendor]/[nome do repositorio]",
    "autoload": {
        "psr-0": {
            "Test": "src/"
        }
    }
}

I edit in my project (in my machine the composer.json):

{
    "name": "foo/bar",
    "description": "Framwork foo",
    "type": "project",
    "authors": [
        {
            "name": "Foo bar",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php": ">=5.3.0",
        "[vendor]/[nome do repositorio]": "*"
    }
 }

After this I run the following command (linux):

$ cd /home/project1
$ php composer.phar update

Windows:

cd c:\wamp\www\project1
composer update

But it returns the following failure:

Problem 1 - The requested package brcontainer/Composer-test could not be found in any version, there may be a typo in the package name.

I tried to change the minimum-stability for dev, but there was the same problem, I missed adding something in the repository?

2 answers

6


This error occurs due to 3 possible factors:

  1. Typo
  2. The package is not available
  3. You’re trying to use a package dev

The first error is easy to understand, the problem is the second, it is not very clear what is "package" or where we should make it available, at first I thought that Composer consulted github and/or bitbucket, but actually that’s not how it works, you can use any repository, beyond the github, but you will have to provide the .git as a package and this.

On the website of https://getcomposer.org if you notice there is a link called Browse Packages this is where the magic happens:

Composer site links

When accessing we are taken to https://packagist.org, then just sign in with your account by clicking use github (if it’s another repository like bitbucket you’ll have to create an account manually, click on Create One Now for this):

Conta do Github

After this he will ask for the necessary permissions just accept, read and then accept.

Then in the top menu click on Submit, in the "Repository URL (Git/Svn/Hg) field", you must paste the .git (supports other formats) of githube, to get the . git go to your repository and at the top will have the link, for example:

url do git

After this just paste into the page field Submit and then click Check:

Enviar git

If the error occurs:

The CSRF token is invalid. Please Try to resubmit the form.

It is because the page has expired, just click on the link Submit at the top and try again. Other repositories (of other people) with similar names may appear, it is the opportunity to think of a more creative name for yours, but it is totally optional, just click the Submit button to confirm.

It must look something like this:

Package submited

You need to create a Release on github (if you’re using github), so go to Releases on your repository:

inserir a descrição da imagem aqui

Then click the button Create a new release, fill the form and click on the button Publish release

However some repositories do not have Releases (this is the third problem I mentioned in the beginning) even though I am in Packagist, this because the project is still in development, then it is necessary to modify its composer.json of your project and add this:

"minimum-stability": "dev",
"prefer-stable": true,

Should stay like this:

{
    "name": "foo/bar",
    "description": "Framwork foo",
    "type": "project",
    "authors": [
        {
            "name": "Foo bar",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php": ">=5.3.0",
        "[vendor]/[nome do repositorio]": "*"
    },
    "minimum-stability": "dev",
    "prefer-stable": true
 }

After this just test your repository on your project that is on your machine:

cd c:\wamp\projeto1
composer update

The result has to be something like this:

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing brcontainer/composer-test-2 (v1.0.0)
    Downloading: 100%

Installing by command line to your project

After preparing everything still note that it is possible to download the package without adding to your composer.json of the project, thus:

composer require [vendor]/[nome do repositorio]

Or:

php composer.phar require [vendor]/[nome do repositorio]

However if the project does not have a Release it is necessary to execute the command like this:

composer require [vendor]/[nome do repositorio]:dev-master

Or:

php composer.phar require [vendor]/[nome do repositorio]:dev-master

2

When you put your package as a project dependency, you need to specify if you want any Constraint version. If you have not yet versioned this dependency, you need to configure to use a branch of it as dependency.

Using a version Constraint:

"require": {
    "php": ">=5.3.0",
    "[vendor]/[nome do repositorio]": "^1.0"
}

Using a branch:

"require": {
    "php": ">=5.3.0",
    "[vendor]/[nome do repositorio]": "dev-master"
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.