简体   繁体   中英

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. 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:

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:

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

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)

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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