PHP's way to create its own composer package

Source: Internet
Author: User
Tags autoload pear composer install hosting
This article mainly introduces you to PHP to create your own composer package method, the need for friends can take a look

Warehouse (Repository)

Warehouse is a common concept in software development, which is similar to the source (sources), mainly refers to the place where the resources are managed. Many of the software has a warehouse concept, such as Yum, NPM, maven, Git, and the protagonist composer of this article. The warehouse hosts the resources centrally and provides protection for the normal work of the software.

Packagist is the default central repository for composer, and most composer packages from the PHP community are hosted on the site. Packagist provides open, free hosting services that can be registered and freely released by any person, without review. Packagist is provided by private packagist for hosting and maintenance, the main difference is: Packagist's official website is https://packagist.org, hosting ..., managed code without open source, warehouse server can be located in the intranet, provide faster, More efficient package code hosting services.

Multiple warehouses can be configured, and composer will automatically identify the most appropriate dependencies for the project. The process for searching the package is as follows: first check whether the current project is configured with an additional warehouse, with priority being retrieved in the additional warehouse, no results up to the additional warehouse retrieval in the global configuration, no configuration or search without results, fallback to the default Packagist central warehouse retrieval. Packages in packagist are always retrieved unless the default warehouse is disabled. For this reason, composer recommends that PHP developers host the package on the Packagist site for easy retrieval and reference by others.

Configure the Warehouse

There are two ways to configure the composer warehouse: the command line and the edit configuration file. Composer Config is a composer configured command that can be used to configure project or global warehouse information, such as:

Composer Config [-G] repo.packagist composer https://packagist.phpcomposer.com

The second method is to edit the configuration file. Edit the project's Composer.json or ~/.config/composer/config.json to add a repositories configuration, such as:

"Repositories": {  "packagist": {"    type": "Composer",    "url": "Https://packagist.phpcomposer.com"  }}

The above configuration uses packagist China full volume mirror site as the default central repository. Deploying PHP projects in the Continental region, it is recommended that you use this repository directory to speed up the download of dependent packages.

The two most important parameters for warehouse configuration are type and URL. Type indicates the types of warehouses, and URLs point to specific URLs. Depending on the location of the warehouse, the commonly used type optional values are:

    1. Composer,composer Package hosting warehouse, such as Packagist China full-volume mirror;

    2. VCS, version control management systems, such as project addresses on GitHub;

    3. A bag on the pear,pear;

    4. Package, which is located on the Internet;

    5. Artifact, code zip package collection;

    6. Path, pointing to the specific location of the code.

The common values for the repositories on the Internet are composer and VCs; local projects, common values are artifact and path. For specific use cases, refer to composer official documentation.

Having mastered the concept of the warehouse and its configuration, we then created our own packages.

Create your own composer package

Creating a composer package takes only two steps: 1. Fill in the package description information; 2. Write the code. This article creates a Hello-composer package to demonstrate the creation process. The package features only one: the output string "Hello, composer!".

The description information of the composer package is stored in the Composer.json file, can be created directly (or copied from other projects) Composer.json file, manually fill in the necessary field information, you can also use the composer init command, interactive input package information, After generating the Composer.json file, complete the rest of the field information. We take the direct editing of the file, enter the following in the Composer.json:

{  "name": "Tlanyan/hello-composer",  "description": "Hello, composer!",  "type": "Library",  "require ": {    " php ":" >=7.0 "  },  " license ":" MIT ",  " authors ": [    {      " name ":" Tlanyan ",      " email ": "Tlanyan@hotmail.com"    }  ],  "minimum-stability": "Stable",  "AutoLoad": {    "psr-4": {      "tlanyan\\": "Src/"}}  }

The above content is basically a required field for a composer package. Other fields can be found in the Composer.json instructions on the composer website. Be aware of the fields marked Root-only, root-only indicates that the current package is the primary project. For example, the Require-dev field is developed in the current project, the package within the field is downloaded and placed in the vendor folder, and if the item is referenced by another project, the value of the field is ignored and the referenced package is not downloaded.

Then write the code. Create a new hellocomposer.php in the SRC directory:

namespace Tlanyan;class hellocomposer{public  static function greet ()  {    echo "Hello, composer!", php_eol;< c3/>}}

Code style recommendations refer to the PSR-2 specification, file naming and path specification recommendations refer to the PSR-4 specification. Also note that the path to the file needs to match the value of AutoLoad in Composer.json.

By simply two steps, we create our own composer package. The package is then referenced in another project.

Reference Composer Package

Create a new test project, reference the package created above and see the effect, as follows:

1. Create a new test folder, copy or create a new Composer.json file, configured as follows:

  {    ....    " Require ": {      " tlanyan/hello-composer ":" * "    },    " minimum-stability ":" Dev ",    " repositories ": {      "Local": {        "type": "Path",        "url": "/path/to/hello-composer"      }    },    ....  }

The configuration file needs to be aware of two points: 1. If the Hello-composer Composer.json file does not have a version field (or not a stable version), the minimum-stability value will not be installed if dev (default is stable); 2. You need to add a custom warehouse with a type value of path.

2. Execute the composer INSTALL-VVV installation dependency package and generate Tlanyan/hello-composer directory vendor directory after installation is complete.

3. Create a new test.php file in test, referencing the Hellocomposer class:

  namespace test;    Require "vendor/autoload.php";    Use Tlanyan\hellocomposer;    Class Test  {public    static main ()    {      hellocomposer::greet ()    }  }    Test::main ();

. Execute Test.php:php test.php, Output "Hello, composer!".

By configuring the composer warehouse, we successfully referenced the created Hello-composer package. Once the test is no problem, it can be posted online for other people to use. The following is a brief release process.

Publish Composer Package

There are several ways to publish composer packages to the Internet:

    1. Package into a zip and upload to any publicly accessible website;

    2. Upload to the code repository via version control software;

    3. Submitted to the Pear community;

    4. Submit to the private composer warehouse;

    5. Submit to Packagist.

The first four methods require the user to configure the warehouse information to retrieve the package (the pear community is almost dead and can be ignored). If the code is open source, it is recommended to submit to packagist to facilitate the search and use of PHP developers around the world, contributing to composer ecology.

To submit the package to Packagist, go through the following process:

    1. Create a project on GitHub and submit the code;

    2. Submit a package at Packagist input Project address;

    3. On GitHub configuration item, trigger Packagist automatic Update.

The first two steps are required, and the third step is optional. In the spirit of responsibility for the submitted package, we strongly recommend that you complete the third step.

The process of submitting the package involves GitHub and packagist two sites, and the relationship between GitHub and Packagist is: GitHub hosts the actual code and files, and packagist the author, package name, version number, download amount, and so on for the managed package. Briefly say Packagist is an index, and GitHub is the content provider.

Detailed steps can refer to the official website guidelines or online tutorials, the online content is too much, this article no longer repeat.

Summarize

This article introduces the concept of the composer warehouse, creates a complete composer package, and gives guidelines for submitting packages to packagist. After the user has mastered the relevant concepts and the operating mechanism, can submit the code to contribute to the community, but also can jump out of the Packagist free reference and install the dependency package.

Related Article

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.