composer官网
composer学习
Composer 是 PHP5.3以 的一个依赖管理工具。它涉及 “packages” 和 “libraries”,但它在每个项目的基础上进行管理,在项目的某个目录中(例如 vendor)进行安装。
简单、方便的管理项目的第三方依赖库。
#搜索composer
brew search composer
#安装composer
brew install composer
成功安装结果
Updating Homebrew…
==> Auto-updated Homebrew! Updated 1 tap (homebrew/core).
==> Updated Formulae aws-cdk carton==> Downloading https://getcomposer.org/download/1.10.9/composer.phar
######################################################################## 100.0%
补充说明:
brew 方式安装composer属于全局安装,composer命令可以在全局使用。
#下载依赖(全局安装)
composer install
#初始化
composer init composer
#更新第三方依赖库
composer update
#指定某一个库的更新,其他的没有影响
composer update monolog/monolog
#composer版本更新,如果有则更新到最新版本
composer selfupdate
补充说明:
#更新第三方依赖库
composer update
此操作会直接从composer.json文件读取信息,下载库文件,然后同步更新composer.lock。此时这个操作可以看作先删除composer.lock文件,然后在执行install命令操作。
#create-project创建一个larabel项目 \
#在blog目录下载laravel/laravel项目
composer create-project laravel/laravel blog --prefer-dist
补充说明:
–repository-url: 提供一个自定义的储存库来搜索包,这将被用来代替 packagist.org。可以是一个指向 composer 资源库的 HTTP URL,或者是指向某个 packages.json 文件的本地路径。
–stability (-s): 资源包的最低稳定版本,默认为 stable。
–prefer-source: 当有可用的包时,从 source 安装。
–prefer-dist: 当有可用的包时,从 dist 安装。
–dev: 安装 require-dev 字段中列出的包。
–no-install: 禁止安装包的依赖。
–no-plugins: 禁用 plugins。
–no-scripts: 禁止在根资源包中定义的脚本执行。
–no-progress: 移除进度信息,这可以避免一些不处理换行的终端或脚本出现混乱的显示。
–keep-vcs: 创建时跳过缺失的 VCS 。如果你在非交互模式下运行创建命令,这将是非常有用的。
成功安装结果
Creating a “laravel/laravel” project at “./blog” Installing
laravel/laravel (v7.12.0)
- Installing laravel/laravel (v7.12.0): Downloading (failed) Downloading (failed) Downloading (failed) Failed to download
laravel/laravel from dist: The
“https://api.github.com/repos/laravel/laravel/zipball/5639581ea56ecd556cdf6e6edc37ce5795740fd7”
file could not be downloaded: failed to open stream: Connection
refused
Now trying to download from source- Installing laravel/laravel (v7.12.0): Cloning 5639581ea5 from cache Created project in
/Users/apple/Documents/Mac/C-repository/local/php/composer_test/blog@php -r “file_exists(’.env’) || copy(’.env.example’, ‘.env’);” Loading composer repositories with package information Updating
dependencies (including require-dev) Package operations: 99 installs,
0 updates, 0 removals
- Installing voku/portable-ascii (1.5.2): Downloading (100%)
- Installing symfony/polyfill-ctype (v1.18.0): Downloading (100%)
- Installing phpoption/phpoption (1.7.4): Downloading (100%)
- Installing vlucas/phpdotenv (v4.1.8): Downloading (100%)
…此处省略…
Downloading (0%) Downloading (0%) Failed to download
fzaninotto/faker from dist: Content-Length mismatch, received 40219
bytes out of the expected 3475269
Now trying to download from source
- Installing fzaninotto/faker (v1.9.1): Cloning failed using an ssh key for authentication, enter your GitHub credentials to access
private repos Head to
https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+appledeMacBook-Pro.local+2020-07-18+1239 to retrieve a token. It will be stored in
“/Users/apple/.composer/auth.json” for future use by Composer. Token
(hidden): No token given, aborting. You can also add it manually later
by using "composer config --global --auth github-oauth.github.com
" Cloning fc10d778e4[Symfony\Component\Process\Exception\ProcessTimedOutException] The
process “git clone --no-checkout
‘https://github.com/fzaninotto/Faker.g it’
‘/Users/apple/Documents/Mac/C-repository/local/php/composer_test/blog/v
endor/fzaninotto/faker’ && cd
‘/Users/apple/Documents/Mac/C-repository/loca
l/php/composer_test/blog/vendor/fzaninotto/faker’ && git remote add
compose r ‘https://github.com/fzaninotto/Faker.git’ && git fetch
composer && git re mote set-url origin
‘https://github.com/fzaninotto/Faker.git’ && git remote set-url
composer ‘https://github.com/fzaninotto/Faker.git’” exceeded the t
imeout of 300 seconds.create-project [-s|–stability STABILITY] [–prefer-source]
[–prefer-dist] [–repository REPOSITORY] [–repository-url
REPOSITORY-URL] [–add-repository] [–dev] [–no-dev]
[–no-custom-installers] [–no-scripts] [–no-progress]
[–no-secure-http] [–keep-vcs] [–remove-vcs] [–no-install]
[–ignore-platform-reqs] [–] [] [] []
补充说明:
安装指定版本的laravel composer create-project laravel/laravel=5.1.11 laravel
–prefer-dist
补充说明:
使用 Composer 从现有的包中创建一个新的项目,本质上是通过git clone(或者 svn checkout)克隆一个代码仓库到自己本地的vendor 目录。
composer.json 文件描述了项目的依赖关系;
例如,需要"monolog/monolog","phpmailer/phpmailer"这两个库,composer.json格式如下:
{
"require": {
"monolog/monolog": "1.0.*",
"phpmailer/phpmailer": "~5.2"
}
}
composer install
补充说明:
composer根据composer.json配置开始下载所依赖的库文件,安装完毕后会生成一个composer.lock文件(若无);composer.lock文件的作用是锁定当前的配置文件:如果已存在composer.lock,则在下次执行composer
install操作时会自动读取composer.lock中的信息。(注意:此时,修该的composer.json文件不会生效。)
除require属性以外,其实composer还支持其他很多属性,例如:
name
description
version
type
keywords
homepage
time
license
author
补充说明:
具体属性代表的意义及支持的全部属性参看https://getcomposer.org/doc/04-schema.md
第三方依赖库目录下也有个composer.json(称之为B.json),和项目工程composer.json(称之为A.json);B.json以monolog/monolog为例composer.json文件配置如下:
{
"name": "monolog/monolog",
"description": "Logging for PHP 5.3",
"keywords": ["log","logging"],
"homepage": "http://github.com/Seldaek/monolog",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Jordi Boggiano",
"email": "[email protected]",
"homepage": "http://seld.be"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {"Monolog": "src/"}
}
}
B.json 属于第三方库本身的配置文件,和项目的配置依赖没有关系,B.json用于制作自己的库文件然后发布供别人下载使用。
**注意:**项目目录下的composer.json才是真正的项目依赖配置文件。
除了库的下载,Composer 还准备了一个自动加载文件,它可以加载 Composer 下载的库中所有的类文件。只需要将下面这行代码添加到你项目的引导文件中即可使用:
require 'vendor/autoload.php';
Composer在安装或更新的时候可能会出现失败或无法访问的情况,这是由于访问的外部网络可能被墙了。所以为Composer配置了一个国内提供的镜像,终端中执行:
修改composer全局配置文件
#用国内镜像
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
#用国外镜像
composer config -g repo.packagist composer https://packagist.org
补充说明:
推荐使用镜像: https://pkg.phpcomposer.com
#配置composer.json文件,填写项目的依赖和其它的一些元数据
{
"repositories": [
{
"type": "git",
"url": "http://reader:[email protected]/erp/QRest.git” //目录依赖地址
},
{
"type": "git",
"url": "http://reader:[email protected]/oms/QUtils.git”
}
]
}
#初始化,下载第三方依赖
composer init
#进行代码更新
composer update
#进入到相应目录增加软链:
ln -s ../oms/QUtils/