composer

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到当前目录

# 安装
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】,我们一起讨论学习。

学习更多内容: https://404.360tryst.com

我的视频课程: https://edu.csdn.net/course/detail/9933

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍穹0113

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值