Home > Development Tools > composer > About the configuration settings of composer.json

About the configuration settings of composer.json

藏色散人
Release: 2020-08-15 15:32:10
forward
5083 people have browsed it

The following tutorial column from composer will tell you about the configuration settings of composer.json. I hope it will be helpful to friends in need!

About the configuration settings of composer.json

The value of the configuration file is key:val and must be wrapped in double quotes

# #1. Configuration file

Name nameThe package name consists of the user name and the warehouse name
Package version restrictions to request the Monolog package 1.0 .*. This means any version in the 1.0 development branch, or any version greater than or equal to 1.0 and less than 1.1 (>=1.0 <1.1).

Description description

Short description of the package. Usually this is just one line long.
Version of the package version

Optional if the package repository can infer the version from somewhere, such as the VCS tag name in the VCS repository. In this case, it is recommended to omit it.


Type type

It is recommended to omit this field and default it to library

Keywordskeywords

These can be used for searches And filtering, in array form such as keywords: ["xunsearch", "search engine", "yii", "yii2"]

Homepagehomepage

The URL of the project website.

Time time

Version release date, must be in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format.

License license

For a package, when there is a choice between licenses ("split licenses"), multiple can be specified as an array. Example of separated licenses:

{
      "license": [
           "LGPL-2.1",
           "GPL-3.0+"
      ]
}
Copy after login

Alternatively, they can be separated by "or" and enclosed in parentheses;

{
      "license": "(LGPL-2.1 or GPL-3.0+)"
}
Copy after login

authorsauthors

每个作者对象可以具有以下属性:
名称:作者姓名 通常他们的真名。 name 
电子邮件:作者的电子邮件地址。 email
主页:作者网站的网址。      homepage
作用:作者在项目中的作用(如开发者或翻译者) role
"authors": [
          {
                "name": "Nils Adermann",
                "email": "naderman@naderman.de",
                "homepage": "http://www.naderman.de",
                "role": "Developer"
          },
          {
                "name": "Jordi Boggiano",
                "email": "j.boggiano@seld.be",
                "homepage": "http://seld.be",
                "role": "Developer"
          }
    ]
Copy after login

Support support

Various information to obtain project support, support information includes the following:
Email: Email address for support. email

Issue: The URL of the issue tracker. issues

Forum: URL of the forum. forum:
wiki: URL of the wiki. wiki
irc: IRC channel support, such as irc://server/channel. irc:
Source: URL to browse or download the source. source
docs: URL of the document. docs
rss: The URL of the RSS feed. rss
An example:

{
      "support": {
            "email": "support@example.org",
            "irc": "irc://irc.freenode.org/composer"
      }
}
Copy after login

Suggestion

"suggest": {
          "monolog/monolog": "Allows more advanced logging of the application flow",
          "ext-xml": "Needed to support XML format in class Foo"
    }
Copy after login

archive archive

This command is used to specify the specified version of the package Make a zip/tar archive. It can also be used to archive your entire project, excluding excluded/ignored files.

composer 命令:php composer.phar archive vendor/package 2.0.21 --format=zip
Copy after login

exclude: Allows configuring a list of patterns to exclude paths. Pattern syntax matches .gitignore files. A leading exclamation point (!) will cause any matching files to be included, even if

the previous pattern excluded them. Leading slashes can only be matched at the beginning of project-relative paths. Asterisks are not expanded to directory separators.


Set options when creating a package. The exclude attribute can set which directories to exclude, for example:

"archive": {
  "exclude": ["/foo/bar", "baz", "/*.test", "!/foo/bar/baz"]
}
Copy after login

bin files

A group should be considered binary and symlinked to the file in bin-dir (from config). The directory address of the bin file relative to the warehouse package
"bin": [
  "util/xs"
 ]

2. Dependency managementPackage link require
require Tell composer the package your project depends on

"require": {
  "php" : "^5.5 || ^7.0",
  "monolog/monolog": "1.0.*"
}
Copy after login

require You need to change the package name (such as monolog/monolog ) that maps to a version constraint (e.g. 1.0.*).

composer show --platform command lists all PHP extensions available on the system

Restrict PHP version [php version requirements]

"require": {
      "swiftmailer/swiftmailer": 5.3.*@dev,
      "phpoffice/phpexcel": "dev-master"
}
Copy after login

Take swiftmailer as an example, swiftmailer/swiftmailer represents the package name ,5.3.@dev, is the version information. What this means together is that the

application we are going to develop depends on version 5.3 of swiftmailer. Among them:


5.3.* means that you can use version 5.3.1 or version 5.3.2. When obtaining it, composer will look for the latest version under version 5.3. The version number supports some more

broader constraints, such as >=1.0, >=1.0, <2.0. More specific information can be viewed at: http://docs.phpcomposer.com/01-basic-usage .md#The-

require-Key

@dev indicates that the development version can be obtained. Usually, a development version means an unstable version and is likely to contain bugs. Stability tags can apply to specific dependencies or

globally.

作用特定依赖项:默认情况下,composer只会获取稳定版本,如果这个例子我们不加@dev约束,而5.3.*版本都是开发版本,那么在
获取的时候composer就会报错,指出改版本不符合要求。如果确定这个开发版本没有问题,那么就可以通过加@dev ,让Composer获
取这个开发版本。

全局稳定性设置:通过设置minimum-stability的值,来告诉Composer当前开发的项目的依赖要求的包的全局稳定性级别,它的值包
括:dev、alpha、beta、RC、stable,stable是默认值。例如:"minimum-stability": "stable"

require-dev

