Home  >  Article  >  Development Tools  >  You can learn composer quickly!

You can learn composer quickly!

藏色散人
藏色散人forward
2020-07-04 13:21:152999browse

The following tutorial column of composer will introduce to you how to learn composer. I hope it will be helpful to friends in need!

You can learn composer quickly!

##When the system has different web applications, but they need to be shared What to do with a lot of code

When the system needs an extended function and someone on the Internet just provides how to use it
How to upgrade, downgrade, rollback PHP code
How to distribute tasks, how to let multiple engineers work together Development tasks

I came into contact with PHP in 2011, when V5.3.5 was just released. From the language level, I don't think PHP has too obvious flaws. On the basis of our rich web-oriented function library, we also have classes, SPL, anonymous functions, etc. These features (not "special" at all) are enough to support the coding needs of a large project.

You can learn composer quickly!PHP5.3

However, when we actually develop and really want to use PHP to write code, we often encounter some crazy things. Problems, these problems have nothing to do with PHP. But it's still a headache. When we want to write a website, we may need a verification code, but in most cases, I don't want to write a verification code myself. There are so many verification code types on the Internet, so I naturally want to use them directly. But when I want to use it directly, what I have to do is:

    Go to the search engine to search, and then see if there is any suitable code in each result that can be used by me.
  1. I found a class. Now I need to introduce this class into my project. In which directory should I put it? How to autoload? Does it depend on any extensions? Will it need to be used on a higher version of PHP than I am currently using? These are all problems I want to solve.
  2. If I want to solve all the problems mentioned in 2, then why don't I just write one?
  3. f**k it
Even if I use my own code, when I have multiple web applications (computer side, wap side, api interface is normal), of course I I hope they are not living in a project (directory), which will increase the difficulty for me to view the specified files, thereby also increasing my maintenance costs. But when I separated these web applications, there were so many common codes (model, logic, auth...), how should I deal with these codes? I modified a small logic of a web application, and I also When I modify it in other applications, either I can’t remember it, or even the smallest change makes me want to break the computer, quit my job, or go out to relax.

Okay, I will split these codes and use them with each other through autoload. This will also allow more people to participate in the development, but the online situation is so complicated, in case there is a problem with any piece of code. , in case there is a web application that is special and the new code does not apply to it. Maintenance is also a problem. If you take over such a web application that depends on many other projects, you may have a lot of trouble if you change the code slightly, because the autoload code makes it difficult for people to intuitively know how the web application is used. Which code for which other projects.

But when it comes to PHP, I don’t want to break it because it’s so convenient to write. I don't want to get out of the trap yet. But if the above problems are not solved, I personally think that writing PHP is still a very frustrating thing. Let's see how other languages ​​solve this problem. JAVA's natural packaging mechanism allows it to use maven, node's npm, and even Perl, which is older than PHP, has cpan. Shouldn't PHP have a package management mechanism?

Fortunately, these problems did not accompany my PHP time for too long, because soon, PHP had Composer, and it was dawn.

Composer is a dependency management tool for PHP. It allows you to declare code libraries that your project depends on and it will install them for you in your project.

This is the introduction of Composer’s Chinese official website.

I try to elaborate on this sentence from my experience.

It allows you to declare the code base that the project depends on. This means that when you want to use the code, you no longer need to copy it yourself. Instead, you can tell Composer through a declaration. Just like going to a restaurant to eat, you don’t have to. Teach the chef how to cook it, let alone cook it yourself, and you don’t have to serve the plate and eat it yourself. Instead, tell the waiter what you want to eat, just tell him. Of course, you can’t tell him that I have an upset stomach today, cook me something. It is easier to digest lighter dishes. Anyway, I never order like this. I always have to tell them what dish you want to eat and the specific name of the dish. This is the difference from looking for code in a search engine before. You can’t tell Composer through keywords, but you have to tell it the name of the code library you want. WTF? How do I know the name of the code? It is impossible for anyone to know the name of other people's codes, unless there is a place that contains all the codes and provides a search function for us to find them and know their names.
packagist.org does this This. We no longer have to rely on luck to search in various search engines. When searching for keywords here, no ads will appear, neither Putian nor JD will appear.

It will install them for you in your project: After telling Composer, Composer will naturally help us bring the dishes. This is something that anyone can understand, the code we want We don’t know which server it is on, but Composer will help us download it locally. But there is still a question here, how to use it after downloading it. We know that if you want to use a file in PHP, you must include or require it. After Composer is downloaded, how to eat this dish? Do you need to prepare your own bowls and chopsticks? Fortunately, there is another good thing called PHP-FIG. This thing does not produce code and does not provide any solutions to practical problems. The only thing he does is BBQ, so what does he BBQ about? As I said above, due to the lack of some basic tools (such as Composer), it is difficult for PHP development to have some standards, such as coding standards, such as directory structure, such as how to automatically load classes, such as how to log, such as how to use cache , what will this lead to? Different companies and different PHP programmers will start to show their magical powers. Of course, this will not matter in the short term for development, but in the long run, it will increase development costs and maintenance costs. When we change to another company When we take over a project, we have to understand the code from scratch. Even in a team, we will increase communication costs because there are no standards. So PHP-FIG does just that: sets standards. The standards he formulated are:

  1. Encoding specifications (psr-1 psr-2)
  2. Auto-loading specifications ( psr-4)
  3. Some common interface log(psr-3) cache(psr-6) http(psr-7)

These standards are described in detail on the official website. What we are going to discuss here is psr-4. I will elaborate here based on my own understanding and experience: psr-4's automatic loading is based on folders and namespaces. We need to specify that a root directory corresponds to a root namespace. On this basis, we can remove the root namespace. Namespace and class name outside the namespace to find this PHP file in the root folder and load

