Zend Framework 2使用composer自动加载第三方库 [英] Zend Framework 2 Autoload Third Party library using composer

查看:678
本文介绍了Zend Framework 2使用composer自动加载第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用composer自动加载第三方库到我的ZF2应用程序 - 特别是Google API。
我按照这篇文章编辑我的composer.json

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

并运行更新。
我可以看到composer / autoload_namespaces.php中的条目

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

在尝试在该目录(Google_Client.php)中实例化一个类时,仍会得到一个致命错误类。



任何想法我缺少什么?
我包含类中的文件,我试图使用它:

 使用Googleanalytics \Google_Client; 

我已经尝试重命名目录,以防万一 - 是问题,还创建了一个简单的test.php文件在该目录下的情况下,类名中的下划线(Google_Client.php)是问题,但仍然是一样的错误。



还有什么我需要添加到我的ZF2应用程序来自动加载这个库?
另外请注意,我决定不使用ZendGdata,因为这个组件似乎不再维护了。
提前感谢

解决方案

软件的自动加载定义不应包括任何供应商模块的自动加载定义。将其移动到用于包含软件的包定义。



并在其他新闻:如果它不与PSR-0一起使用,classmap自动加载器应该照顾

如何为不提供composer.json的存储库创建包。



更新



<



本质上,您只需要几个信息:


  1. 版本号及其在该储存库中的位置。

  2. 您尝试使用的软件名称 - 您可能只想添加供应商名称

  3. 知道如何自动加载软件包,即知道哪个路径用于软件,并将类映射自动加载器应用到该软件包。

  4. 至少包含以下之一:


    1. 托管代码的存储库的URL




$ b php-client,a)存储库的URL是 http://google-api-php-client.googlecode.com/svn/ ,b)最新的版本号为 0.6.7 ,该软件包的A)下载URL为 http://google-api-php-client.googlecode.com /files/google-api-php-client-0.6.7.ta​​r.gz



现在您将其填入此模板:

 repositories:[
{
type:package $ bpackage:{
name:name from(2),
version:version from(1),
dist:{
url:URL from(4/2),
type:tar或zip根据下载
},
source:{
url:URL from(4/1),
type:svn,
reference:tags / version from(1)
}
autoload:{
classmap:[path from(3)]
}
}
}
]

然后你可以在你的要求中要求精确的包:require:{name from(2):version from(1)}



对于您正在使用的google包, 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.ta​​r.gz,
type:tar
},
source:{
url:http://google-api-php-client.googlecode.com/svn/,
type:svn,
reference:tags / 0.6.7
},
autoload:{
classmap:[src /]
}
} b $ b}
]

添加这个主要是样板的东西的好处是,现在免费下载软件。您不必关心如何手动下载,解压缩和安装软件包。您已将此软件的自动加载信息添加到您自己的composer.json中,但它包含在您要使用的软件的软件包定义中,但不包含在您自己软件的自动加载区域中。



您也不必担心Composer会不小心移除您手动下载的套件。


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

解决方案

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.

这篇关于Zend Framework 2使用composer自动加载第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