mudrd8mz / moodle-tool_pluginskel

Generator of Moodle plugins skeletons
https://moodle.org/plugins/tool_pluginskel
Other
51 stars 46 forks source link

Generate multiple files from the same template #13

Closed alexandru-elisei closed 8 years ago

alexandru-elisei commented 8 years ago

Given the recipe:

observers:
    - eventname: core\event\something_happened
      callback: "\local_test\event_observer::something_happened"
      includefile: "/path/to/file/relative/to/moodle/dir/root"
      priority: 200
    - eventname: core\event\something_else_happened
      callback: "\local_test\another_event_observer::something_else_happened"

the db/events.php file will be generated from the db_events.mustache template. But I also need to generate two class files: event_observer.php (which contains the class event_observer with the static function something_happened()) and another_event_observer.php (which contains the class another_event_observer with the static function something_else_happened()).

It is reasonable to expect that the two class files are generated from the same template.

You are passing the entire recipe to each file generator class.

How can I generate two different files from the same template and using the same recipe?

alexandru-elisei commented 8 years ago

This problem came up before in my version of the plugin and the agreed upon solution was to make it possible for files to be generated from different recipes.

The core issue here is that we want to generate different files from the same template using the same recipe.

mudrd8mz commented 8 years ago

I'll look at this after the weekend. Good point.

alexandru-elisei commented 8 years ago

I have been thinking about two possible solutions, one involves creating a new skel class, something like php_multiple_files, which will keep a list of the files that need to generated and their recipies, and the other solution involves creating a function like add_observer_file_skeletons(). This in turn would call the function add_file_skeleton_with_recipe() for every file and the recipe that generates it.

I'll try to implement the idea that fits best tomorrow.

mudrd8mz commented 8 years ago

Your implementation based on explicit recipe is very close to what I was thinking about, too. During the development of the draft, I was playing with a possibility to explicitly override some parts of the recipe. The explicit recipe in your case will always start as a copy of the original recipe (which you call the "global" one) with some parts of it overridden. So it looked reasonable to me to to use something like array_merge_recursive() to do the job.

I admit the results were not always what the caller might intent, as sometimes it may be adding a new item to the list, and sometimes it may be replacing the whole list with a new one.

So after all, I agree that your proposal is better.

alexandru-elisei commented 8 years ago

Closing the issue, solved by various commits (#21, #24).