From 5476db1ee78545c58899a7598a50fd8312cf2334 Mon Sep 17 00:00:00 2001
From: Marco Villegas <git@marvil07.net>
Date: Tue, 4 Feb 2014 20:26:56 -0500
Subject: [PATCH] Initial patch

---
 includes/VersioncontrolRepository.php              |    1 +
 ...ntrolSynchronizationEventProcessorException.php |    7 ++
 includes/interfaces.inc                            |   62 +++++++++++++++++
 .../VersioncontrolEventProcessorNone.inc           |   45 ++++++++++++
 includes/plugins/event_processor/none.inc          |   25 +++++++
 versioncontrol.admin.inc                           |   13 +++-
 versioncontrol.module                              |   73 ++++++++++++++------
 7 files changed, 205 insertions(+), 21 deletions(-)
 create mode 100644 includes/VersioncontrolSynchronizationEventProcessorException.php
 create mode 100644 includes/plugins/event_processor/VersioncontrolEventProcessorNone.inc
 create mode 100644 includes/plugins/event_processor/none.inc

diff --git a/includes/VersioncontrolRepository.php b/includes/VersioncontrolRepository.php
index dbe1db2..352301e 100644
--- a/includes/VersioncontrolRepository.php
+++ b/includes/VersioncontrolRepository.php
@@ -96,6 +96,7 @@ abstract class VersioncontrolRepository implements VersioncontrolEntityInterface
    * - repomgr
    * - auth_handler
    * - reposync
+   * - event_processor
    *
    * @var array
    */
diff --git a/includes/VersioncontrolSynchronizationEventProcessorException.php b/includes/VersioncontrolSynchronizationEventProcessorException.php
new file mode 100644
index 0000000..0811b3e
--- /dev/null
+++ b/includes/VersioncontrolSynchronizationEventProcessorException.php
@@ -0,0 +1,7 @@
+<?php
+/**
+ * @file
+ *   An exception to be thrown when an error occurs during event procesing
+ *   after repository synchronization.
+ */
+class VersioncontrolSynchronizationEventProcessorException extends Exception { }
diff --git a/includes/interfaces.inc b/includes/interfaces.inc
index 41b5c81..91c4886 100644
--- a/includes/interfaces.inc
+++ b/includes/interfaces.inc
@@ -468,3 +468,65 @@ interface VersioncontrolRepositoryHistorySynchronizerInterface {
    */
   public function verifyData();
 }
