Teach you how to publish your own Composer package

I. Introduction

Composer is a tool used by PHP to manage dependencies. We not only have to learn to use packages provided by others, but also learn to make and share our own packages. The following demonstrates how to create our own Composer package.

Ready to work:

  1. Register a Github account
  2. Register a Packagist account

2. Practice

This case demonstrates how to create a third-party message push (Aurora Push) package.

1. Create a Github repository

Log in to Github, create a repository yanlongma/push, and clone the code locally:

$ git clone https://github.com/yanlongma/push.git

2. Create the Composer configuration file

Enter the project root directory and create the composer configuration file composer.json, which can be compser initcreated or manually. The final file content is roughly as follows:

{
    "name": "yanlongma/push",
    "description": "Third party message push",
    "authors": [
        {
            "name": "Yanlong Ma"
        }
    ],
    "license": "MIT",
    "require": {
        "php": ">=5.4"
    },
    "autoload": {
        "psr-4": {
            "YanlongMa\\Push\\": "src/"
        }
    }
}

3. Submit the code to Github

Write code according to the functions you need to implement. The final project structure of this project is as follows:

.git/  
.gitignore  
composer.json  
README.md  
src/
    Client.php	
    JPush.php

Submit the code to Github after the code is written and the test is OK.

4. Publish the package to Packagist

Log in to Packagist, check out the code of the https://github.com/YanlongMa/push.gitwarehouse , and the system will automatically set the relevant information of the package according to the composer.json file in the warehouse.

5. Set up automatic update of packages in Packagist

If automatic synchronization is not set, each time the code in Github is updated, it needs to be manually updated in the corresponding package, so it is recommended to set automatic update. Proceed as follows:

  1. Enter the yanlongma/push repository and select "Settings -> Integrations & services";
  2. Click "Add service", select "Packagist";
  3. Fill in the information corresponding to your Packagist account (click to view https://packagist.org/profile/ after logging in )
  4. After the configuration is complete, click "Test service" in the upper right corner. If "Okay, the test payload is on its way." appears, the configuration is successful.

6. Using Shared Packages

After publishing the package to Packagist, you can search and use the package according to the package name, and declare the package dependency in your own project:

$ composer require yanlongma/push

The specific use of this package can be found at https://github.com/yanlongma/push .

Note :

  • After the package is published to Packagist, it may take a few minutes to search on the client;
  • If there is no tag, you need to specify dev, the complete commandcomposer require "yanlongma/push @dev"

This article was first published on Ma Yanlong's personal blog , welcome to share, please indicate the source for reprinting. Ma Yanlong's personal blog: http://www.mayanlong.com Ma Yanlong's personal Weibo: http://weibo.com/imayanlong Ma Yanlong's Github homepage: https://github.com/yanlongma

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325372981&siteId=291194637