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

Teach you to use Composer to manage dependencies

藏色散人
Release: 2020-08-07 13:23:46
forward
2904 people have browsed it

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
Copy after login

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

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

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
Copy after login

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
Copy after login

or simply change it to an executable file

>mv composer.phar composer
>chmod +x composer
Copy after login

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"
}
}
Copy after login

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$
Copy after login

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
Copy after login

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": [
....下略
Copy after login

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
...下略
Copy after login

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:

<?php
require &#39;vendor/autoload.php&#39;;
$phpmailer = new PHPMailer;
Copy after login

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 >, >=, !=, <=,

  • Use "~" before the version number , indicating the version before the next version number change. For example, ~2.7 means that the version is greater than or equal to 2.7 and less than 3.0

  • After the version number, you can also add different stability flags, such as 2.7.*@beta. The flags that can be used are: dev, alpha, beta, RC, stable

After specifying the version, execute composer install, and the latest version of the package will be installed according to the specified version rules.

In fact, every directory with a composer.json file is also the root directory of a package. However, if you want to make it a kit for others to use, you still need to add many settings, which are beyond the scope of this discussion.

* composer.lock

After the first installation of the package, this file will be generated, which records the information of the installed package. The real function of this file is: if there is this file in the directory, when performing the installation, it will not search for an updated version, but will install it according to the version recorded in this file. This design is important because the new version of the suite is likely to be incompatible with the currently used version. If the same version is not used, it is difficult to ensure the stability of the system. In the past, when using pear to manage packages, if you were not careful, tragedies caused by upgrades might occur.

In addition, as long as this file is added to version management, all developer directories will also have this file, so the package versions used by everyone will be consistent, which can reduce the need to use the package during development. Program compatibility issues caused by different versions.

* vendor directory

所有套件都会放置在这个目录,并且依照/的目录结构来组织。

* vendor/autoload.php
Copy after login

只要引用这个档案,就可以载入套件中所有对外公开的类别。基本上每个套件都会定义自己的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!

Related labels:
source:ithome
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template