Andy
Andy

Reputation: 5395

Loading a Composer package not listed in vendor/composer/autoload_namespaces.php

I have an application written in CakePHP 3. I've installed Cake and other packages via Composer.

The most recent one I installed was fineuploader/php-traditional-server (https://packagist.org/packages/fineuploader/php-traditional-server) by placing this in my composer.json then running composer update:

 "require": {
    "fineuploader/php-traditional-server": "1.1.0"
 }

This has downloaded the files as expected into vendor/fineuploader/php-traditional-server.

However, if I try to reference that in one of my Cake Controllers, like this:

use UploadHandler;

public function test()
{
    var_dump(new \UploadHandler);
}

It errors saying Class 'UploadHandler' not found

The only way I've been able to get the above to run is by hard-coding a link to the appropriate file. So instead of use UploadHandler; I have

require_once(ROOT . DS . 'vendor/fineuploader/php-traditional-server/handler.php'); 

I read on https://jtreminio.com/2012/10/composer-namespaces-in-5-minutes/ that vendor/composer/autoload_namespaces.php is supposed to contain the namespaces that can be autoloaded. But when I open that file up, there's no reference to this package (in fact there are only about 3 whereas my vendor directory has loads of packages some of which I can use just fine).

So what is the correct way to load UploadHandler? I thought the point of using Composer was that it took care of autoloading these files, so you didn't have to use things like require()?

Upvotes: 1

Views: 536

Answers (1)

ndm
ndm

Reputation: 60463

Have you read the note in the handler.php file?

Do not use or reference this directly from your client-side code. Instead, this should be required via the endpoint.php or endpoint-cors.php file(s)

So AFAICT the package doesn't define any autoloading capabilites because you're not supposed to use the UploadHandler class directly, but instead you should require the endpoint.php or endpoint-cors.php file, which do not include any classes, and should only be required when needed.

Surely these endpoint files are not compatible with CakePHP, so you may have a reason to use the handler class directly. The package however doesn't use namespaces, and the classname doesn't match the filename, ie it follows neither PSR-0 nor PSR-4 conventions, and as already mentioned, the packages composer.json doesn't define any autoloading configuration, so no matter what conventions the package would follow, this would result in no autoloading capabilities for the packages PHP classes/files.

So if you want autoloading capability, you could fix this on your end by for example adding a classmap autoload entry to your projects composer.json, like:

{
    "autoload": {
        "classmap": [
            "vendor/fineuploader/php-traditional-server/"
        ]
    }
}

Then re-dump the autoloader, and autoloading should work fine:

$ composer dump-autoload

See also

Upvotes: 2

Related Questions