Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer packages, autoloading non-class based files

Tags:

When I was digging into the source of a Composer package on github I noticed that there were php files that matched namespace names but were preceded with an underscore. Puzzled I pulled the package down (via Composer) and noticed that the class loader that Composer generates required these underscored files explicitly, not autoloading as I'd expected.

For instance, in the crunch/regular-expression package there is a namespace called Crunch\RegularExpression:

-- src ---- Crunch ------- RegularExpression       <-- folder containing classes ------- _RegularExpression.php  <-- file namespace to Crunch/RegularExpression                                     containing functions and constants                                      (instead of a class) 

Initially I thought these underscored files were a feature of PSR-0 that I had missed, but then I looked at the Composer generated autoload_real.php and saw that _RegularExpression.php (amongst others) was being required explicitly:

… $loader->register(true);  require $baseDir . '/src/Crunch/_RegularExpression.php'; require $baseDir . '/src/Crunch/RegularExpression/_Modifier.php'; require $baseDir . '/src/Crunch/RegularExpression/Pattern/_Modifier.php'; require $baseDir . '/src/Crunch/RegularExpression/Pattern/_Assertion.php';  return $loader; … 

Haven't been able to find any meaningful documentation about this feature of Composer. Is it a good "standard" for exporting non-class based namespaced dependencies, like functions and constants?

Update

My question turned out to be a slight misnomer. The selected answer lead me to discover that non-class based assets can be explicitly declared for loading in composer.json:

"autoload": {     "psr-0": { "Crunch\\RegularExpression": "src" },     "files": [         "src/Crunch/_RegularExpression.php",         "src/Crunch/RegularExpression/_Modifier.php",         "src/Crunch/RegularExpression/Pattern/_Modifier.php",         "src/Crunch/RegularExpression/Pattern/_Assertion.php"     ] } 

The underscores on the files were a convention used to delineate them from class definitions and have no special purposes in autoloading.

like image 908
Mark Fox Avatar asked Apr 29 '13 07:04

Mark Fox


People also ask

What is autoloading in Composer?

Autoloading: The classmap Directive You just need to provide a list of directories, and Composer will scan all the files in those directories. For each file, Composer will make a list of classes that are contained in that file, and whenever one of those classes is needed, Composer will autoload the corresponding file.

How do I get the Composer json file?

To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.

What is Composer minimum stability?

An alternative is to set your minimum-stability to dev, but tell composer you want to use stable whenever possible: "minimum-stability": "dev", "prefer-stable" : true. This basically means it will always use stable UNLESS there is no way to install a stable dependency, and therefore use dev.


1 Answers

Composer doesn't treat those files in any special way. The package author in this case used this as some sort of convention to stores functions it seems.

The files are required by Composer because they're defined as "files" autoload in the composer.json, not because of some black magic on filenames.

like image 177
Seldaek Avatar answered Jun 12 '23 18:06

Seldaek