Need help with 3rd party class in Container

I’m trying to use a library to merge PDFs, but I cannot figure out how I’m declaring it twice. Getting a this error: Cannot declare class PDFMerger, because the name is already in use …

I’ve tried lots of variations to get it to work. I’ve also tested the PDF library using the command line and it works perfectly outside of Nextcloud.

**Possible causes of the issue. **

  • PDFMerger class name. The conversion of some names to camelcase makes me think this could be a problem.
  • 3rd party classes are handled differently that isn’t shown in the docs.
  • I’m re-declaring something in the controller that belongs in the container.
  • I’m a terrible programmer.

One of these things is the cause of my issue. Any help is really appreciated.

Code: https://github.com/PaperlessHouse/nextcloud-faxapp

Library code: https://github.com/myokyawhtun/PDFMerger

I put the library at /lib/Vendor/PDFMerger/PDFMerger

Code in /lib/AppInfo/Application.php

// Import class
use OCA\FaxApp\Vendor\PDFMerger\PDFMerger;
// Controller with query to PDFMerger object
$container->registerService('FaxController', function($c){
    return new FaxController(
        $c->query('Logger'),
        $c->query('AppName'),
        $c->query('Request'),
        $c->query('FaxService'),
        $c->query('PDFMerger'),
        $c->query('UserSession')->getUser()->getUID(),
        $c->query('Config')
    );
});
// Register PDFMerger    
$container->registerService('PDFMerger', function($c){
    return new PDFMerger(
    );
});

In the Controller, I’ve got this code.

class FaxController extends Controller {
    // I'm not sure if this is necessary, but based my code on the OCR app 
    /** @var PDFMerger */
    private $PDFMerger;

    /**
     * @param ILogger $logger
     * @param string $AppName
     * @param IRequest $request
     * @param FaxService $faxService
     * @param PDFMerger $pdfMerger
     * @param string $UserId
     */
    public function __construct(ILogger $logger, $AppName, IRequest $request, $faxService, $pdfMerger, $UserId, IConfig $config){
    parent::__construct($AppName, $request);
        $this->logger = $logger;
        $this->appName = $AppName;
        $this->faxService = $faxService;
        $this->pdfMerger = $pdfMerger;
        $this->userId = $UserId;
        $this->config = $config;

This is how I do it: https://github.com/nextcloud/news/blob/master/lib/AppInfo/Application.php#L113

Then let it auto inject everything via type hints https://github.com/nextcloud/news/blob/master/lib/Service/FeedService.php#L51

BTW, the issue is most likely a PHP issue because there can only be one class, see https://stackoverflow.com/questions/38169097/fatal-error-cannot-declare-class

So what the error means is that Nextcloud or whatever app was enabled already imported the PDFMerger class so loading it from your vendor directory fails.

Typical PHP issue that makes working in PHP such a pain.

The only solution for such an issue would be to only specifiy your required libs in a composer.json file, then let Nextcloud gather all composer.json files and install global copies in a global thirdparty directory, because libraries are basically global.