While the existing "faces" abstraction layer in Rules failed as concept, it still gives us a lot of flexibility in Rules: we can easily add support for implement conditions and actions as classes (what I felt would have not been welcomed by the community when I wrote Rules 2.x back in 2009).

But now, classes are cool - so let's add support for them!?

Comments

fago’s picture

Status: Active » Needs review
StatusFileSize
new23.72 KB

So attached patch adds in support for class-based event,action,condition definition - including a simple discovery mechanism based on the already existing possibility to expose additional files to Rules via hook_rules_file_info().

So a class must reside in module.rules.inc, in a file specified in hook_rules_file_info() or in any other included location and support auto-loading, e.g. via the registry. Then, the class must implement the suiting interface as well as a static getInfo() method providing the plugin info. Compared to info provided via info-hooks getInfo() must include the plugin 'name' and the providing 'module'.

Attached patch implements that and converts node conditions to classes to have an example.

fago’s picture

StatusFileSize
new11.78 KB

ops, previous patch contained other patches as well - please ignore.

See attached patch.

fago’s picture

Title: Support implement conditions and actions via classes » Support implementing conditions and actions via classes
fubhy’s picture

+++ b/includes/rules.core.incundefined
@@ -1933,6 +1938,29 @@ interface RulesPluginImplInterface {
+ *

weird line break.

+++ b/rules.moduleundefined
@@ -217,12 +222,40 @@ function rules_fetch_data($hook) {
+ */
+function rules_discover_plugins($class) {
+  // Make sure all files possibly holding plugins are included.
+  RulesAbstractPlugin::includeFiles();
+
+  $items = array();
+  foreach (get_declared_classes() as $plugin_class) {
+    if (is_subclass_of($plugin_class, $class) && method_exists($plugin_class, 'getInfo')) {
+      $info = call_user_func(array($plugin_class, 'getInfo'));
+      $items[$info['name']] = $info;
+    }
+  }
+  return $items;
+}
+

Let's try solving this with ReflectionClass::getFileName() and then doing a while() loop using dirname() to find the module through a keyed list of module names (keys being the paths to the modules).

Status: Needs review » Needs work

The last submitted patch, d7_rules_classes.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
StatusFileSize
new628 bytes
new11.82 KB

Realized I forgot a rather crucial line -> upated patch.

Not addressing #4 yet - the suggestion for determining the module sounds good though!

Status: Needs review » Needs work

The last submitted patch, d7_rules_classes.interdiff.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
StatusFileSize
new3.15 KB
new12.78 KB

Noticed I forgot to move over the node-type-assertion callbacks - yes, callbacks are easy to loose. Hopefully this fixes tests.

Status: Needs review » Needs work

The last submitted patch, d7_rules_classes.interdiff.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
StatusFileSize
new10.38 KB
new20.22 KB

Good, so this optimises things a bit more, auto-detects the module key, adds hook_rules_directory() and documentation.

fago’s picture

Issue tags: +fluxkraft

tagging

fubhy’s picture

Very good. Discovery looks much better now. Maybe add a test for that?

fago’s picture

Issue tags: +Needs tests
StatusFileSize
new20.51 KB
new2.03 KB

It's already indirectly tested with conditions, but yes we should add an explicit test case for it as well.

Updated patch with improved hook_rules_directory() which now allows modules to expose directories for other modules.

fago’s picture

Issue tags: -Needs tests
StatusFileSize
new2.05 KB
new22.43 KB

Added tests.

fago’s picture

Status: Needs review » Fixed

Committed.

Automatically closed -- issue fixed for 2 weeks with no activity.