Reputation: 483
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.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.autoload_namespaces.php
file is created within the default vendor
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 systemautoload_namespaces.php's
namespaces IF EACH DOWNLOADED FILE CONTAINS A NAMESPACE DEFINITION THAT MATCHES ONE OF autoload_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.autoload_namespaces.php
file AND ARE NOT AFFECTED IN ANY WAY BY THE "Autoloader" SECTION of composer.json
, ASSUMING THAT A NAMESPACE PRESENT WITHIN THE autoload_namespaces.php
FILE IS NOT DUPLICATED IN THE composer.json
"Autoloader" SECTION.Is my understanding about the above items correct? If not, would you please correct me?
Thanks In Advance -
Upvotes: 2
Views: 1617
Reputation: 70863
You got some things wrong.
ad 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.
ad 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.
That compilation will behave differently depending on the type of autoload that has been defined: PSR-4, PSR-0, classmap or files.
ad 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).
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()
.
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)
.
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.
ad 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.
autoload_namespaces.php
is not the only source of autoload definitions.
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.
Upvotes: 2