Home  >  Article  >  Development Tools  >  Teach you to use Composer to manage dependencies

Teach you to use Composer to manage dependencies

藏色散人
藏色散人forward
2020-08-07 13:23:462679browse

The following tutorial column of composer will introduce you to using Composer to manage dependencies. I hope it will be helpful to friends in need!

Teach you to use Composer to manage dependencies

composer was originally a tool designed to manage package dependencies in Symfony, the PHP Framework. Because it is simple and easy to use, it has now become an independent open source project . Many frameworks and libraries can now be installed and managed using composer.

In fact, in PHP, there has been such a package dependency management tool for a long time, which is PEAR. However, the settings of PEAR are too complicated, and it is difficult to set dependencies for individual projects, so now Composer is loved by the public.

This introduction is only for users, so it will not cover the parts that package developers need to know.

* Installation

If you are a Windows user, you only need to download the installation file and execute the installation:

https://getcomposer.org/Composer-Setup.exe

If you want to install manually, you can refer to Guidelines from the official website:

http://getcomposer.org/doc/00-intro.md#installation-windows

If you are a user of UNIX Like system, you can install it through this command: (curl needs to be installed first)

curl -sS https://getcomposer.org/installer | php

The installation program will check the PHP settings. , and then download composer.phar to the current directory. To execute composer, you can execute

php composer.phar

or simply change it to an executable file

>mv composer.phar composer
>chmod +x composer

and then execute ./composer.

However, if you need them in different working directories and there is no problem with execution permissions, you can also copy the files directly to /usr/local/bin.

* Set dependencies

When using composer in a project, you must first generate a composer.json file, which specifies the package and version to be used. For example, when you need to use phpmailer to send a letter, you can specify it like this:

{
"require": {
"phpmailer/phpmailer": "~5.2.7"
}
}

and then execute the installation:

eng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing phpmailer/phpmailer (v5.2.7)
    Downloading: 100%         
Writing lock file
Generating autoload files
Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$

and the installation is complete. Take a look at what is installed:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ ls -l
total 16
-rw-r--r--  1 fillano  staff    66 10 11 18:15 composer.json
-rw-r--r--  1 fillano  staff  2330 10 11 18:16 composer.lock
drwxr-xr-x  5 fillano  staff   170 10 11 18:16 vendor

According to the files in the directory, we can find that originally there was only the composer.json file. After installation, there is a composer.lock file and the vendor directory. Let’s take a look at the contents of composer.lock first:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ cat composer.lock
{
    "_readme": [
        "This file locks the dependencies of your project to a known state",
        "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
    ],
    "hash": "065c23f92d5ae579cb91beff67f41196",
    "packages": [
        {
            "name": "phpmailer/phpmailer",
            "version": "v5.2.7",
            "source": {
                "type": "git",
                "url": "https://github.com/PHPMailer/PHPMailer.git",
                "reference": "8717a79565b2c0ed67f851d70e1949febdf3b226"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/8717a79565b2c0ed67f851d70e1949febdf3b226",
                "reference": "8717a79565b2c0ed67f851d70e1949febdf3b226",
                "shasum": ""
            },
            "require": {
                "php": ">=5.0.0"
            },
            "require-dev": {
                "phpdocumentor/phpdocumentor": "*",
                "phpunit/phpunit": "*"
            },
            "type": "library",
            "autoload": {
                "classmap": [
                    "class.phpmailer.php",
                    "class.pop3.php",
                    "class.smtp.php"
                ]
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "LGPL-2.1"
            ],
            "authors": [
....下略

looks like the information about the package that was just installed.

Let’s take a look at what’s in the vendor directory:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ tree vendor
vendor
├── autoload.php
├── composer
│   ├── ClassLoader.php
│   ├── autoload_classmap.php
│   ├── autoload_namespaces.php
│   ├── autoload_real.php
│   └── installed.json
└── phpmailer
    └── phpmailer
        ├── LICENSE
        ├── PHPMailerAutoload.php
        ├── README.md
        ├── changelog.md
        ├── class.phpmailer.php
        ├── class.pop3.php
        ├── class.smtp.php
        ├── composer.json
        ├── docs
        │   ├── Callback_function_notes.txt
        │   ├── DomainKeys_notes.txt
        │   ├── Note_for_SMTP_debugging.txt
        │   ├── extending.html
        │   ├── faq.html
        │   ├── generatedocs.sh
        │   └── pop3_article.txt
...下略

It seems that in addition to the phpmailer directory where phpmailer is installed, there are mainly autoload.php files and composer directories.

It turns out that to load a package installed through composer, you need to reference the vendor/autoload.php file first, and then you can use phpmailer. Write a simple program to test it:

There are no errors after execution, which means phpmailer can load normally... Next, let’s take a look at the uses of these files.

* composer.json

For users, this file is mainly used to maintain dependencies. Just add an object to the file in the "require" attribute, where the attribute name is the package name and the value is the version. The package name is divided into two parts. The first part is the vendor, and the second part is the actual package name, separated by "\". There are several rules for versions:

  • Directly specify the version number, for example, 2.7.3

  • After specifying the main version number, use "*" to specify Minor version number, for example, 2.7.* means the version number is greater than or equal to 2.7.0. Versions less than 2.8.0

  • ## use >, >=, !=, e187be6b48df49ceaf1536548537893a/acab04e509e5b64e7e2bc0d60ab53414的目录结构来组织。
    * vendor/autoload.php

    只要引用这个档案,就可以载入套件中所有对外公开的类别。基本上每个套件都会定义自己的autoload规则,在安装时,composer会把这些规则加入,这样透过autoload.php就可以直接使用所有已安装的类别。

    =====

    从这些地方可以看到,Composer这个套件管理工具,在设计上已经做了很周密的考量,只需要简单指定要使用的套件及版本,一个指令就可以安装完毕,引用一个胆案之后就能使用,这样真的非常方便。所以目前几乎所有的程式库以及Framework,应该都逐渐在套用这个工具了。未来在开发PHP程式,恐怕最基本的工具也就是composer。

The above is the detailed content of Teach you to use Composer to manage dependencies. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ithome. If there is any infringement, please contact admin@php.cn delete