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) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • 5.0 Saloon »
  • Replace hooks & include_path overload with event dispatcher pattern
Pages: [1]

Author Topic: Replace hooks & include_path overload with event dispatcher pattern  (Read 705 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Replace hooks & include_path overload with event dispatcher pattern
March 12, 2014, 04:39:33 am
While we are trying to put more GoF into civi, what about two patterns (ab)used in the extensions that could be revisited

- hooks -> EventDispatcher
D8 discussion https://drupal.org/node/1509164 

- each extension into the include_path
this is used to be able to add new hooks (so event dispatcher could replace)
to replace stuff in the core (so DI instead)
and to add new apis (same)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Replace hooks & include_path overload with event dispatcher pattern
March 12, 2014, 04:08:10 pm
+1 on include_path -- would prefer a standard autoloading mechanism supported by extensions. composer-based extension management is one solution (with pros/cons -- more standard/popular and includes dependency management, but mostly used for CLI, and mostly engineered for managing code instead of DBs).

I've done some experiments with exposing Doctrine entities with RESTful interface, e.g.

https://github.com/totten/civicrm-core/blob/doctrine-hateoas/Civi/Core/Worldregion.php#L47

It works with Doctrine entities, but we haven't examined how to define Doctrine entities in extensions. (The class-loader is likely to be one part; the DB upgrades is likely to be another part.)

FWIW, this ( https://github.com/totten/civicrm-core/commit/80e6dbd4e196c0959cbe4c9f99edc99359805ea7 ) uses a technique that I think is neat -- ie scanning (for classes with annotations) and storing an index (in a cache). It's a compelling pattern for handling resource registrations.

--

Regarding hooks and Event Dispatcher, I have somewhat mixed feelings. Event Dispatcher is more flexible, and I suspect it's slightly more recognizable for WP/Joomla developers, and I'd personally choose it when writing a new application. But Drupal developers are a big constituency for Civi, and Drupal-style hooks probably have the broadest recognition in our community. It doesn't need to be either/or (one can simultaneously support both hook- and EventDispatcher-interfaces for the same events), but I have mixed feelings about supporting multiple-ways-to-do-the-same-thing. If we supported ED, wouldn't we want to deprecate hooks?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Replace hooks & include_path overload with event dispatcher pattern
March 13, 2014, 03:49:24 am
include path is used several ways that we should aim at replacing

new stuff (eg. new page or new api) -> autoloading
overwrite of existing core code-> DI
magic -> ?

I like the idea of putting the right function/object in the right place and it works (eg. hooks or apis). However, it's a pita to debug when it's not called (cache issue?, path issue?, typo?,  other? all of the above?...). Don't know to what extend registering explicitly a listener/subscriber or adding explicitly an api is that of a burden compared to the gained clarity/easier to debug

I think D8 will have both events and hooks. If civi5 forces to rewrite all hooks (eg because Doctrine forces to change the data format), probably better to change as well to event listeners. If not, no strong opinion...
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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: Replace hooks & include_path overload with event dispatcher pattern
March 24, 2014, 07:57:05 pm
I like having some 'conventions' so you know where to find stuff.

Not only is this an important factor in orienting new developers, but also facilitates collaboration as when looking at a piece of code you did not write you are in 'known territory' and can immediately understand how it works (well, most of the time anyway!).

That said, the include_path is a PITA and most certainly leads to reduced performance.

So maybe a middle-ground solution would be that the extension manager does 'scan' the extension code via a standard pattern and creates the registrations automatically (ie. scan the <ext-root>/api for any files, use the file name to create a registration, no need to add to include_path anymore). This might be done during the extension installation process and cached somewhere.

This might be a big plus as well in terms of security: when installing a new extension, we could have a page that lists all it implements based on this scan/cache - ie. defines new apis (with the list of), replaces core files, changes database schema, adds menu items, etc.

Just brainstorming and it's getting late ...
cividesk -- CiviCRM delivered ... your way!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • 5.0 Saloon »
  • Replace hooks & include_path overload with event dispatcher pattern

This forum was archived on 2017-11-26.