Hook system

Hi everyone,

just another “design question”: imagine I want to develop the DEFINITIVE module for users (it’s an example, don’t be anxious)… so I develop it all but in profile page I want other modules (that depends on definitive-user-module) contribute portlets to the page… and here is the good part… WITHOUT touching the code of the original user module

the only solution I have thought is by configuration: in user module set in an array property the list of templates of contributions to display on profile page (but this is "modifying" the original user module)

what about using events? the thing is I cannot see how to design this kind of "hook dependency system"… example




USER <----- BLOGs (contribute headers for last entries to profile page)

        \-- COMMENT (last comments)

        \-- ...



I don’t know if I’m clear…

Greetings

Hwangar

Sort of subcribing to this thread, as I’ll be facing the same issue soon as well. As I am familiar with Drupal as well, I know exactly what you mean. I have also looked into it earlier real briefly, but I am not sure Yii’s event system is suitable. As far as I could tell that is mostly a one-way communication, whereas the hook we are looking for requires a two way system. Personally, I am flirting with the idea of using interfaces that modules can implement. In pseudo-code, that would look roughly like this.




class definitive-user-module

{

    public function actionProfile()

    {

        foreach Yii::app()->getModules()

        {

            if module is instance of IUserPofile interface

                ... do something ...

        }

    }

}



Then in each module that needs to supply profile stuff:




class MyModule implements IUserProfile

{

    function userProfile()

    {

        return 'some value';

    }

}



Note that this is untested. I am not sure yet how this would work with validating multiple models that might be used in the profile page. The downside that I also see is that each module would get instantiated, regardless of whether or it is needed. Perhaps a more refined hook component could handle an array config where you supply the "other modules to be checked" for each module.

hey jodev…

I was looking at the code of yii and modules (the descriptor class of the module) seems to be already loaded and configured from config file, so your idea seems to be viable… you propose using OOP and declaring an Interface as (if you know java) Eclipse architecture does to communicate with the module… seems a bit embarrasing but could work

I’m going to try that and let’s see…

Thank you, man!!

Hwangar

Well, after some tests I have something that seems to work:

  • Hook module: declares an interface IHook (just the method getMessage()), the module descriptor and a simple Controller

  • Client modules: declaring descriptors that implements IHook (returning a simple script)

Now the interesting part:

from the controller I want every contributing module (implementing IHook) and collect the messages:




public function actionIndex()

 {

      echo "<div><h1>DEFAULT Controller de Hwangar Module</h1></div>";

      foreach (Yii::app()->getModules() as $id=>$module) {

        $type=$module['class'];

	if(!class_exists($type,false))

  	  $type=Yii::import($type,true);


        $class=new ReflectionClass($type);

        if ($class->implementsInterface('IHook')) {

          $instance = Yii::app()->getModule($id);

          echo "<div>".$id."--";

          print_r($instance->getMessage());

          echo "</div>";

        }

      }  

}    




as you can see, I loop over module descriptors and instantiate only the implementors of IHook, printing the messages…

what do you think, could this evolve to something interesting? I came accross this project failed, but I did not investigate it: http://code.google.com/p/yii-modules/

greetings…

Hwangar