CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Including external libraries in an extension
Pages: [1]

Author Topic: Including external libraries in an extension  (Read 1289 times)

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Including external libraries in an extension
March 10, 2013, 01:18:01 pm
I am building an extension that uses external libraries that are not in the CiviCRM distribution. The 'standard' way of approaching this would be to:
- create a 'packages' directory in my extension directory, and put the libraries there,
- have a require_once('library/entrypoint.php'); statement in the extension code.

However the include_path statements in _myextension_civix_civicrm_config does not provision for such extensions directory, forcing me to have workarounds in the extension code. I would therefore propose to make the following changes in the civix templates:

in template for function _myextension_civix_civicrm_config, replace:
  $include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
  set_include_path( $include_path );

with:
  $include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
  if (is_dir($extRoot . 'packages')) {
    $include_path = $extRoot . 'packages' . PATH_SEPARATOR . $include_path;
  }
  set_include_path( $include_path );
cividesk -- CiviCRM delivered ... your way!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Including external libraries in an extension
March 11, 2013, 06:08:07 am
Hi,

Have you tried to put the library into package/thelib and require_once ("package/thelib/thethingyouwant").

The root of extension being in the path, should work.

I have done zero test, but I imagine having a gazillion folders on the include path doesn't help perf, not need to double that number ;)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Including external libraries in an extension
March 11, 2013, 07:54:40 am

If you dont have too many packages, you can always install it in the root of your extension, and then use:

require_once 'thelib/main.file.to.include.php';

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Including external libraries in an extension
March 11, 2013, 11:18:28 pm
Thanks all, the solutions you outlined is what I referred to as 'workarounds', and what I have implemented so far.

I was proposing a change to civix that would allow extensions to be closer to core civiCRM in terms of programming conventions, ie.:
- put all external libraries in a 'packages' folder
- have a require_once 'thelib/entrypoint.php'; in the extension code

Lobo - I do not know the performance impact of multiplying include directories, but we are already adding one directory per active extension in the chain. What I am proposing is to add a second one, but only if the extension uses external libraries. Seems reasonable.

PS. maybe the Drupal folks have some data on multiplying directories in the include_path - worth a search in their forums!
cividesk -- CiviCRM delivered ... your way!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Including external libraries in an extension
March 12, 2013, 04:16:27 am
Hi,

Not sure I understand why you call it workaround. What's the issue of putting the path of the library relative to the root of the extension instead of to the package extension?

The only reason I can think of not doing the same in the core is that PEAR tend to want to be in the include path, so the package had to be added. So having the package folder in the include path is the workaround, not the other way around ;)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Including external libraries in an extension
March 12, 2013, 08:48:31 am

1. I did not comment on length of include path and efficiency, in general in my 2 cents: compute power is cheap while programming power is not. So simple solutions are good :)

2. drupal has a folder called libraries where all 3rd party libraries are supposed to go. I think the CiviCRM starter kit is doing that (we unbundle a few big libraries from our distro) for that

the main issue IMO is:

a. The CMS and CiviCRM can use common libraries.

b. So far PHP is not great at handling multiple copies / versions of libraries (or i dont know how to do that). Our solutions so far have been very patchy

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Including external libraries in an extension
March 12, 2013, 10:47:25 am
Points well taken, let's forgo the proposed civix change. Thanks for your input.
cividesk -- CiviCRM delivered ... your way!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Including external libraries in an extension

This forum was archived on 2017-11-26.