Zend Framework 2 Autoload Third Party library using composer

别来无恙 提交于 2019-12-01 14:30:19

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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!