Consider two scenarios:
In the first the library is loaded from Github, composer.json looks like this:
{
"require": {
"revenuewire/ach-file": "*"
}
}
composer libraries are downloaded with this command:
php composer update
PHP code:
<?php
require '../composer/vendor/autoload.php';
$achFileHeaderRecord = new \RW\ACH\FileHeaderRecord([
// Required
\RW\ACH\FileHeaderRecord::IMMEDIATE_DESTINATION => ' 123456789',
\RW\ACH\FileHeaderRecord::IMMEDIATE_ORIGIN => '1234567890',
\RW\ACH\FileHeaderRecord::DESTINATION_NAME => 'DESTNAME',
\RW\ACH\FileHeaderRecord::ORIGIN_NAME => 'Orig Name',
// Optional
\RW\ACH\FileHeaderRecord::FILE_DATE => new DateTime(),
\RW\ACH\FileHeaderRecord::FILE_ID_MODIFIER => 'A',
\RW\ACH\FileHeaderRecord::REFERENCE_CODE => '',
]);
?>
This code will run successfully.
But when I try to load the exactly same library from Gitlab, composer.json looks like this:
{
"repositories": [
{
"type": "package",
"package": {
"name": "mygitlab/ach-file",
"version": "1.2",
"type": "package",
"source": {
"url": "https://mygitlab.ca/mygitlab/ach-file.git",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"mygitlab/ach-file": "*"
}
}
And now run the same code I get this error:
PHP Fatal error: Uncaught Error: Class 'RW\ACH\FileHeaderRecord' not found
This is caused because the library is not autoloaded. It can be solved by adding this line, to load it manually:
require '../composer/vendor/aptpayapi/ach-file/bootstrap.php';
So in the case it is loaded as external package it does not read/execute the library's composer.json
{
"name": "revenuewire/ach-file",
"type": "library",
"description": "Create and read ACH Payment files",
"homepage": "https://github.com/revenuewire/ach-payment-file",
"license": "Apache-2.0",
"authors": [
{
"name": "Matthew Casiro",
"email": "mattcasiro@gmail.com"
}
],
"autoload": {
"psr-4": {
"RW\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"RW\\Tests\\": "tests/"
}
},
"config": {
"platform": {
"php": "7"
}
},
"require": {
"ext-ssh2": "*",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
Is it possible to force autoload of externally loaded packages?