Forum


Replies: 3   Views: 1592
Ns version, private respository: class not found errors
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Mikevg  · 20-04-2021 - 17:49

I've followed the instructions for creating a private repository for the PhpDocx Namespaced version, and integrated it with my lumen framework project, and it is being correctly loaded into the /vendor/<companyname>/phpdocx folder, however when I try to run my code I get a "Class 'phpdocx\Create\CreateDocx' not found" error.

I assume this has something to do with the namespacing / autoloading not setup correctly? I've tried just about everything I can think of as far as the autoload setting goes.

Currently I have this in the PhpDocx repositories composer.json:

"autoload": {
  "psr-4": {
    "phpdocx\\": "phpdocx/Classes/Phpdocx"
  }
},

I've also tried:

"autoload": {
  "psr-4": {
    "phpdocx\\": "<companyname>/phpdocx/Classes/Phpdocx"
  }
},

and both variations without the /Phpdocx at the end, but none of them work. My IDE has no problem resolving these class names, it's just at runtime that it fails.

What do I need to do to get this working?

Thanks.

Posted by admin  · 21-04-2021 - 06:29

Hello,

We think the problem is you are not using the correct namespace, from the error you get:

class 'phpdocx\Create\CreateDocx' not found

Please note that the internal path is Classes/Phpdocx/Create (Phpdocx, not phpdocx; this is with the first character uppercase).

If you are using a repository (such as a private repository) to install phpdocx, composer will download and install phpdocx and then it will autoload the classes automatically,

Adding the classmap/psr-4 settings manually to composer.json is only needed when you are copying the files without using a repository.

On Integrate phpdocx with Composer projects you can read the steps to integrate phpdocx with composer copying the files of the library manually.

If you copy the files manually instead of using a repository that is loaded from Composer, the recommended way to autoload phpdocx using Composer is with the classmap option:

"autoload": {
    "classmap": [
        "vendor/phpdocx/Classes/Phpdocx"
    ]
}

not psr-4, although some users integrate it with psr-4: https://www.phpdocx.com/en/forum/default/topic/2050 too:

"psr-4": {
    "Phpdocx\\": "vendor/phpdocx/Classes/"
}

After you have added the library path to Composer you need to update the Composer autoloader (again, only if yu have copied the files manually):

$ composer dump-autoload

Also please check you are using the correct namespaces when you instantiate the classes in your code.

Integrating phpdocx with Composers and any PHP namespaces project is fully tested, so we are sure the problem comes from a not correct namespace when the class are being used.

Regards.

Posted by Mikevg  · 28-04-2021 - 21:09

Thank you. I still seem to be having issues with this.

This is the composer.json in the project that I am trying to include Phpdocx into:

"require": {
        "<company>/phpdocx": "^11.0"
    },

    "repositories": [
        {
            "type": "vcs",
            "url": "git@github.com:<company>/phpdocx.git"
        }
    ],

And this is the composer.json in the private repo I've setup for Phpdocx.

{
        "name": "<company>/phpdocx",
        "description": "A private library for PhpDocx",
            "autoload": {
                "psr-4": {
                        "Phpdocx\\": "Classes/Phpdocx"
                }
        },
        "minimum-stability": "dev",
        "prefer-stable": true
}

Without this autoload section, I get "class not found" errors when trying to instantiate a Phpdocx instance.

This is the code where I'm calling Phpdocx

use Phpdocx\Create\CreateDocx;

$docx = new CreateDocx();
$pdfName = self:: getPdfName($inputFileName);
$docx->transformDocument($inputFileName, $pdfName);

Everything seems to be working, but when I run `composer update` I get the lots of warnings similar to:

Class Phpdocx\Libs\TCPDI located in C:/xampp/htdocs/<project>/vendor/<company>/phpdocx/Classes/Phpdocx\Libs\TCPDF_lib.php does not comply with psr-4 autoloading standard. Skipping.

What do I need to do to get rid of these warnings?

Posted by admin  · 29-04-2021 - 05:19

Hello,

phpdocx includes two external libraries that doesn't complain psr-4. As explained in our previous reply, the recommended way to include phpdocx with Composer is using classmap:

"autoload": {
    "classmap": [
        "vendor/phpdocx/Classes/Phpdocx"
    ]
}

not psr-4 as you are using. Using classmap to autoload phpdocx classes with Composer is fully tested and working, please use classmap not psr-4.

On https://getcomposer.org/doc/04-schema.md#classmap you can read about using Composer with classmap.

Regards.