Blogs / Marko Zmak / loading external libraries in extensions

loading external libraries in extensions

Wednesday 06 October 2010 4:38:01 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

By : Marko Žmak

Often you need to include external 3rd party libraries or your own libraries in the extensions (like for example a SOAP library). The problem comes when you have to include the same library in several extensions.

Here is how to do it without having to copy the same library in every extension directory.

The Problem

When using non official eZ extensions or those developed on your own, there's often a need to include other 3rd party libraries (for example a SOAP library) or some of your own libraries. The most simple way is to copy the library to your extension directory and include it in your PHP sources.

But the problem comes when you need to use the same library in several extensions. If you copy the library to the all extension directories then you have to update all the copies when a new version of a library comes, or when you make changes to some of your libraries. Also you could have problems with your include() directives if you change the folder in which your extension resides.

The Solution

Fortunately since eZ 4 there's an autoload mechanism that allows you to include your libraries without having to include() them in your PHP scripts. You just generate the autoload file and include it in you PHP script. This is how you do it...

1. In the extension folder create a folder where you will put all the needed libraries. For example, named "external_libraries". Note, that you will not enable it as an extension, you just have to put it in the extension folder so that it will be parsed by the autoload generation script.

2. In all your PHP scripts include the autoload.php:

require_once('autoload.php');

3. Regenerate the autoload arrays from shell:

php bin/php/ezpgenerateautoloads.php

or by clicking the "Regenerate autoloads" button in the "Extensions" section of admin.

Optionally you can generate autoload array only for extensions:

php bin/php/ezpgenerateautoloads.php --extension

Note: All this only works for libraries in which you use PHP classes, objects and methods. It doesn't work if you want to call a normal PHP function from a library.

For advanced users

If you are an advanced user, you can even include libraries that are outside of your eZP installation. It goes like this:

1. Build the autoload array for the external folder

php bin/php/ezpgenerateautoloads.php --target=autoload /path/to/external/folder/library_name

This creates a file named "library_name_autoload.php" in the autload folder of your eZ installation

2. Create a PHP script with a custom autoload class and method, that will load the autload array from "library_name_autoload.php". For example see the ezpAutoloader class in autoload.php script of your eZP installation. The script should register the created autoload class and method at the end:

spl_autoload_register( array( 'myAutoloadClas', 'myAutoloadMethod' ) );

3. Include your autoload script in your PHP scripts.

I didn't test this advanced usage, but it should work. If anyone tries, post your feedback here.

eZ debug

Timing: Jan 17 2025 21:15:08
Script start
Timing: Jan 17 2025 21:15:08
Module start 'content'
Timing: Jan 17 2025 21:15:08
Module end 'content'
Timing: Jan 17 2025 21:15:08
Script end

Main resources:

Total runtime0.1761 sec
Peak memory usage6,144.0000 KB
Database Queries185

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0048 596.1797180.8203
Module start 'content' 0.00480.0750 777.0000441.9688
Module end 'content' 0.07980.0962 1,218.9688408.8516
Script end 0.1760  1,627.8203 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00351.9730210.0002
Check MTime0.00130.7608210.0001
Mysql Total
Database connection0.00080.443510.0008
Mysqli_queries0.103458.72961850.0006
Looping result0.00150.83701830.0000
Template Total0.151385.920.0756
Template load0.00211.170720.0010
Template processing0.149284.733320.0746
Template load and register function0.00010.043210.0001
states
state_id_array0.00070.418610.0007
state_identifier_array0.00070.399920.0004
Override
Cache load0.00181.0005310.0001
Sytem overhead
Fetch class attribute name0.00150.871230.0005
Fetch class attribute can translate value0.00040.213610.0004
class_abstraction
Instantiating content class attribute0.00000.003430.0000
XML
Image XML parsing0.00020.114710.0002
General
dbfile0.00271.5579220.0001
String conversion0.00000.001830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplblog_entry/full.tplextension/community_design/design/suncana/override/templates/blog_entry/full.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
3content/datatype/view/ezxmltags/header.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/header.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
1content/datatype/view/ezkeyword.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezkeyword.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 23
 Number of unique templates used: 8

Time used to render debug report: 0.0002 secs