Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of Composer?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the use of Composer, I hope you will gain something after reading this article, let's discuss it together!

What is composer used for?

For students who do not know composer, there is bound to be this question.

This word is often seen on the Internet, especially in GitHub, and is often seen when using Laravel and Yii. It is recommended to use composer installation during installation. Is it only used during installation? I have also used other frameworks, why not install Thinkphp and Codeigniter with composer?

With these questions, we study.

Composer official website: https://www.phpcomposer.com

Composer is a tool used by PHP (5.3 +) to manage dependencies.

For a long time, PHP's open source approach has been project-level, that is, an open source is a project, such as a complete set of CMS (Dede, WordPress, discuz), a set of frameworks (Thinkphp, Codeigniter). Why is that? One of the very important reasons is that it is difficult for you to take it apart. If you take it apart, there is not an effective management tool to combine. As a result, no one cares about the small module that is taken apart.

Then composer appeared, it is an effective management tool, it is responsible for the management of small modules taken apart, and then effectively integrated to make it a complete project.

For example, logging uses monolog/monolog, HTTP client uses: guzzlehttp/guzzle, and so on.

Composer package platform: https://packagist.org, which contains a large number of excellent installation packages, we can easily use excellent code in our project with a composer command.

As a proud PHPer, we can't just use other people's development packages forever. We must develop a package for others and ourselves.

Now that we know that it has so many benefits, let's learn from it, starting with the installation of composer.

How is composer installed?

Official getting started document: https://docs.phpcomposer.com/00-intro.html

Through the above method, the installation can be completed.

Next, we use GitHub in conjunction with the Composer tool to demonstrate how to develop a Composer package.

How is the composer package developed?

For example, develop a composer package that handles numbers.

Create a project on GitHub

Log in to GitHub (if you don't have an account, create it), click "+" in the upper right corner, and select "New repository".

In the creation interface, Repository name enter "numberFormat", Description is optional, leave it unfilled for the time being, then select "Public" in Public (GitHub recommended method, free, everyone can access it) and Private (fee, specified access is free for individual developers after 2019-01-09), then select "Public" by checking "Initialize this repository with a README" and clicking the "Create Repository" button.

At this point, it indicates that an empty project named "numberFormat" has been created on GitHub.

Next, you need to code the remote project clone locally (Git command line, Git client).

Learn to create a composer.json

What are the parameters of composer.json and how to write them? please refer to the document: https://docs.phpcomposer.com/04-schema.html#composer.json

If a project wants to call the development package, you can know how to load the file through composer.json.

Composer.json can be created in two ways, one is composer init, the other is manual creation.

Let's first execute composer init to see the effect.

Create the numberFormat directory locally, and then git clone the project you just created.

/ / enter the local numberFormat directory composer init Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (/) [root/number-format]: number-format/number-format Description []: a packet Author [XinLiang, n to skip]: / / enter Minimum Stability []: / / enter Package Type (e.g. Library, project, metapackage, composer-plugin) []: / / enter License []: / / enter Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no {"name": "number-format/number-format", "description": "a package that handles numbers", "authors": [{"name": "XinLiang" "email": "109760455@qq.com"}], "require": {}} Do you confirm generation [yes]? / / enter

At this point, the local numberFormat directory to see the composer.json file, of course, you can directly in the directory in accordance with this format for manual creation, later directly edit the file.

Create project coding content

The development package is structured as follows:

-- src source code directory (required)

-- tests unit test directory (optional)

We create directories and files according to the established directory structure, and then modify them in composer.json.

Next, create a class (NumberFormat.php) in the src directory:

/ * Digital formatting class * @ author XinLiang * / namespace numberFormat Class NumberFormat {/ * formatted byte * @ param int $num digit * @ param int $precision precision * @ return string * / public static function byte_format ($num = 0 $precision = 1) {if ($num > = 1000000000000) {$num = round ($num / 1099511627776, $precision) $unit = 'TB';} elseif ($num > = 1000000000) {$num = round ($num / 1073741824, $precision); $unit =' GB';} elseif ($num > = 1000000) {$num = round ($num / 1048576, $precision) $unit = 'MB';} elseif ($num > = 1000) {$num = round ($num / 1024, $precision); $unit =' KB';} else {return number_format ($num). Bytes';} return number_format ($num, $precision).'. $unit;}}

Modify composer.json

{"name": "number-format/number-format", "description": "A package dealing with numbers", "authors": [{"name": "XinLiang", "email": "109760455@qq.com"}], "minimum-stability": "dev" "require": {"php": "> = 5.3.0"}, "autoload": {"psr-4": {"numberFormat\": "src/"}}, "license": "MIT"}

Now that our development package is complete, let's test whether the package is available.

Test the development package

In the local numberFormat directory, install through composer install

Composer install Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Writing lock file Generating autoload files / / indicates that the installation is successful

Surprised to find that there is an additional vendor directory in the local numberFormat directory.

Create a NumberFormatTest.php in the tests directory

/ * * Digital format test class * @ author XinLiang * / require'. / vendor/autoload.php'; use\ numberFormat; $number = '102400010; echo numberFormat\ NumberFormat::byte_format ($number); / / output: 97.7 MB

At this point, the test is successful, the next step is to release to the packagist platform, for the majority of developers to meet.

Publish to packagist platform

Packagist.org is the platform on which composer installs the package (you can log in with your GitHub account).

Now submit the local code to GitHub.

Publish to the packagist platform, after logging in, there is a "Submit" button in the upper right corner of the home page, click to enter the development package submission interface. In the "Repository URL (Git/Svn/Hg)" input box, enter the address of the GitHub project, click the "Check" button, wait a few seconds, it will show that the verification is successful, and the "Submit" button will be displayed, and click to complete the submission of the development package.

Congratulations, this package can be used in any PHP framework that supports composer.

So the question is, just now we wrote some crude packages, later we maintain the code, do we need to operate the new code in the original way?

No, no! Because we can set code updates on the GitHub platform and let packagist.org update automatically, isn't that cool!

Find the code repository in GitHub and select "settings"-> "Webhooks", which is automatically updated by binding by default.

If it is not bound, you can set it as follows: "settings"-> "Webhooks"-> "Add webhook"->

Payload URL fill in: "https://packagist.org/api/github""

Content type fill in: "application/json"

Secret fill in: "token provided by packagist"

Other defaults are fine.

Click "Add webhook" to finish.

At this point, after we update the code later, we will automatically synchronize it to packagist.org.

/ / other developers can obtain the package composer require number-format/number-format:dev-master in this way

Why is there: dev-master, why don't referencing other packages need to be set this way?

Because the other packages we refer to are stable packages, the default is:-stable.

Because we set the minimum-stability property in composer.json, we can understand the "version constraint", so I won't say much about it here.

When we release the package, what if we don't get the error report? it may be the problem of mirroring.

Composer set image address / / View global settings composer config-gl / / first: set domestic image composer config-g repo.packagist composer https://packagist.phpcomposer.com / / second: set domestic image composer config-g repo.packagist composer https://packagist.laravel-china.org / / third: set domestic image composer config-g repos.packagist composer https://php.cnpkg.org after reading this article I believe you have a certain understanding of "what is the use of Composer". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report