XF 2.1 Still can't figure out how to get composer working...

Jaxel

Well-known member
I've never used composer before... In my shell I ran the following commands:
Code:
cd src/addons/EWR/Backup
composer require aws/aws-sdk-php
This downloaded everything to the /vendor folder in my addon. I then added the following line to my addon.json:
Code:
"composer_autoload": "vendor\composer"

However, running the following code:
Code:
<?php

namespace EWR\Backup\Repository;

use XF\Mvc\Entity\AbstractCollection;
use XF\Mvc\Entity\Repository;
use Aws\S3;

class Amazon extends Repository
{
    public $s3log;
    
    public function runBackupS3($subpath, $subdir)
    {
        ...
    
        $client = new S3\S3Client([
            'version'         => 'latest',
            'region'          => $meta['region'],
            'credentials'    => [
                'key'            => $meta['key'],
                'secret'        => $options->EWRbackup_s3_secret,
            ]
        ]);
        
        ...
    }
}

Returns an error:
Error: Class 'Aws\S3\S3Client' not found in src/addons/EWR/Backup/Repository/Amazon.php at line 24

What am I missing?
 
Only thing I can think of is that you would need to do something which rebuilds the active add-on cache, as that's where the composer autoload cache is built.

Easiest way to do that is to "Sync changes" which can be done from the Admin CP add-on list or using the CLI command.
 
"composer_autoload": "vendor\composer"
That line may also not be doing what you expect, though I'm not sure how JSON would interpret the backslash. You likely just want a forward slash (as it's a directory separator).
 
Okay, before I read that, I changed "composer_autoload" to an array, hoping that was the issue. But now I'm getting a fatal error on my dev site:
An exception occurred: [ErrorException] [E_NOTICE] Array to string conversion in src/XF/App.php on line 2167

  1. XF::handlePhpError() in src/XF/App.php at line 2167
  2. XF\App->setupAddOnComposerAutoload() in src/XF/App.php at line 1742
  3. XF\App->setup() in src/XF/Pub/App.php at line 89
  4. XF\Pub\App->setup() in src/XF.php at line 365
  5. XF::setupApp() in src/XF.php at line 388
  6. XF::runApp() in index.php at line 20
Where can I fix this in the database?
 
Disable listeners in config.php:
PHP:
$config['enableListeners'] = false;
Then fix the addon.json file and sync the changes.

Finally, remove or comment out the above line from config.php.
 
Okay, to actually USE the third-party API in my addon, do I have to "use" the entire directory?

IE: use EWR\Backup\vendor\aws\aws_sdk_php\src\S3

That is a bit cumbersome. I tried use Aws\S3 (which is how it's called in the API), but thats giving me the error:
Error: Class 'EWR\Backup\Repository\Transfer' not found in src/addons/EWR/Backup/Repository/Amazon.php at line 36
 
The AWS S3 classes should be autoloaded as per the instructions they provide. The use statement and code example you provided previously does seem to be correct.

But that particular error does not appear to be related to using that. It seems to be referencing some other code error.
 
You right. I had to do "S3\Transfer" and not just "Transfer". Amazon re-arranged a few things.
 
Back
Top Bottom