+
+/**
+ * Defines a versioncontrol synchronization event processor plugin behavior.
+ *
+ * Note: This is similar to hook_versioncontrol_code_arrival(), but this type
+ * of plugins are intended to help different configuration per repository per
+ * plugin.
+ */
+interface VersioncontrolSynchronizationEventProcessorInterface {
+  /**
+   * Reacts when new code has arrived correctly.
+   *
+   * @param VersioncontrolEvent $event
+   *   The event describing the code arrival.
+   *
+   * @throws VersioncontrolSynchronizationEventProcessorException $event
+   *   Something wrong happened during the processing. It let caller knows if
+   *   was executed correctly or not.
+   */
+  public function process(VersioncontrolEvent $event);
+}
+
+/**
+ * Defines a way for plugins to define configuration.
+ *
+ * This relies on the plugin itself having on its definition array set to TRUE
+ * the 'has_configuration_form' key.
+ *
+ * @todo Currently it is only used on versioncontrol synchronization event
+ * processor plugin. It's pending to be used on other plugins.
+ */
+interface VersioncontrolPluginConfigurationInterface {
+  /**
+   * Build a configuration form to be used to configure a plugin instance.
+   *
+   * @param array $form
+   *   A form array.
+   * @param array $form_state
+   *   the corresponding form state.
+   */
+  public function buildForm(&$form, &$form_state);
+
+  /**
+   * Validate the form built at buildForm().
+   *
+   * @param array $form
+   *   A form array.
+   * @param array $form_state
+   *   the corresponding form state.
+   */
+  public function validateForm(&$form, &$form_state);
+
+  /**
+   * Handle any special handling on form submission.
+   *
+   * @param array $form
+   *   A form array.
+   * @param array $form_state
+   *   the corresponding form state.
+   */
+  public function submitForm(&$form, &$form_state);
+}
diff --git a/includes/plugins/event_processor/VersioncontrolEventProcessorNone.inc b/includes/plugins/event_processor/VersioncontrolEventProcessorNone.inc
new file mode 100644
index 0000000..8c2e4ec
--- /dev/null
+++ b/includes/plugins/event_processor/VersioncontrolEventProcessorNone.inc
@@ -0,0 +1,45 @@
+<?php
+/**
+ * @file
+ * Basic event processor class doing nothing.
+ */
+
+/**
+ * An example event processor class.
+ */
+class VersioncontrolEventProcessorNone implements VersioncontrolSynchronizationEventProcessorInterface, VersioncontrolPluginConfigurationInterface {
+  const BEHAVIOR_A = 1;
+  const BEHAVIOR_B = 2;
+
+  /**
+   * This plugin is an example that does nothing.
+   */
+  public function process(VersioncontrolEvent $event) {
+  }
+
+  public function buildForm(&$form, &$form_state) {
+    $form['behavior'] = array(
+      '#type' => 'select',
+      '#title' => 'Behavior',
+      '#options' => array(
+        self::BEHAVIOR_A => t('Behavior A'),
+        self:: BEHAVIOR_B => t('Behavior B'),
+      ),
+      '#description' => t('An option that will not change any behavior.'),
+    );
+    $form['message'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Message',
+      '#description' => t('One message that will not be displayed.'),
+      '#required' => TRUE,
+    );
+  }
+
+  public function validateForm(&$form, &$form_state) {
+    // Nothing special.
+  }
+
+  public function submitForm(&$form, &$form_state) {
+    // Nothing special.
+  }
+}
diff --git a/includes/plugins/event_processor/none.inc b/includes/plugins/event_processor/none.inc
new file mode 100644
index 0000000..c2eecf4
--- /dev/null
+++ b/includes/plugins/event_processor/none.inc
@@ -0,0 +1,25 @@
+<?php
+/**
+ * @file
+ * This plugin contains the main documentation of how to make an event
+ * processor plugin.
+ * It does not really provide any functionallity.
+ */
+
+$plugin = array(
+
+  // The versioncontrol system machine name as the backend declares.
+  'vcs' => 'test',
+
+  // This title is going to be shown on the repository edition, for the
+  // user to identify the plugin.
+  'title' => t('Empty event processor'),
+
+  'handler' => array(
+    'class' => 'VersioncontrolEventProcessorNone',
+    'file' => 'VersioncontrolEventProcessorNone.inc',
+    'path' => drupal_get_path('module', 'versioncontrol') . '/includes/plugins/event_processor',
+  ),
+
+  'has_configuration_form' => TRUE,
+);
diff --git a/versioncontrol.admin.inc b/versioncontrol.admin.inc
index 09abc2f..0413138 100644
--- a/versioncontrol.admin.inc
+++ b/versioncontrol.admin.inc
@@ -308,6 +308,7 @@ function versioncontrol_admin_settings_plugins($form, &$form_state) {
         $form['versioncontrol_plugins_' . $vcs][$plugin_slot][$variable_name] = array(
           '#type' => 'select',
           '#title' => t('Plugin name'),
+          '#multiple' => $plugin_slot_data['multiple'],
           '#default_value' => $default_value,
           '#options' => function_exists($callback) ? $options + $callback() : $options,
         );
@@ -319,7 +320,7 @@ function versioncontrol_admin_settings_plugins($form, &$form_state) {
     }
   }
 
