【PHP】包管理工具Composer
Composer是PHP目前普遍应用的包管理机制,可以方便地实现依赖包的管理、安装、更新和自动加载。
Composer的安装
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Composer项目配置
在项目中增加composer.json,语法定义见这里
{ "name": "项目名", "description": "项目描述", "type": "project", "require": { "php": ">=7.0", "illuminate/container": "^5.5", "illuminate/database": "^5.5", "illuminate/events": "^5.5", "illuminate/support": "^5.5" }, "require-dev": { "phpmd/phpmd":"^2.0.0", "phpunit/phpunit": "^4.8", "codeception/aspect-mock": "^2.0" }, "scripts": { "post-install-cmd": [ "../scripts/composer-post-install" ] }, "config": { "preferred-install": "dist" }, "autoload": { "files": [ "./function.php" ], "classmap": [ "./lib/" ], "psr-4": { "Model\\": "./Model/" } }, "autoload-dev": { "classmap": [ "../unittest/lib/" ], "psr-4": { "Test\\": "../unittest/testcase/" } }, "repositories": [ {"type": "composer", "url": "https://packagist.phpcomposer.com"}, {"packagist": false} ], "extra": { "include_files": [ "./bootstrap/const.php" ] } }
Composer常用命令
# 增加包依赖 composer require 依赖包 # 安装包 composer install # 更新包 composer update # 显示composer命令 composer list # 搜索包 composer search 通配符 # 在全局目录下跑composer命令 composer global 命令
至于可以用的公共依赖包可以在 Packagist 里找到。
Composer使用建议
- 常用的系统功能基本上都已经有现成的包,可以直接通过依赖方式引入到项目中直接使用
- 公司内相对底层的抽象代码也可以独立建包,通过依赖方式引入到不同的项目中;这样就需要建立私有的Packagist库;之前可以用Toran Proxy来建立公司私有库,但目前该工具已经停止运营,工具页面也直接引导到Private Packagist上了
彩蛋
Packagist 中国全量镜像:https://pkg.phpcomposer.com
附录
标签: PHP
还没有人抢沙发呢~