tmuras/moosh — pull request #4 by brki (2013-06-08 16:30:21+00:00)
Title: Namespaced commands

I noticed that some commands need to be written differently for different versions. For 2.5 and 2.6, for example, the API calls used by CategoryCreate and CohortEnrol have changed. I'm sure that there will be other Moodle API changes in the future, too.

I thought about how to handle this, and came up with the idea of having the commands namespaced (e.g. Moosh\Command\Moodle23\Cohort\CohortEnrol and Moosh\Command\Moodle25\Cohort\CohortEnrol ).

The function moosh_load_all_commands() is used to find the appropriate version to use. If run in a Moodle directory, and the Moodle version is, say 2.6, it will pick the latest available command up to the version 2.6. For example, if I run it in the master branch (2.6), it looks for all commands, starting with those in the Moodle23 directory and continuing up to the Moodle26 directory (if it exists). Later entries overwrite earlier entries. This results in it having the command that is most likely to work for the version of Moodle it is running against. If my explanation is hard to understand, just read the moosh_load_all_commands() function, hopefully it is clear there :).

I'm quite happy with how it turned out. The Moodle25 versions of the commands inherit from the Moodle23 commands as the logic is only slightly different, and just contain a few lines of code.

While going through all the commands, I also made some database-related adjustments. $DB->sql_like() is used instead of raw '... LIKE ...' to make it more portable. And I adjusted the SqlDump command so that it works with Postgresql databases, too.

I know it's a huge changeset, but there's not really much that changed besides making the commands namespaced.