有时候,我们会发现,有些包依赖只会在开发过程中使用,正式发布的程序不需要这些包,这个时候,就需要用到另外一个键,即
require-dev。例如,我们想用codeception进行单元测试,那么就可以通过require-dev引入这个开发环境下的依赖包:

"require-dev": {
      "codeception/codeception": "2.0.0 "
}
Copy after login

加了这个依赖后,再运行下命令看看效果。
composer install

三、自动加载
自动加载lib目录下的OrderManager.php文件
1.使用Files方式(ps:通常作为函数库的载入方式(而非类库))

"autoload":{
      "files":["lib/OrderManager.php"]
}
Copy after login

files键对应的值是一个数组,数组元素是文件的路径,路径是相对于应用的根目录。加上上述内容后,运行命令:

composer dump-autoload
Copy after login

让composer重建自动加载的信息,完成之后,就可以在index.php里调用OrderManager类啦。

2.Classmap方式自动加载

通过文件引入的方法虽然直观,但是很费劲,每个文件都得引入一次,实在不是好的解决办法。有没有更好的办法呢?尝试将
autoload的值改成:

"autoload": {
          "classmap": ["src/", "lib/", "Something.php"]
    }
Copy after login

再此运行composer dump-autoload,尝试调用,依然能够成功创建OrderManager类。其实,classmap通过建立类到文件的对应关系,
当程序需要OrderManager类时,compoer的自动加载类通过查找OrderManager类所在的文件,然后再将改文件include进来。因此,这
又导致了一个问题,那就是每加一个新类,就需要运行一次composer dump-autoload来创建类到文件到对应关系。

从classmap中排除文件
如果要从类映射中排除某些文件或文件夹,可以使用“from-classmap”属性,这些类将从类映射中跳过

"autoload": {
          "exclude-from-classmap": ["/Tests/", "/test/", "/tests/"]
    }
Copy after login

3. PSR0/4加载方式
PSR-0,PSR-4,类文件都要求有个命名空间

PSR-0则规定类名中的下划线_会被转化成目录分隔符

namespace SilkLib;
class OrderManager
{
      public function test()
      {
            echo "hello";
      }
 }
Copy after login

同时,文件夹的结构也要修改成:应用根目录\lib\SilkLib\OrderManager.php
然后修改composer.json里的autoload部分如下:

"autoload":{
      "psr-0":{
            "SilkLib":"lib/"
      }
}
Copy after login

这里需要注意的是,SlikLib是命名空间,lib是目录名,他们的组合告诉composer,文件搜索是在:lib/SilkLib/ 目录下,而不是
在 SilkLib/lib 目录下,这一点要特别注意,有点绕,容易弄错。

如果我们把命名空间改成 Slik\lib, 相应的目录结构要改成:应用根目录\lib\Silk\lib\OrderManager.php,autoload部分的写法
相应的也要改成:

"autoload":{
      "psr-0":{
    "Monolog\\": "src/",
            "Silk\\lib":"lib/"
      }
}
Copy after login

如果您需要在多个目录中搜索相同的前缀,则可以将它们指定为数组:

   "autoload": {
          "psr-0": { "Monolog\\": ["src/", "lib/"] }
    }
Copy after login

注意Silk\lib是双斜杆。好了,那我们试试再加一个类,然后不用运行composer dump-autoload命令,看看新类是否能加载上。在
lib目录下,新增一个ShipManager.php文件,内容如下:

namespace Silk\lib;
class ShipManager
{
      public function test()
      {
            echo &#39;hello ship class&#39;;
      }
}
Copy after login

尝试在vendor 同级目录下的index.php文件中调用:

$orderMgr = new Silk\lib\OrderManager();
$orderMgr->test();
$shipMgr = new Silk\lib\ShipManager();
$shipMgr->test();
Copy after login

运行成功,说明使用psr-0规范进行自动加载,比classmap更加方便。

可以直接指定到类级别。这对于在全局命名空间中只有一个类的库很有用,
php源文件也位于包的根目录中,则可以如下声明:

{
      "autoload": {
            "psr-0": { "UniqueGlobalClass": "" }
      }
}
Copy after login

如果您想要有一个可以使用命名空间的备用目录,则可以使用空的前缀,如:

{
    "autoload": {
          "psr-0": { "": "src/" }
      }
}
Copy after login

在psr-4关键字下,您可以定义相对于包根的命名空间到路径的映射

下面试试psr-4方式,整理下目录结构,改成:应用根目录\lib\OrderManager.php,类文件修改命名空间为Silk[包根目录文件夹名/
包里文件夹名称],
修改autoload

"autoload":{      "psr-4":
  {            "Silk\\":"lib",  // Silk 代表着--->项目类文件中的命令空间,将Silk 命名空间绑定到  /用户名/仓库名/lib 目录下                             // 文件夹路径:  
      vedor/
        用户名/
          仓库名称/
            composer.json
            lib/  
              Classname.php  lib这文件夹下的类文件 ----> 类名要和类文件同名, 类文件的命名空间统一为  Silk
    "Monolog\\": "src/",      }}//调用是  先引入vendor/autoload.php
Copy after login

使用命名空间:
use \Silk\类名---->等同于 使用包里的 lib\类文件---->类名

再次运行composer dump-autoload

如果您需要在多个目录中搜索相同的前缀,则可以将它们指定为数组:

"autoload": {
          "psr-4": { "Monolog\\": ["src/", "lib/"] }
  }
Copy after login

如果您想要有一个可以在任何命名空间中查找的备用目录,您可以使用一个空的前缀,如:

  "autoload": {
          "psr-4": { ""exclude" }
  }
Copy after login

The above is the detailed content of About the configuration settings of composer.json. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template