How to make a composer package available? [duplicate]

3

The json below is part of the composer.json file of a package I created:

{
    "name": "libspkgs/utils",
    "type": "library",
    "description": "Descrição aqui.",
    "keywords": ["key1", "key2"],
    "homepage": "https://endereco.com",
    "license": "MIT",
    "authors": [
        {
            "name": "Nome do autor",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php": ">=5.6.27"
    },
    "autoload": {
        "psr-4": {
            "Pkg\": "lib/"
            }
    },
    "minimum-stability": "dev"
}

I'm adding it to Laravel this way:

composer require libspkgs/utils v1.0.0

But how do I always add the last release ?

    
asked by anonymous 15.11.2016 / 02:22

1 answer

3

Standard provisioning of a package

First, you need to have an account at Packagist . Currently (year 2016), you can use your Github account to create it.

Then you need to have a public repository to be pointed to by Packagist.

It is important to mention that because of organizational issues, it is important that the repository name needs to have the same "library name" as the one that will be used in the composer.

For example, for a package named vendor_name/library_name in Packagist, it would be important for your repository in Github to call library_name .

Note : To continue this tutorial, you should keep in mind that you need to have considerable knowledge about versioning tools (such as GIT, for example).

After creating your repository, I recommend following some guidelines for creating your library.

For example, a very common pattern is to define your namespace from the src folder of your project:

library_name/
    .gitignore
    composer.json
    src/
       Hello.php

So we could configure composer.json as follows:

 "name" : "vendor_name/library_name",

 "required" : {
      "php" : ">=5.4"
 },

 "autoload" : {
       "psr-4" : {
           "VendorName\LibraryName\" :  "src/"
       }
 }

Your class Hello.php within src , obviously, should look like this:

namespace VendorName\LibraryName;

class Hello {}

Note : To test your library before submitting it, you must run the composer dump command to generate the autoload. If you have dependencies on other libraries, you should use composer install .

After all this, you can commit and push your changes to the repository:

>>> cd library_name
>>> git commit -am "my first commit"
>>> git push

After this, you need to submit your library to Packagist via this form:

Aftersubmission,youneedtoinsertyourTOKENPackagistAPIintothesettingsofyourGithubrepository.

Youshouldclickonthe"settings" option and then "integrations and services". After that, in the "add service" option you should choose "packagist".

Afterthat,youshouldclickonthe"packagist" service that has been added, and configure it by placing your user and Packagist token.

See:

TheTokentobeaddedcanbefoundonthisPackagistscreen:

After doing all this, you can now test whether your library is working properly by using the command:

composer require vendor_name/library_name

But what about versioning?

You need to set a tag in your repository so that you can mark a "usable" version of your library. For example, if you are already sure that your library is ready for use, you can set a version for it.

You can define a tag like this:

git tag 0.0.1

Then, to send it to your repository, you need to run the command:

git push --tags

Note that tags must follow a pattern. I usually always use the three sets of numbers.

The versions in your Packagist will be organized according to these numbers.

For example;

1.0.0
0.0.3
0.0.2
0.0.1

For the answer does not get too long, I suggest reading some posts from the site:

16.11.2016 / 12:25