I'm just getting started with Composer. I want to make sure I correctly understand what Composer does when it downloads and installs packages from Packagist using Composer's defaults. Is this correct:
我刚刚开始使用Composer。我想确保我正确理解Composer使用Composer的默认设置从Packagist下载和安装软件包时的功能。它是否正确:
- The "Require" section of the
composer.json
file specifies the packages that will be downloaded and installed from Packagist. Only those packages listed in the "Require" section will be downloaded and installed. - composer.json文件的“Require”部分指定将从Packagist下载和安装的软件包。只下载并安装“要求”部分中列出的那些软件包。
- During the download and install process in Step #1, an
autoload_namespaces.php
file is created within the defaultvendor
folder (which is created in the root of the project during downloading and installation). This file contains a namespace definition for each downloaded and installed package which is mapped to a folder on the file system - 在步骤1中的下载和安装过程中,将在默认供应商文件夹(在下载和安装期间在项目的根目录中创建)中创建autoload_namespaces.php文件。此文件包含每个下载和安装的包的命名空间定义,该包映射到文件系统上的文件夹
- The classes contained within the files downloaded in Step #1 are only accessible via
autoload_namespaces.php's
namespaces IF EACH DOWNLOADED FILE CONTAINS A NAMESPACE DEFINITION THAT MATCHES ONE OFautoload_namespaces.php's
NAMESPACES. If no namespace definition is present within a downloaded file the classes within the file are assigned to the GLOBAL namespace. - 步骤1中下载的文件中包含的类只能通过autoload_namespaces.php的命名空间访问。如果每个下载的文件都包含与autoload_namespace.php的一个名字匹配的NAMESPACE定义。如果下载的文件中不存在命名空间定义,则文件中的类将分配给GLOBAL命名空间。
- The "Autoloader" section of the composer.json file is ONLY used to define custom namespaces and map those custom namespaces to a folder on the file system. Downloaded and installed package namespaces are defined only in the
autoload_namespaces.php
file AND ARE NOT AFFECTED IN ANY WAY BY THE "Autoloader" SECTION ofcomposer.json
, ASSUMING THAT A NAMESPACE PRESENT WITHIN THEautoload_namespaces.php
FILE IS NOT DUPLICATED IN THEcomposer.json
"Autoloader" SECTION. - composer.json文件的“Autoloader”部分仅用于定义自定义命名空间,并将这些自定义命名空间映射到文件系统上的文件夹。已下载和安装的软件包名称空间仅在autoload_namespaces.php文件中定义,并且不受composer.json的“自动加载器”部分的任何影响,假定在autoload_namespaces.php文件中存在的NAMESPACE文件未在作曲家中重复使用。 json“Autoloader”部分。
Is my understanding about the above items correct? If not, would you please correct me?
我对上述项目的理解是否正确?如果没有,请你纠正我?
Thanks In Advance -
提前致谢 -
1 个解决方案
#1
1
You got some things wrong.
你有些不对劲。
ad 1.:
广告1:
There is a require-dev
section that will also be installed unless you prevent this by using the --no-dev
flag when installing or updating. Also note that the package name is not necessarily connected to something published on Packagist, you can also have a section repositories
that can point to alternative sources.
除非您在安装或更新时使用--no-dev标志来防止这种情况,否则还会安装require-dev部分。另请注意,程序包名称不一定与Packagist上发布的内容相关联,您还可以使用可指向备用源的部分存储库。
ad 2.:
广告2:
The creation of any files beyond the vendor/autoload.php
is internal to Composer, and you shouldn't mess with it. Composer will read the definitions of all autoload
and autoload-dev
(unless --no-dev
is given) sections and compile and appropriate autoloader from it. This INCLUDES the main software that has all the require
and require-dev
for other packages.
在vendor / autoload.php之外创建任何文件都是Composer内部的,你不应该搞乱它。 Composer将读取所有autoload和autoload-dev的定义(除非给出--no-dev)部分,并从中编译和适当的自动加载器。这包括对其他软件包具有所有require和require-dev的主要软件。
That compilation will behave differently depending on the type of autoload that has been defined: PSR-4, PSR-0, classmap or files.
根据已定义的自动加载类型,该编译的行为会有所不同:PSR-4,PSR-0,类映射或文件。
ad 3.:
广告3:
If a package does not have a autoload
section, any code cannot be autoloaded. Assuming that the author of a package did everything right, one can safely assume that autoloading will take place correctly, depending on the type of autoloading (PSR-4, PSR-0, classmap or files).
如果程序包没有自动加载部分,则无法自动加载任何代码。假设程序包的作者一切正确,可以安全地假设自动加载将正确进行,具体取决于自动加载的类型(PSR-4,PSR-0,类图或文件)。
A package can contain multiple entries for autoloading, and can be responsible for more than one namespace. The entries in autoload_namespaces.php
are mainly irrelevant to understand Composer autoloading in general: A package defines some autoloading, a software requires that package, includes vendor/autoload.php
, and the developer knows from the documentation that there is a class WhateverNamespace\Foo
and simply uses it: $class = new WhateverNamespace\Foo()
or use WhateverNamespace\Foo; $class = new Foo()
.
包可以包含多个用于自动加载的条目,并且可以负责多个名称空间。 autoload_namespaces.php中的条目通常与理解Composer自动加载无关:一个软件包定义了一些自动加载,软件需要该软件包,包括vendor / autoload.php,开发人员从文档中知道有一个类WhateverNamespace \ Foo并简单地使用它:$ class = new WhateverNamespace \ Foo()或使用WhateverNamespace \ Foo; $ class = new Foo()。
Don't assume any connection from the package name to the namespace, or from the namespace to the folder name. Everything can be named differently (although it is confusing, which rarely is a good idea) when it comes to Composer, there are no enforced rules that say a package named GreatVendor/Awesome
has to use the namespace GreatVendor\Awesome
or any folder layout named like that. Using classmaps, any class can be hosted in any file, more that one class can be hosted in a single file, and code can still be included using include/require(_once)
.
不要假设从包名称到命名空间,或从命名空间到文件夹名称的任何连接。在Composer中,所有内容都可以有不同的名称(尽管它很容易混淆,这很少是一个好主意),没有强制规则说明名为GreatVendor / Awesome的包必须使用名称空间GreatVendor \ Awesome或任何命名的文件夹布局像那样。使用类映射,任何类都可以托管在任何文件中,一个类可以托管在一个文件中,并且仍然可以使用include / require(_once)包含代码。
The general case would be that any recent package will stick to PSR-4 or at least PSR-0 and use namespaces. But note that the origin of this comes from the code itself, not from Composer. Composer will only execute the defined autoload definition and act accordingly, it will not magically interfere with changing namespaces or something. It still is basic PHP, with the added benefit of a common interface that is telling a generic autoloader how to find a class if it is needed.
一般情况是任何最近的包将坚持PSR-4或至少PSR-0并使用名称空间。但请注意,它的起源来自代码本身,而不是来自Composer。 Composer将只执行定义的自动加载定义并相应地执行,它不会神奇地干扰更改名称空间或其他内容。它仍然是基本的PHP,具有通用接口的额外好处,它告诉通用自动加载器如何在需要时查找类。
ad 4.:
广告4:
There are no "custom namespaces". What should "custom" mean here? The autoload
section must match the code that is written, you cannot change the namespaces in the code to different ones by using Composer.
没有“自定义命名空间”。 “习惯”应该是什么意思?自动加载部分必须与编写的代码匹配,您不能使用Composer将代码中的命名空间更改为不同的命名空间。
autoload_namespaces.php
is not the only source of autoload definitions.
autoload_namespaces.php不是自动加载定义的唯一来源。
Composer will handle duplicate namespace entries well. It must fail if there are duplicate classes defines, but as long as there is only an overlap on the namespace prefix (like GreatVendor
being used by the two packages GreatVendor\Cache
and GreatVendor\Db
), Composer will tell them apart.
Composer将很好地处理重复的命名空间条目。如果有重复的类定义,它必须失败,但只要命名空间前缀只有重叠(如两个包GreatVendor \ Cache和GreatVendor \ Db使用的GreatVendor),Composer就会将它们区分开来。
#1
1
You got some things wrong.
你有些不对劲。
ad 1.:
广告1:
There is a require-dev
section that will also be installed unless you prevent this by using the --no-dev
flag when installing or updating. Also note that the package name is not necessarily connected to something published on Packagist, you can also have a section repositories
that can point to alternative sources.
除非您在安装或更新时使用--no-dev标志来防止这种情况,否则还会安装require-dev部分。另请注意,程序包名称不一定与Packagist上发布的内容相关联,您还可以使用可指向备用源的部分存储库。
ad 2.:
广告2:
The creation of any files beyond the vendor/autoload.php
is internal to Composer, and you shouldn't mess with it. Composer will read the definitions of all autoload
and autoload-dev
(unless --no-dev
is given) sections and compile and appropriate autoloader from it. This INCLUDES the main software that has all the require
and require-dev
for other packages.
在vendor / autoload.php之外创建任何文件都是Composer内部的,你不应该搞乱它。 Composer将读取所有autoload和autoload-dev的定义(除非给出--no-dev)部分,并从中编译和适当的自动加载器。这包括对其他软件包具有所有require和require-dev的主要软件。
That compilation will behave differently depending on the type of autoload that has been defined: PSR-4, PSR-0, classmap or files.
根据已定义的自动加载类型,该编译的行为会有所不同:PSR-4,PSR-0,类映射或文件。
ad 3.:
广告3:
If a package does not have a autoload
section, any code cannot be autoloaded. Assuming that the author of a package did everything right, one can safely assume that autoloading will take place correctly, depending on the type of autoloading (PSR-4, PSR-0, classmap or files).
如果程序包没有自动加载部分,则无法自动加载任何代码。假设程序包的作者一切正确,可以安全地假设自动加载将正确进行,具体取决于自动加载的类型(PSR-4,PSR-0,类图或文件)。
A package can contain multiple entries for autoloading, and can be responsible for more than one namespace. The entries in autoload_namespaces.php
are mainly irrelevant to understand Composer autoloading in general: A package defines some autoloading, a software requires that package, includes vendor/autoload.php
, and the developer knows from the documentation that there is a class WhateverNamespace\Foo
and simply uses it: $class = new WhateverNamespace\Foo()
or use WhateverNamespace\Foo; $class = new Foo()
.
包可以包含多个用于自动加载的条目,并且可以负责多个名称空间。 autoload_namespaces.php中的条目通常与理解Composer自动加载无关:一个软件包定义了一些自动加载,软件需要该软件包,包括vendor / autoload.php,开发人员从文档中知道有一个类WhateverNamespace \ Foo并简单地使用它:$ class = new WhateverNamespace \ Foo()或使用WhateverNamespace \ Foo; $ class = new Foo()。
Don't assume any connection from the package name to the namespace, or from the namespace to the folder name. Everything can be named differently (although it is confusing, which rarely is a good idea) when it comes to Composer, there are no enforced rules that say a package named GreatVendor/Awesome
has to use the namespace GreatVendor\Awesome
or any folder layout named like that. Using classmaps, any class can be hosted in any file, more that one class can be hosted in a single file, and code can still be included using include/require(_once)
.
不要假设从包名称到命名空间,或从命名空间到文件夹名称的任何连接。在Composer中,所有内容都可以有不同的名称(尽管它很容易混淆,这很少是一个好主意),没有强制规则说明名为GreatVendor / Awesome的包必须使用名称空间GreatVendor \ Awesome或任何命名的文件夹布局像那样。使用类映射,任何类都可以托管在任何文件中,一个类可以托管在一个文件中,并且仍然可以使用include / require(_once)包含代码。
The general case would be that any recent package will stick to PSR-4 or at least PSR-0 and use namespaces. But note that the origin of this comes from the code itself, not from Composer. Composer will only execute the defined autoload definition and act accordingly, it will not magically interfere with changing namespaces or something. It still is basic PHP, with the added benefit of a common interface that is telling a generic autoloader how to find a class if it is needed.
一般情况是任何最近的包将坚持PSR-4或至少PSR-0并使用名称空间。但请注意,它的起源来自代码本身,而不是来自Composer。 Composer将只执行定义的自动加载定义并相应地执行,它不会神奇地干扰更改名称空间或其他内容。它仍然是基本的PHP,具有通用接口的额外好处,它告诉通用自动加载器如何在需要时查找类。
ad 4.:
广告4:
There are no "custom namespaces". What should "custom" mean here? The autoload
section must match the code that is written, you cannot change the namespaces in the code to different ones by using Composer.
没有“自定义命名空间”。 “习惯”应该是什么意思?自动加载部分必须与编写的代码匹配,您不能使用Composer将代码中的命名空间更改为不同的命名空间。
autoload_namespaces.php
is not the only source of autoload definitions.
autoload_namespaces.php不是自动加载定义的唯一来源。
Composer will handle duplicate namespace entries well. It must fail if there are duplicate classes defines, but as long as there is only an overlap on the namespace prefix (like GreatVendor
being used by the two packages GreatVendor\Cache
and GreatVendor\Db
), Composer will tell them apart.
Composer将很好地处理重复的命名空间条目。如果有重复的类定义,它必须失败,但只要命名空间前缀只有重叠(如两个包GreatVendor \ Cache和GreatVendor \ Db使用的GreatVendor),Composer就会将它们区分开来。