composer
composer是PHP依赖管理工具,composer集成了PSR-4规范的类自动加载,composer官网地址:
composer包托管在https://packagist.org/,当我们需要检索某项功能的依赖包时可以到这里自行检索,目前这里包含了很多著名的依赖框架和库,如:phpunit、guzzle、elasticsearch、phpoffice等。
1.composer的安装
下载前请确保您的电脑能够成功连接互联网并且正确安装了PHP环境,可以在命令行通过执行
php -v命令验证PHP安装是否正确,如果可以正确输出PHP版本号表示安装正确。
下载安装步骤
# 安装
php installer
# 删除安装脚本
php -r "unlink('installer');"
通过以上命令你的目录里会多一个
composer.phar文件,这样我们就完成了对composer的局部安装,执行命令php composer.phar结果如下
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.10.8 2020-06-24 21:23:30
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
about Shows the short information about Composer.
archive Creates an archive of this composer package.
browse Opens the package's repository URL or homepage in your browser.
cc Clears composer's internal package cache.
check-platform-reqs Check that platform requirements are satisfied.
clear-cache Clears composer's internal package cache.
clearcache Clears composer's internal package cache.
config Sets config options.
create-project Creates new project from a package into given directory.
depends Shows which packages cause the given package to be installed.
diagnose Diagnoses the system to identify common errors.
dump-autoload Dumps the autoloader.
dumpautoload Dumps the autoloader.
exec Executes a vendored binary/script.
fund Discover how to help fund the maintenance of your dependencies.
global Allows running commands in the global composer dir ($COMPOSER_HOME).
help Displays help for a command
home Opens the package's repository URL or homepage in your browser.
i Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
info Shows information about packages.
init Creates a basic composer.json file in current directory.
install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
licenses Shows information about licenses of dependencies.
list Lists commands
outdated Shows a list of installed packages that have updates available, including their latest version.
prohibits Shows which packages prevent the given package from being installed.
remove Removes a package from the require or require-dev.
require Adds required packages to your composer.json and installs them.
run Runs the scripts defined in composer.json.
run-script Runs the scripts defined in composer.json.
search Searches for packages.
self-update Updates composer.phar to the latest version.
selfupdate Updates composer.phar to the latest version.
show Shows information about packages.
status Shows a list of locally modified packages, for packages installed from source.
suggests Shows package suggestions.
u Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
update Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
upgrade Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
validate Validates a composer.json and composer.lock.
why Shows which packages cause the given package to be installed.
why-not Shows which packages prevent the given package from being installed.
如果想全局安装
composer,将composer.phar文件移动到系统环境变量目录下并给予执行权限即可。
2.使用composer下载依赖
下面演示下载作者开发的一个简单工具依赖
roachhttps://packagist.org/packages/jhq0113/roach
composer require jhq0113/roach
执行完以上命令后,可以发现当前目录有一下变化
-rw-r--r-- 1 qiang staff 59 7 4 11:46 composer.json
-rw-r--r-- 1 qiang staff 1934 7 4 11:46 composer.lock
drwxr-xr-x 5 qiang staff 160 7 4 11:46 vendor
最主要的变化是目录多了一个
composer.json文件,内容如下,当我们需要更改依赖包时,修改require节点后,执行composer update即可。
{
"require": {
"jhq0113/roach": "^1.0"
}
}
我们如何使用刚刚下载的
roach依赖呢?在项目的入口文件直接require vendor/autoload.php,这样roach库中的所有类都可以自动加载了,使用时直接use命名空间即可。如下:
<?php
use roach\Container;
require __DIR__.'/vendor/autoload.php';
Container::set('startTime', time());
echo Container::get('startTime').PHP_EOL;
以上例程输出
1593834582
composer还有很多应用,包括中国镜像、初始化项目等,这里不再一一介绍,在官网都有详细介绍,自己去官网学习即可。
3.搭建自己的composer库
搭建自己的
composer库的前提是得有个github账号,自己去注册即可。
-
1.在
github创建一个自己喜欢名称的仓库,如:roach。 -
2.
git克隆roach仓库,添加composer文件,内容如下:{ "name":"jhq0113/roach", //库名称 "description": "roach", "keywords": ["roach", "php", "tools"], "license": "MIT", "homepage":"https://github.com/jhq0113/roach", //主页地址 "type": "library", "authors": [ //作者信息 { "name": "Jiang Haiqiang", "email": "jhq0113@163.com", "homepage": "https://github.com/jhq0113", "role": "Owner" } ], "autoload": { "psr-4": { "roach\\": "src/" //自动加载目录 } } }编写自己的代码实现,案例配置中
src目录会自动与\roach命名空间对应,在下载使用的时候,composer会自动完成名称空间的注册。 -
3.去https://packagist.org/packages网站进行注册登录,如果没有账号,直接使用
github账号授权登录即可。 -
4.在
packagist中点击submit,填写Submit package信息,这里仅填写github中的Repository URL即可,Check没有问题提交即发布了一个自己的开源composer包。
关于
composer的介绍就到这了,如果我们在搭建和使用过程中遇到问题,可以加到QQ群【621674014】,我们一起讨论学习。
6054

被折叠的 条评论
为什么被折叠?



