简体   繁体   English

使用Drupal Libraries模块加载PHP库并使用类

[英]Loading PHP Libraries and Using Classes using Drupal Libraries module

I'm trying to wrap my head around using the Drupal Libraries module in order to load PHPGoogleMaps library. 为了加载PHPGoogleMaps库,我试图使用Drupal库模块来解决问题 I've worked my way to creating the configuration and loading the library into my module. 我已经设法创建配置并将库加载到模块中。 I guess my main problem gets to using the libraries classes now. 我想我的主要问题是现在使用库类。 I'm sure there is a different way I need to call or use them now. 我敢肯定,现在我需要使用另一种方式打电话或使用它们。 My previous non drupal code was something to the effect of: 我以前的非drupal代码具有以下效果:

require( 'resources/PHPGoogleMaps/Core/Autoloader.php' );
$map_loader = new SplClassLoader('PHPGoogleMaps', 'resources/');
$map_loader->register();
$map = new \PHPGoogleMaps\Map;
$address = '...';
$geocode = \PHPGoogleMaps\Service\Geocoder::geocode($address);

Now in Drupal using the Libraries API I'm not to sure where to start once the library is loaded as I'm still running into class not found errors when trying the code below: 现在在Drupal中使用Libraries API,我不确定在加载库后从哪里开始,因为在尝试以下代码时我仍然遇到类未找到错误:

libraries_load('PHPGoogleMaps');
$map = new \PHPGoogleMaps\Map;
//Fatal error: Class 'PHPGoogleMaps\Map' not found

I should note that the library is correctly loaded and pulls the correct info when checking using libraries_detect() and printing out the returned libraries_load info 我应该注意,在使用libraries_detect()进行检查并打印出返回的libraries_load信息时,该库已正确加载并提取了正确的信息。

In my library definition I'm not sure if I need to add more than just the autoloader file here 在我的库定义中,我不确定是否需要在此处添加除自动加载器文件以外的其他内容

$libraries['PHPGoogleMaps'] = array(
    'name' => 'PHPGoogleMaps', 
    'vendor url' => 'https://github.com/galen/PHPGoogleMaps', 
    'download url' => 'https://github.com/galen/PHPGoogleMaps', 
    'version arguments' => array(
      'file' => 'version.txt',
      'pattern' => '/Version (\d+)/', 
      'lines' => 1,
    ), 
    'files' => array(
        'php' => array(
            'Core/Autoloader.php',
        ),
    ),
);

Ah, for anyone trying to figure this out I still need the SplClassLoader information (I'm still pretty new to using this) 啊,对于任何想弄清楚这一点的人,我仍然需要SplClassLoader信息(我仍然很陌生)

$library = libraries_load('PHPGoogleMaps');
$map_loader = new SplClassLoader('PHPGoogleMaps','sites/all/libraries');
$map_loader->register();

I temporarily put the manual path in, though I'm sure it will be better practice to parse this from the $library['library path'] url 我暂时输入了手动路径,尽管我相信最好从$ library ['library path'] url解析此路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM