Composer not auto-loading package
Asked Answered
T

2

7

I'm currently working on Laravel 4. I have added the following to my composer.json, and ran the update:

    "require": {
        ...
        "koraktor/steam-condenser": "*"

The package: https://packagist.org/packages/koraktor/steam-condenser

The issue I'm having is that if I call one of the classes it uses, for example:

$steamUser = new SteamId('000000000000000000');
echo "Welcome, " . $steamUser->getNickname() . "<br />";

I get the error Class 'SteamId' not found

If I manually require the file needed, then it works perfectly:

require_once('/home/path-to-laravel/laravel/vendor/koraktor/steam-condenser/lib/steam-condenser.php');

I've ran composer dump-autoload and still doesn't work. Does anyone know why this is? It's really frustrating me :(

Termite answered 19/6, 2013 at 15:18 Comment(0)
G
11

Steam Condenser is not (yet) compliant to PSR-0, so you have to use a different autoloading approach (see http://getcomposer.org/doc/04-schema.md#autoload).

Using the files method should be best suited here:

{
    "autoload": {
        "files": ["vendor/koraktor/steam-condenser/lib/steam-condenser.php"]
    }
}
Gifford answered 19/6, 2013 at 15:55 Comment(3)
Koraktor it's working! Thanks very much for taking the time to look into it.Termite
Actually, classmap might be a better solution. files simply forces the file to be loaded, even if you do not use the classes in there. classmap on the other hand enables the autoloader to find the classes and only load them on demand.Adigun
Hm, looking at it it seems like files is indeed the right choice. The file you're loading not just loads the classes but contains some data needed by them as well.Adigun
I
1

Just requiring a package, does not force composer to autoload the package.

Have a look into autoloading with composer, but something like these should get you started:

autoload: {
    "classmap": ["vendor/koraktor/steam-condenso/lib"]
}
Ingrate answered 19/6, 2013 at 15:25 Comment(3)
Thanks (there is a typo in that though). I'm confused though, other packages I've been using (such as HybridAuth) literally worked out of the box, didn't have to add anything, but with this I did? It does load the files. However I get the error "Use of undefined constant STEAM_CONDENSER_PATH - assumed 'STEAM_CONDENSER_PATH' - I guess that's because it isn't loading a file which is defining that.Termite
Have a look into the code and see where this define happens and figure out if it is getting loaded.Ingrate
Happens in the "steam-condenser.php" file: define('STEAM_CONDENSER_PATH', dirname(FILE) . '/'); So I assume not... but it is within the /lib/ directory which is odd!Termite

© 2022 - 2024 — McMap. All rights reserved.