Zend Framework 2 Autoload Third Party library usin

2019-02-21 05:30发布

问题:

I am trying to use composer to autoload a third party library into my ZF2 application - specifically Google api. I followed the answer in this post on SO, edited my composer.json

"autoload": {
    "psr-0": {"Googleanalytics\\": "vendor/google-api-php-client/src/"}
}

and ran update. I can see the entry in composer/autoload_namespaces.php

'Googleanalytics\\' => array($vendorDir . '/google-api-php-client/src'),

but i still get a fatal error class not found when trying to instantiate a class in that directory (Google_Client.php).

Any ideas what i am missing? I am including the file in the class i am trying to use it:

use Googleanalytics\Google_Client;

I have tried renaming the directory in case the - was the problem and also creating a simple test.php file in that dir in case the underscore in the class name (Google_Client.php) was the problem, but still the same error.

Is there anything else i need to add to my ZF2 application to autoload this library? Also note i decided not to use ZendGdata as this component does not seem to be maintained anymore. Thanks in advance

回答1:

The autoload definition of your software should not include the autoload definition of any vendor module. Move that to the package definition you use to include the software.

And in other news: If it does not work with PSR-0, the classmap autoloader should take care of it.

Update

How to create the package for a repository not offering a composer.json

Essentially you'd need only a couple of pieces of information:

  1. The version number and where it's located in that repository.
  2. A name of the software you are trying to use - you'd probably only want to add a vendor name and not be too creative with the module.
  3. Know how to autoload the package, i.e. know which path is used for the software and apply the classmap autoloader to it.
  4. At least one of the following, preferredly both:
    1. The URL of the repository that hosts the code
    2. The URL of a download of a published version

In case of the "google-api-php-client", the a) URL of the repository is http://google-api-php-client.googlecode.com/svn/, the b) most current version number is 0.6.7, the A) download URL of that package is http://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.7.tar.gz.

And now you fill it into this "template":

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "name from (2)",
            "version": "version from (1)",
            "dist": {
                "url": "URL from (4/2)",
                "type": "tar or zip according to download"
            },
            "source": {
                "url": "URL from (4/1)",
                "type": "svn",
                "reference": "tags/version from (1)"
            },
            "autoload": {
                "classmap": ["path from (3)"]
            }
        }
    }
]

And then you can require that exact package in your requirements: "require": { "name from (2)": "version from (1)" }

For the google package you are using this would essentially get you to use this:

"require": {
    "google/google-api-php-client":"*"
},
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "google/google-api-php-client",
            "version": "0.6.7",
            "dist": {
                "url": "http://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.7.tar.gz",
                "type": "tar"
            },
            "source": {
                "url": "http://google-api-php-client.googlecode.com/svn/",
                "type": "svn",
                "reference": "tags/0.6.7"
            },
            "autoload": {
                "classmap": ["src/"]
            }
        }
    }
]

The benefit of adding this mostly boilerplate stuff is that you get the downloading of the software for free now. You don't have to care about how to manually download, unpack and install the package. You did add the autoloading information for this software to your own composer.json, but it is contained in the package definition of the software you want to use, it is not contained in the autoloading area of your own software.

You also do not have to worry about Composer removing your manually downloaded package accidentally.



回答2:

For anyone else looking to add a third party library to ZF2 using composer, here are the steps that worked for me.

  1. Copy third party library to vendor folder
  2. Add following line to composer.json

    "autoload": { "classmap": ["vendor/PATH TO LIBRARY"] }

  3. Run php composer.phar update

Then you should see all the classes that were in the 3rd party library in the file in the composer folder: composer/autoload_classmap.php

When instantiating any class from the library in your zf2 application, dont forget to prefix the class name with a \. For example:

$client = new \Google_Client();