2

I'm trying to include the PHPInsight library for performing sentiment analysis on user data.

Based on the demo file provided in it, I added it into the sites/all/libraries path so that it would read sites/all/libraries/phpInsight and tried to create an object

$sentiment = new \PHPInsight\Sentiment();

which resulted in blank screen.

Following this I looked into

  1. Include a third party library using namespaces, dependencies and
  2. https://www.drupal.org/node/1976234.

Apparently owing to a known constraint of D7 with namespace installed the xautoload and modified the hook_libraries_info as below

function analyzer_libraries_info() {
 $libraries['phpInsight'] = array(
  'name' => 'PHP Insight',
  'vendor url' => 'https://github.com/JWHennessey/phpInsight',
  'download url' => 'https://github.com/JWHennessey/phpInsight/archive/master.zip',
  'files' => array(
    'php' => array('autoload.php'),
    ),
  'xautoload' => function($adapter) {
    $adapter->namespaceRoot('\PHPInsight', 'lib');
  },
  );
 return $libraries;
}

I'm still uncertain about how to use the xautoload and my autoload file that needs to be included is in sites/all/libraries/phpInsight/autoload.php. Since I haven't used libraries API or PHP namespaces and some clarity on what needs to be done to get it included and instantiate the object with how it happens would be of great help.

  • Surely $adapter->namespaceRoot('\PHPInsight', 'lib'); would be $adapter->namespaceRoot('\PHPInsight', 'sites/all/libraries/phpInsight'); or similar? 'lib' would probably only make sense if the library code was at analyzer/lib/PHPInsight. You might want to change the library folder to be case sensitive too to match the library name (PHPInsight), that usually makes a difference with PSR-0 autoloaders IIRC – Clive May 28 '15 at 14:28
  • Actually maybe it should be $adapter->namespaceRoot('\PHPInsight', 'PHPInsight');, with the folder renamed to match the case – Clive May 28 '15 at 14:31
  • Can you specify the exact path to the file that defines the class? – donquixote May 28 '15 at 20:51
  • No, according to the documentation link you posted, I think it should be $adapter->namespaceRoot('PHPInsight', 'lib'); So without the backslash in the beginning. – donquixote May 28 '15 at 20:55
  • You can also try $adapter->add('PHPInsight', 'lib');, which is the Composer syntax for PSR-0, I think to remember. – donquixote May 28 '15 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.