Develop your first composer package

Source: Internet
Author: User
Develop your first composer package. composer is a PHP dependency management tool. This article describes how to build a package and submit it to Packagist, in this way, you can easily use your package through composer.

To develop a composer package, follow these steps:

  1. Initialize the composer. json file
  2. Define the namespace and package name
  3. Functions to be implemented in the implementation package
  4. Submit to GitHub
  5. Register the Packagist
Initialize the composer. json file

After the composer is installed, you can run composer init locally and set composer. json through interactive command lines.

The following describes several attributes and general settings:

  1. Name: this attribute defines the package name, which is separated by a slash (/). The supplier name is followed by the Packagist name. The vendor representative Packagist website provides a unique name for developers, it is used to organize packages and prevent name conflicts. Therefore, you are advised to visit https://packagist.org/packages/yourvendornameto replace yourvendorname in the url with the name you want. if the page does not contain 404, it indicates it has been registered.
  2. License. We recommend that you read two articles about the license and how to select the license for the open-source project.
  3. The dependencies required by require to install the current package. The current package is installed only when all dependencies are installed.
  4. Autoload:PSR-4OrPSR-0Settings, more recommended use of PSR-4 standards.

On http://json-schema.org/, the definition of JSON Schema and implementation of various functions in various languages are introduced. validator is an online verification service. In fact, the simplest thing is to use composer validate composer. json to verify whether the file is incorrect.

This is the video I demonstrated setting composer. json.

Project structure

Taking the single sign-on sdk I developed as an example, this project is based on Laravel to implement simple access to the single sign-on system for website access. applications can access SSO only by registering with the server and implementing the specified interface.

The project structure is a typical MVC structure,

.└── geo    └── geosso        ├── LICENSE        ├── README.md        ├── composer.json        └── src            ├── Contracts            ├── Http            │   ├── Controllers            │   ├── Middleware            │   └── Requests            ├── ParamsBean            ├── Providers            ├── Support            └── config12 directories

LICENSE, README. md, and composer. json are manually added after tree-d is run.

The project root directory is defined under src and is also defined in composer. json, so that when composer loads this package, it knows how to parse the file path through the namespace.

The Http directory indicates the Request Response. the Controllers under the Http directory indicates the controller of the valid request. Middleware indicates the first level of the request. the Middleware intercepts the request and Requests obtains the front-end request and filters the request.

Contracts indicates the interface definition. ParamsBean represents the parameter encapsulation when the application layer communicates with the underlying service. each parameter is obtained through Bean instead of passing an array to make the call consistent, and type detection is performed when the interface is forced to be called, to a large extent, parameter transmission between layers can be unified.

Providers represents Laravel's service container. through the service container, you can register routing and configuration, load helper classes, and bind interfaces with them.

Support is a helper class that encapsulates commonly used logic-independent functions. config represents the application's own configuration. through config, you can easily set the configuration and use the global function config () call.

Submit to GitHub

According to the previous steps, a package has a basic skeleton. Next, upload the package to GitHub, configure the project, integrate the continuous integration service, and release the open source project license.

When initializing a project, GitHub can select generate. gitignore file, select license, initialize the README. md file, switch to the local project directory, and upload the directory to GitHub by following the steps below:

> Git init # Initialize repository> git remote set-url origin -- push -- add git@github.com: jayxhj/geosso. git # add remote tracking repository address> git add.> git commit> git push origin master

Submit to Packagist

Packagist is the default package metadata address of composer. after obtaining the metadata from Packagist, the code is pulled from GitHub. Therefore, after uploading your developed package to GitHub, you need to register it in Packagist, so that people all over the world can pull your code through composer.

Only three steps are required to submit to Packagist:

  1. Register an account
  2. Submit the SDK at https://packagist.org/packages/submit
  3. Set webhook so that the Packagist can be synchronized to the Packagist in a timely manner after the package update is submitted.

Since then, a basic package development has ended. You can use composer to manage PHP dependencies, compile composer package to expand your own class libraries, and introduce other class libraries to fill in your own functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.