Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Plugins - Allow CiviCRM extensions to define plugins/commands for cv #219

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

totten
Copy link
Member

@totten totten commented Sep 24, 2024

(draft / description TODO - just checking Jenkins for now - depends on #218)

@totten
Copy link
Member Author

totten commented Sep 24, 2024

Some open todos:

  • Add special variant of ListCommand (the generic screen which lists available subcommands) -- we need to boot before generating the list, and it should be guarded to ensure that built-in/global commands are still offered (even if there's no bootable environment*)
  • Figure out some test-coverage
  • Write docs+description

@totten totten force-pushed the master-plugin-2x branch 2 times, most recently from 117c0f6 to 19b2bc8 Compare September 24, 2024 18:47
@totten
Copy link
Member Author

totten commented Sep 24, 2024

A lot of the base-command shuffling has been merged, so this is smaller now. Still needs work. But I also need to switch over to other things for a while.

Just noting - for my local testing of this, I would put this bit of glue-code inside an extension main-file:

diff --git a/info.xml b/info.xml
index 946e0c0..fb27a5e 100644
--- a/info.xml
+++ b/info.xml
@@ -34,6 +34,7 @@
   <classloader>
     <psr0 prefix="CRM_" path=""/>
     <psr4 prefix="Civi\" path="Civi"/>
+    <psr4 prefix="CvPlugin\" path="CvPlugin"/>
   </classloader>
   <mixins>
     <mixin>ang-php@1.0.0</mixin>
diff --git a/mosaico.php b/mosaico.php
index 9a2e8ed..ce3a0b4 100644
--- a/mosaico.php
+++ b/mosaico.php
@@ -3,6 +3,26 @@
 require_once 'mosaico.civix.php';
 use CRM_Mosaico_ExtensionUtil as E;

+Civi::dispatcher()->addListener('&civi.cv-lib.plugins', function (array &$plugins, array $pluginEnv) {
+  if ($pluginEnv['protocol'] !== 1) {
+    return;
+  }
+
+  // Load a single plugin file, eg "myextension.cv-plugin.php"
+  $f = __DIR__ . '/' . E::SHORT_NAME . '.' . $pluginEnv['appName'] . '-plugin.php';
+  if (file_exists($f)) {
+    $plugins[E::SHORT_NAME] = $f;
+  }
+
+  // Load a folder `./CvPlugin` with various classes
+  $appNs = CRM_Utils_String::convertStringToCamel($pluginEnv['appName']) . 'Plugin';
+  $folder = __DIR__ . '/' . $appNs;
+  if (file_exists($folder)) {
+    \Civi\Cv\Cv::app()->add(new \CvPlugin\Command\HelloCommand());
+    // FIXME: Scan $folder and instantiate commands. Maybe similar to ClassScanner::scanFolders()
+  }
+});
+
 /**
  * Implements hook_civicrm_config().
  *

With a little more polishing, that could become a suitable basis for implementing civix generate:cli-command (e.g. using a mixin and using separate class-files for item).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant