作者简介:

       姜海强:闷骚码农,互联网行业摸爬滚打数余载,先后担任中国体育直播TV主程、网信集团先锋支付架构师、奇虎360服务器端资深开发。热爱技术,喜欢分享,热衷领域:PHP/Golang语言、面向对象设计模式、Redis、Yaf、Yii2、微服务等。

视频课程

yaf+yar微服务-腾讯课堂
yaf+yar微服务-51CTO学院
CSDN学院

Github

个人主页
swoole-boot
roach
roach-orm

QQ群:

姜海强的QQ群

公众号:

360tryst公众号

composer

composer是PHP依赖管理工具,composer集成了PSR-4规范的类自动加载,composer官网地址:

https://www.phpcomposer.com/

composer包托管在https://packagist.org/,当我们需要检索某项功能的依赖包时可以到这里自行检索,目前这里包含了很多著名的依赖框架和库,如:phpunitguzzleelasticsearchphpoffice等。

1.composer的安装

下载前请确保您的电脑能够成功连接互联网并且正确安装了PHP环境,可以在命令行通过执行php -v命令验证PHP安装是否正确,如果可以正确输出PHP版本号表示安装正确。

下载安装步骤

下载https://install.phpcomposer.com/installer到当前目录

  1. # 安装
  2. php installer
  3. # 删除安装脚本
  4. php -r "unlink('installer');"

通过以上命令你的目录里会多一个composer.phar文件,这样我们就完成了对composer的局部安装,执行命令php composer.phar结果如下

  1. ______
  2. / ____/___ ____ ___ ____ ____ ________ _____
  3. / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
  4. / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
  5. \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
  6. /_/
  7. Composer version 1.10.8 2020-06-24 21:23:30
  8. Usage:
  9. command [options] [arguments]
  10. Options:
  11. -h, --help Display this help message
  12. -q, --quiet Do not output any message
  13. -V, --version Display this application version
  14. --ansi Force ANSI output
  15. --no-ansi Disable ANSI output
  16. -n, --no-interaction Do not ask any interactive question
  17. --profile Display timing and memory usage information
  18. --no-plugins Whether to disable plugins.
  19. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
  20. --no-cache Prevent use of the cache
  21. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  22. Available commands:
  23. about Shows the short information about Composer.
  24. archive Creates an archive of this composer package.
  25. browse Opens the package's repository URL or homepage in your browser.
  26. cc Clears composer's internal package cache.
  27. check-platform-reqs Check that platform requirements are satisfied.
  28. clear-cache Clears composer's internal package cache.
  29. clearcache Clears composer's internal package cache.
  30. config Sets config options.
  31. create-project Creates new project from a package into given directory.
  32. depends Shows which packages cause the given package to be installed.
  33. diagnose Diagnoses the system to identify common errors.
  34. dump-autoload Dumps the autoloader.
  35. dumpautoload Dumps the autoloader.
  36. exec Executes a vendored binary/script.
  37. fund Discover how to help fund the maintenance of your dependencies.
  38. global Allows running commands in the global composer dir ($COMPOSER_HOME).
  39. help Displays help for a command
  40. home Opens the package's repository URL or homepage in your browser.
  41. i Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  42. info Shows information about packages.
  43. init Creates a basic composer.json file in current directory.
  44. install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  45. licenses Shows information about licenses of dependencies.
  46. list Lists commands
  47. outdated Shows a list of installed packages that have updates available, including their latest version.
  48. prohibits Shows which packages prevent the given package from being installed.
  49. remove Removes a package from the require or require-dev.
  50. require Adds required packages to your composer.json and installs them.
  51. run Runs the scripts defined in composer.json.
  52. run-script Runs the scripts defined in composer.json.
  53. search Searches for packages.
  54. self-update Updates composer.phar to the latest version.
  55. selfupdate Updates composer.phar to the latest version.
  56. show Shows information about packages.
  57. status Shows a list of locally modified packages, for packages installed from source.
  58. suggests Shows package suggestions.
  59. u Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  60. update Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  61. upgrade Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  62. validate Validates a composer.json and composer.lock.
  63. why Shows which packages cause the given package to be installed.
  64. why-not Shows which packages prevent the given package from being installed.

如果想全局安装composer,将composer.phar文件移动到系统环境变量目录下并给予执行权限即可。

2.使用composer下载依赖

下面演示下载作者开发的一个简单工具依赖roachhttps://packagist.org/packages/jhq0113/roach

  1. composer require jhq0113/roach

执行完以上命令后,可以发现当前目录有一下变化

  1. -rw-r--r-- 1 qiang staff 59 7 4 11:46 composer.json
  2. -rw-r--r-- 1 qiang staff 1934 7 4 11:46 composer.lock
  3. drwxr-xr-x 5 qiang staff 160 7 4 11:46 vendor

最主要的变化是目录多了一个composer.json文件,内容如下,当我们需要更改依赖包时,修改require节点后,执行composer update即可。

  1. {
  2. "require": {
  3. "jhq0113/roach": "^1.0"
  4. }
  5. }

我们如何使用刚刚下载的roach依赖呢?在项目的入口文件直接require vendor/autoload.php,这样roach库中的所有类都可以自动加载了,使用时直接use命名空间即可。如下:

  1. <?php
  2. use roach\Container;
  3. require __DIR__.'/vendor/autoload.php';
  4. Container::set('startTime', time());
  5. echo Container::get('startTime').PHP_EOL;

以上例程输出

  1. 1593834582

composer还有很多应用,包括中国镜像初始化项目等,这里不再一一介绍,在官网都有详细介绍,自己去官网学习即可。

3.搭建自己的composer库

搭建自己的composer库的前提是得有个github账号,自己去注册即可。

  • 1.在github创建一个自己喜欢名称的仓库,如:roach

  • 2.git克隆roach仓库,添加composer文件,内容如下:

    1. {
    2. "name":"jhq0113/roach", //库名称
    3. "description": "roach",
    4. "keywords": ["roach", "php", "tools"],
    5. "license": "MIT",
    6. "homepage":"https://github.com/jhq0113/roach", //主页地址
    7. "type": "library",
    8. "authors": [ //作者信息
    9. {
    10. "name": "Jiang Haiqiang",
    11. "email": "jhq0113@163.com",
    12. "homepage": "https://github.com/jhq0113",
    13. "role": "Owner"
    14. }
    15. ],
    16. "autoload": {
    17. "psr-4": {
    18. "roach\\": "src/" //自动加载目录
    19. }
    20. }
    21. }

    编写自己的代码实现,案例配置中src目录会自动与\roach命名空间对应,在下载使用的时候,composer会自动完成名称空间的注册。

  • 3.去https://packagist.org/packages网站进行注册登录,如果没有账号,直接使用github账号授权登录即可。

  • 4.在packagist中点击submit,填写Submit package信息,这里仅填写github中的Repository URL即可,Check没有问题提交即发布了一个自己的开源composer包。

关于composer的介绍就到这了,如果我们在搭建和使用过程中遇到问题,可以加到QQ群【621674014】,我们一起讨论学习。

QQ群:

姜海强的QQ群

公众号:

360tryst公众号