Forums / Developer / HOW TO register the autoload of an embeded Zend like component in an Extension ?

HOW TO register the autoload of an embeded Zend like component in an Extension ?

Author Message

Sylvain Gogel

Tuesday 10 November 2009 12:12:38 pm

Hi there,

I'm building an extension that will expose a bunch of template operators and modules that user a small component lib i wrote. This component, called Ewm is built using the Zend Coding standard.

I want to embed this lib in an extension and use the lib autoloader to avoid require/includes in operators and module functions

Can't find a way to include the Autoloader code somewhere. Who an i acheave this?

extension
  - ewmadapter
    - autoloads
    - design
    - lib
       - Ewm
    - modules
    - settings
    - translations

the autoloader looks like this (in Ewm/Autoloader.php) :

class Ewm_Autoloader
{
  static public function register()
  {
    ini_set('unserialize_callback_func', 'spl_autoload_call');
    spl_autoload_register(array(new self, 'autoload'));
  }

  static public function autoload($class)
  {
    if (0 !== strpos($class, 'Ewm'))
    {
      return false;
    }

    require dirname(__FILE__).'/../'.str_replace('_', '/', $class).'.php';

    return true;
  }
}

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Damien Pobel

Wednesday 11 November 2009 12:27:43 pm

Hi Sylvain,

the easiest way is probably to let the autoloader of eZ Publish do the job. Active your extension and regenerate the autoload array with the following command line :

php bin/php/ezpgenerateautoloads.php

so your class will be automatically included when needed without having to define a specific Autoloader class.

If your component uses one or more Zend components, you'll also probably need to alter the include_path as there are sometimes explicit include statements in Zend code or include of PHP files not containing a class definition. You can do that in your own code before using your component or in config.php file in the eZ Publish root. I use the former in autostatus extension when using Zend_Service_Twitter component.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

André R.

Thursday 12 November 2009 4:46:50 am

Or you can set it up in config.php, have a look in config.php-RECOMMENDED file, it includes an example in the bottom.

This means the users of your extensions would need to set this up as part of the extension installation procedure, but should be simple if you provide examples in your installation doc.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Sylvain Gogel

Thursday 12 November 2009 11:23:04 am

I will give a try the the ezpgenerateautoloads.php, did not figured out it will autoload stuff in a extension/xxx/lib folder

does it works for 4.0 ?

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Damien Pobel

Thursday 12 November 2009 1:04:18 pm

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Sylvain Gogel

Friday 13 November 2009 12:15:54 pm

"

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

"

Works like a charm with ezpgenerateautoload.php !! some ezp Awesomeness ^_^

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Olav Frengstad

Monday 20 December 2010 10:19:12 am

"

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

"

This means:

// Append your directory to the include path
set_include_path( get_include_path() . PATH_SEPARATOR . 'extensions/xyz/library/');

// Just create the new object it, or do a spl_autoload_call( $className );
// to look for the class before you try to initiate it.
$object = new $className();

eZ debug

Timing: Jan 18 2025 10:31:47
Script start
Timing: Jan 18 2025 10:31:47
Module start 'content'
Timing: Jan 18 2025 10:31:48
Module end 'content'
Timing: Jan 18 2025 10:31:48
Script end

Main resources:

Total runtime0.9005 sec
Peak memory usage4,096.0000 KB
Database Queries214

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0068 588.0938180.7813
Module start 'content' 0.00680.7468 768.8750694.6250
Module end 'content' 0.75360.1467 1,463.5000345.0234
Script end 0.9003  1,808.5234 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.4937210.0002
Check MTime0.00160.1796210.0001
Mysql Total
Database connection0.00070.081610.0007
Mysqli_queries0.795188.30092140.0037
Looping result0.00260.28702120.0000
Template Total0.863995.920.4319
Template load0.00320.359420.0016
Template processing0.860695.573620.4303
Template load and register function0.00010.009710.0001
states
state_id_array0.00170.191410.0017
state_identifier_array0.00270.304320.0014
Override
Cache load0.00260.2897520.0001
Sytem overhead
Fetch class attribute can translate value0.00130.147450.0003
Fetch class attribute name0.00160.1786100.0002
XML
Image XML parsing0.00180.200750.0004
class_abstraction
Instantiating content class attribute0.00000.0030130.0000
General
dbfile0.00690.7717400.0002
String conversion0.00000.001130.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.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
6content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
4content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
2content/datatype/view/ezxmltags/quote.tpldatatype/ezxmltext/quote.tplextension/ezwebin/design/ezwebin/override/templates/datatype/ezxmltext/quote.tplEdit templateOverride template
1content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 42
 Number of unique templates used: 10

Time used to render debug report: 0.0002 secs