如何在TYPO3 Extbase Extension中包含或自动加载外部库? +依赖注入? [英] How do I include or autoload external libraries in a TYPO3 Extbase Extension? + Dependecy Injection?

查看:89
本文介绍了如何在TYPO3 Extbase Extension中包含或自动加载外部库? +依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Extbase 1.4开发TYPO3 4.6扩展,并且试图包含一个外部库.在我的案例中,该库 facebook PHP SDK 位于$_EXTKEY/Resources/PHP/facebook-php-sdk/facebook.php下.我希望该库在需要时自动加载并自动注入(依赖注入).

I'm developing a TYPO3 4.6 Extension with Extbase 1.4 and im trying to include an external library. The library, in my case the facebook PHP SDK, is under $_EXTKEY/Resources/PHP/facebook-php-sdk/facebook.php. I would like the library to autoload and automatically inject (Dependecy Injection) where I need it.

我在网上发现的一些评论表明,其中应包含带有require_once()的库:

Some comments I found online suggest that one should include libraries with require_once():

http://forge.typo3.org/issues/33142

  1. 如果它只是一个很小的帮助程序库,它打算存储在{PackageRoot}/Resources/PHP/{libraryName}中,并且仅通过require包含在内.这个问题是否值得怀疑?/li>
  2. 如果FLOW3软件包根本上主要代表了前一个库,例如Imagine或Swift包中的情况,则该库代码将直接放在{PackageRoot}/Classes下."
  1. if it's just a tiny helper library, it's intended to be stored in {PackageRoot}/Resources/PHP/{libraryName} and just included via require. is this suspected by the problem however?
  2. if the FLOW3 package mainly represents the foreing library at all, like it's the case in Imagine or Swift package, the library code is put below {PackageRoot}/Classes directly."

http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2011-July/009946.html

我将在特定操作中包括该类(使用require_once)来处理此问题.这样,您就可以访问这些函数,并且该类成为您的库."

"I would include the class (using require_once) from within a specific action to handle this. That way you have access over those functions and the class becomes your library."

我尝试了这个,它的工作原理是这样的:

I tried this and it works like this:

<?php
require_once( t3lib_extMgm::extPath('extkey') . 'Resources/PHP/facebook-php-sdk/facebook.php');

class Tx_WsLogin_Domain_Repository_FacebookUserRepository extends Tx_WsLogin_Domain_Repository_UserRepository {

protected $facebook;

public function __construct() {
    $this->setFacebook(new Facebook(array(
        'appId' =>'',
        'secret' => '')
    ));
    parent::__construct();
}

public function setFacebook(Facebook $facebook) {
    $this->facebook = $facebook;
}


public function sampleFunction() {
    $userId = $this->facebook->getUser();
}

}
?>

但是如何获取它以使用injectFacebook函数自动加载并自动注入库?

But how can I get it to autoload and automatically inject the library with the injectFacebook function?

类似于 @alex_schnitzler

Like @alex_schnitzler and @sorenmalling mentioned about autoloading:

@PeterTheOne将所有文件放入ext_autoload.php中,然后使用DI或对象管理器.

@PeterTheOne Put all the files inside ext_autoload.php and then use DI or the object manager.

@PeterTheOne将类定义放入扩展中的ext_autoload.php中?

@PeterTheOne put the class definition into ext_autoload.php in your extension?

我这样尝试过(文件:ext_autoload.php):

I tried it like this (file: ext_autoload.php):

<?php

$extPath = t3lib_extMgm::extPath('extKey');

return array(
    'facebook' => $extPath . 'Resources/PHP/facebook-php-sdk/facebook.php',
);

?>

似乎找到并包含了正确的文件.但是当我尝试使用用户依赖注入时(例如

It seems to find and include the right file. But when I try to user Dependency Injection (like peter answered) I get an error:

未传递正确的构造函数依赖关系信息数组!

在第247行的文件/var/syscp/webs/web1/dev/typo3_src-4.5.15/typo3/sysext/extbase/Classes/Object/Container/Container.php中引发了InvalidArgumentException.

InvalidArgumentException thrown in file /var/syscp/webs/web1/dev/typo3_src-4.5.15/typo3/sysext/extbase/Classes/Object/Container/Container.php in line 247.

我认为这是因为Facebook类的构造函数具有必需的$ config参数.

I think this is because the constructor of the Facebook class has a required $config argument.

我按照彼得的回答,并在 @alex_schnitzler @ sorenmalling ,他把我指向了ObjectManager,我的FacebookService现在看起来像这样:

I did what peter said in his answer and with the help of @alex_schnitzler and @sorenmalling, who pointed me to the ObjectManager, my FacebookService looks like this now:

class Tx_Extkey_Service_FacebookService implements t3lib_Singleton {

/**
* @var Tx_Extbase_Object_ObjectManagerInterface
*/
protected $objectManager;

/**
 * Facebook from @link https://github.com/facebook/facebook-php-sdk facebook-php-sdk
 *
 * @var Facebook
 */
protected $facebook;

/**
* @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
*/
public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
    $this->objectManager = $objectManager;
}

/**
 * 
 */
public function initializeObject() {
    $this->facebook = $this->objectManager->create(
        'Facebook',
        array(
            'appId' =>'input appId here',
            'secret' => 'input app secret here'
        )
    );
}

/**
 * @return Facebook
 */
public function getFacebook() {
    return $this->facebook;
}

}

有关更多帮助,请阅读: http://forge. typo3.org/projects/typo3v4-mvc/wiki/Dependency_Injection_(DI)有关 initializeObject()通过对象管理器创建原型对象的部分>

For more help read: http://forge.typo3.org/projects/typo3v4-mvc/wiki/Dependency_Injection_(DI) the parts about initializeObject() and Creating Prototype Objects through the Object Manager

推荐答案

Extbase注入非常简单. 这是实际的实现.使用外部库,不是.

Extbase injection is pretty simple. Here's the actual implementation. Using external libraries, however, is not.

确定了如何加载库后,是否尝试过注入库?像这样:

Once you figure out how to load the library, have you tried just injecting it? Like so:

/**
 * @var Facebook
 */
protected $facebook;

/**
 * inject the facebook
 *
 * @param Facebook facebook
 * @return void
 */
public function injectFacebook(Facebook $facebook) {
    $this->facebook = $facebook;
}

注意::添加注释后,您需要在注释中使用@param,还需要清除配置缓存.

NOTE: You need the @param in the comment and you also need to clear your configuration cache after adding this code.

我不了解Facebook SDK API,但希望您可以使用默认构造函数实例化Facebook对象,然后稍后使用setter方法添加参数.您可能想要创建一个FacebookService类(单例),该类加载Facebook PHP并设置基本参数.然后,您可以在需要时注入FacebookService以获取实际的Facebook对象.

I don't know about the Facebook SDK API, but hopefully you can instantiate the Facebook object with the default constructor and then add the arguments later with setter methods. You might want to create a FacebookService class (singleton) that loads the Facebook PHP and sets the essential arguments. Then you can inject a FacebookService to get the actual Facebook object whenever you need it.

这篇关于如何在TYPO3 Extbase Extension中包含或自动加载外部库? +依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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