#根文件夹 lib#根命名空间 model#file lib/A.phpnamespace model;class A {}#file lib/entity/B.phpnamespace mode\entity;class B{}#file demo.php$a = new \model\A();$b = new \model\entity\B();

Composer can realize the mapping according to the specified standard (such as psr-4) relationship (such as lib->model in the code) to generate the function of automatically loading classes. In fact, Composer provides these standards:

files specifies the path to the PHP file. This method will load these files every time it is requested. It is suitable for PHP files with some common functions

Classmap is smarter than files. You can specify a folder or a file for automatic loading. The disadvantage is that even if you specify a folder, adding a file to this folder requires Composer to regenerate the autoload file, which is suitable for some applications that cannot Use psr-4 classes or class libraries, such as a third-party interface client. This client may have existed before the psr-4 rules appeared. If we still want to use Composer for management, we can use this method

psr-0 The predecessor of psr-4 was outdated before, just pretend I didn’t say it
psr-4 Just like what I introduced above Yes, adding one or more files in this way does not require regenerating the autoload file, because it is loaded according to the mapping relationship between the namespace and the folder.

So what are the benefits of Composer implementing this?

We don’t need to write any autoload files ourselves. At the same time, this standard is easy to understand and accept, and the cost of maintaining and learning the code is also reduced.
As long as the third-party libraries we need also use Composer to handle automatic Loaded, we only need to require this package, then Composer will also handle the code for loading this third-party library. We have a super powerful autoload file

So, what we have to do is learn to deal with Composer and then Start enjoying code from developers around the world.

就像上面描述的,Composer就像一个机器猫,你要什么它就给什么,那么交互的方式就类似于SQL语句那样,告诉它你要什么然后它给你结果。所以我们要做的就是描述需求,也就是当产品经理,好过瘾。

{
    "name": "fmw/test",
    "description": "fmw test",
    "authors": [
        {
            "name": "zzc",
            "email": "2272713550@qq.com"
        }
    ],
    "repositories": [
        {
            "type": "composer",
            "url": "http://package.fmw.com"
        }
    ],
    "version":"1.0.106",
    "require": {
        "fmw/other-layer":"1.*",
        "fmw/common":"1.*"
    },
    "require-dev":{
        "php-console/php-console": "^3.1",
        "phpdocumentor/phpdocumentor": "2.*"
    },
    "autoload":{
        "psr-4":{
            "model\\":"src/"
        }
    }
}

以上代码是一个我用过的composer配置文件,可以看出这是一个标准的json。我们来看一下这段json的每个key:

name和description是你给这个php项目起的名字,当这个项目仅仅是一个web项目,这两个其实不是很重要,但是这个项目其实是一个向外发布的代码库,就很关键了,name需要独一无二,description需要一句话来描述这个包的作用。

authors就是相当于宣布一下主权,可以有多个

repositories相当于你需要下载的代码库所在的仓库,默认会有一个全局的仓库,具体是什么就不在这里说了,上面的某个网址有介绍,在这里添加一个是因为如果你有个私人的仓库(有些代码不太适合放在公开的仓库吧),则可以在这里声明

version是版本号,这个是跨时代的功能啊,有了这个,PHP程序员也可以刷版本号了啊!

require则是上面阐述了很多的功能,解决了我说的那些痛点,通过“name”:"version"声明,可以有多个,require以后使用composer install命令composer会下载代码并自动加载
require-dev用法一致,但是功能不同,是用来声明一些在开发时候才用到的包,比如测试、文档等等

autoload 上面有介绍,就不废话

上面工作做完以后,执行composer install我们可以看到和composer.json同级的文件夹下生成了一个vendor文件夹,我们新建一个php文件引入vendor下的autoload.php文件就可以使用包和我们自己声明的autoload的php文件了
#index.php

include ‘./vendor/autoload.php’;

到这里,我们就算会用了composer,至于如何使用composer的功能就不拾人牙慧了,但是还有一些问题想讨论一下。

比如有些代码不太适合放在公开的仓库,但是我们还是希望包的形式来使用,毕竟这样的话,一个公司内部就很容易分工了,每一个PHP程序员维护若干个包,多方便,所以建立一个内部的代码仓库是很重要的。这时候Composer官方提供的工具satis就可以发挥作用了。

Simple static Composer repository generator

这是它的介绍,一个简单的Composer仓库生成器。使用它的步骤如下:

在合适的目录执行 php composer.phar create-project composer/satis --stability=dev --keep-vcs(前提是你已经按照Composer)
新建一个satis.json 实例如下

{
    "name": "My Repository",
    "homepage": "http://packages.dev.com",
    "repositories": [
        {"type": "vcs", "url": "http://git.dev.com/maxincai/package1.git"},
        {"type": "vcs", "url": "http://git.dev.com/maxincai/package1.git"},
    ],
    "require": {
        "maxincai/package1": "*",
        "maxincai/package2": "*",
    }
}

执行 php bin/satis build satis.json public/(public就是所有包的存放目录)
将public目录作为一个web服务对外发布就好了

使用的时候只需要在repositories多加一项(就像我在上面的composer.json做的那样),然后引入包就好了

关于Composer,上面就是我目前要说的了,通过Composer我们可以将业务逻辑、通用函数、逻辑拆分成不同的包,再也不需要做拷贝代码的蠢事了。

The above is the detailed content of You can learn composer quickly!. For more information, please follow other related articles on the PHP Chinese website!

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