-  // Add special configuration for webviewer_url_handler plugin.
+  // Add special configuration for webviewer_url_handler an event_processor plugins.
   foreach ($backends as $vcs => $backend) {
     foreach (ctools_get_plugins('versioncontrol', 'webviewer_url_handlers') as $machine_name => $plugin) {
       $variable = 'versioncontrol_repository_' . $vcs . '_base_url_' . $machine_name;
@@ -329,6 +330,16 @@ function versioncontrol_admin_settings_plugins($form, &$form_state) {
         '#default_value' => variable_get($variable, ''),
       );
     }
+    // @fixme Actually do what is needed.
+    foreach (ctools_get_plugins('versioncontrol', 'event_processor') as $machine_name => $plugin) {
+      dpm($plugin);
+      //$variable = 'versioncontrol_repository_' . $vcs . '_base_url_' . $machine_name;
+      //$form['versioncontrol_plugins_' . $vcs]['webviewer_url_handler'][$variable] = array(
+        //'#type' => 'textfield',
+        //'#title' => t('Default base URL for %plugin_name', array('%plugin_name' => $plugin['title'])),
+        //'#default_value' => variable_get($variable, ''),
+      //);
+    }
   }
 
   $form['submit'] = array(
diff --git a/versioncontrol.module b/versioncontrol.module
index 8fe9833..d595d69 100644
--- a/versioncontrol.module
+++ b/versioncontrol.module
@@ -564,6 +564,39 @@ function versioncontrol_plugin_get_names($plugin_type) {
 }
 
 /**
+ * Load the names of all required plugins with the specified vcs.
+ *
+ * To be used at forms.
+ *
+ * @todo: Let all plugins to define an optional vcs, so this can live inside
+ *        versioncontrol_plugin_get_names().
+ *
+ * @param string $plugin_type
+ *   This can be: webviewer_url_handlers, event_processor.
+ *
+ * @return array
+ *   The list of plugin titles indexed by plugin name.
+ */
+function versioncontrol_plugin_get_names_by_vcs($plugin_type, $vcs='') {
+  ctools_include('plugins');
+
+  $names = array();
+  foreach (ctools_get_plugins('versioncontrol', $plugin_type) as $name => $plugin) {
+    if (!empty($vcs)) {
+      if ($plugin['vcs'] == $vcs) {
+        $names[$name] = $plugin['title'];
+      }
+    }
+    else {
+      $names[$name] = $plugin['title'];
+    }
+  }
+
+  asort($names);
+  return $names;
+}
+
+/**
  * Load the names of all 'user_mapping_methods' for use at forms.
  */
 function versioncontrol_user_mapping_methods_get_names() {
@@ -593,27 +626,16 @@ function versioncontrol_repomgr_get_names() {
 
 /**
  * Load the names of all 'webviewer_url_handlers' for use at forms.
- *
- * @todo: Let all plugins to define an optional vcs, so this can live inside
- *        versioncontrol_plugin_get_names().
  */
-function versioncontrol_webviewer_url_handlers_get_names($vcs='') {
-  ctools_include('plugins');
-
-  $names = array();
-  foreach (ctools_get_plugins('versioncontrol', 'webviewer_url_handlers') as $name => $plugin) {
-    if (!empty($vcs)) {
-      if ($plugin['vcs'] == $vcs) {
-        $names[$name] = $plugin['title'];
-      }
-    }
-    else {
-      $names[$name] = $plugin['title'];
-    }
-  }
+function versioncontrol_webviewer_url_handlers_get_names($vcs = '') {
+  return versioncontrol_plugin_get_names_by_vcs('webviewer_url_handlers', $vcs);
+}
 
-  asort($names);
-  return $names;
+/**
+ * Load the names of all 'event_processor' plugins to be used at forms.
+ */
+function versioncontrol_event_processor_get_names($vcs = '') {
+  return versioncontrol_plugin_get_names_by_vcs('event_processor', $vcs);
 }
 
 /**
@@ -648,7 +670,7 @@ function versioncontrol_webviewer_url_handlers_get_default_plugin($vcs) {
 }
 
 /**
- * Helper function for handlin plugin settings.
+ * Helper function for handling plugin settings.
  */
 function versioncontrol_plugins_get_information() {
   return array(
@@ -656,26 +678,37 @@ function versioncontrol_plugins_get_information() {
       'author_mapper' => array(
         'name' => t('Repository author mapping'),
         'fetcher' => 'versioncontrol_user_mapping_methods_get_names',
+        'multiple' => FALSE,
       ),
       'committer_mapper'   => array(
         'name' => t('Repository committer mapping'),
         'fetcher' => 'versioncontrol_user_mapping_methods_get_names',
+        'multiple' => FALSE,
       ),
       'auth_handler' => array(
         'name' => t('Repository versioncontrol authentication'),
         'fetcher' => 'versioncontrol_auth_handlers_get_names',
+        'multiple' => FALSE,
       ),
       'webviewer_url_handler' => array(
         'name' => t('Repository webviewer URL handler'),
         'fetcher' => 'versioncontrol_webviewer_url_handlers_get_names',
+        'multiple' => FALSE,
       ),
       'repomgr' => array(
         'name' => t('Repository manager'),
         'fetcher' => 'versioncontrol_repomgr_get_names',
+        'multiple' => FALSE,
       ),
       'reposync' => array(
         'name' => t('Repository syncronization'),
         'fetcher' => 'versioncontrol_reposync_get_names',
+        'multiple' => FALSE,
+      ),
+      'event_processor' => array(
+        'name' => t('Event processor'),
+        'fetcher' => 'versioncontrol_event_processor_get_names',
+        'multiple' => TRUE,
       ),
     ),
     'view' => array(
-- 
1.7.10.4

