Composer

PHP-FIG

PHP-FIG(PHP Framework Interop Group, PHP框架可互用性小组)可理解为PHP通用性框架小组,他们制定PHP编码规范(PSR, Proposing a Standards Recommendation)。此小组从2009年开始,又几位框架开发者所组成,一直扩展到目前20多位成员,原名为PHP Standards Group。小组成立的最主要原因是希望PHP能够 有个最低程度的共同编码风格可以依循,以避免各家发展的写法。

PSR

PSR(PHP Standard Recommendations, PHP标准规范)是PHP-FIG组织制定的PHP规范,是PHP开发的实践标准。项目的目的在于:通过框架作者或代表之间讨论,以最低程度的限制,制定一个协作标准,各个框架遵循统一的编码规范,编码各家自信发展的风格阻碍PHP的发展,解决这个层序设计师由来已久的困扰。

Composer

Composer是PHP的安装包管理工具,服务于PHP生态系统,包括Composer和Packagist两部分。

  • Composer

Composer 是由 Jordi Boggiano 和 Nils Aderman 创作的命令行工具,作用是为项目自动安装所依赖的开发包。

Composer 理念借鉴自 npmbundlercomposer包含了一个依赖解析器用来处理开发包之间复杂的依赖关系,还包含下载器、安装器等。

  • Packagist

Packagist是Composer的默认开发包仓库,类似如Java的maven、Python的pip、Node.js的npm、前端有bower等。

packagist

下载安装

安装Composer要求

  • php 版本大于 5.5.9
  • php开启openssl扩展
# 创建目录并进入
$ mkdir composer && cd composer

# 下载composer
$ php -r "copy('http://install.phpcomposer.com/installer','composer-setup.php');"

# 下载composer
$ php -r "readfile('https://getcomposer.org/installer');" | php
# 上述命令不行将https替换为http
$ php -r "readfile('http://getcomposer.org/installer');" | php


# 安装composer
$ php composer-setup.php
Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:
# 需php.ini中开启openssl扩展
The openssl extension is missing, which means that secure HTTPS transfers are impossible.
If possible you should enable it or recompile php with --with-openssl

# 安装composer
$ php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.6.5) successfully installed to: D:\composer\composer.phar
Use it: php composer.phar

# 关于composer
$ composer about
Composer - Package Management for PHP
Composer is a dependency manager tracking local dependencies of your projects and libraries.
See https://getcomposer.org/ for more information.

# 查看composer版本
$ composer -V
Composer version 1.2.1 2016-09-12 11:27:19

# windows制作bat文件并将composer目录加入环境变量
$ vim composer.bat
@php "%~dp0composer.phar" %*
$ echo @php "%~dp0composer.phar" %* > composer.bat

切换源

查看镜像地址

$ composer config -gl repo.packagist

安装Composer后修改全局配置文件,采用中国镜像,加快下载速度。

  • 官方全量镜像:https://repo.packagist.org
  • 中国全量镜像:https://packagist.phpcomposer.com
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com

项目操作

初始化项目

# 初始化生成composer.json
$ composer init
$ cat composer.json
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0"
    },
    "require-dev": {
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

安装依赖

$ composer install

从当前目录读取composer.json处理依赖后,在当前目录自动创建vendor目录,并将require配置项中的依赖包下载安装到vendor目录下。

若当前目录存在composer.lock则从它读取依赖版本,而非composer.json,以确保依赖的版本。若没有composer.lock则处理往后会自动创建。

更新依赖

# 获取依赖的最新版本并升级composer.lock
$ composer update

# 使用composer安装时,若出现卡慢情况可添加-vvv参数,查看具体安装进度。
$ composer update -vvv

Composer的类采用PSR-4标准,Composer会自动生成vendor/autoload.php文件用于自动加载,利用它可自动引入文件并完成类库的自动加载。

引入类库

$ composer require 厂商/类库=版本
$ composer require phpmailer/phpmailer

引入类库的配置文件格式

{
  "require":{
    "厂商/类库":"版本"
  }
}

# 示例
{
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0"
    }
}

卸载类库

$ composer remove phpmailer/phpmailer

搜索依赖

# 搜索依赖
$ composer search monolog
# 列出依赖
$ composer show

# 检测composer.json是否有效
$ composer validate

# 升级composer
$ composer self-update

# 查看配置
$ composer config --list

# 打印自动加载索引,更新autoloader,优化项目的自动加载。
$ composer dump-autoload

安装Laravel

# 安装Laravel
$ composer create-projecct --prefer-dist laravel/laravel lrv

# 安装Laravel并指定版本
$ composer create-project --prefer-dist laravel/laravel lrv 5.5.*

你可能感兴趣的:(Composer)