Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add command generate:service #319

Merged
merged 9 commits into from
Dec 4, 2023
Merged

Add command generate:service #319

merged 9 commits into from
Dec 4, 2023

Conversation

totten
Copy link
Owner

@totten totten commented Dec 3, 2023

Add a command to generate "services" in the "service container". The code-style is based on automatic registration in v5.55+. By default, it implements EventSubscriberInterface (since this is common).

Usage

## Generate service (without specifying a name)
civix generate:service

## Generate service (with specific name)
civix generate:service myextension.foo

Note: If you run civix interactively, it will prompt to confirm/revise the name of the service and PHP class.

Output

On a demo extension (named civix_addsvc), here's the console output:

Screenshot 2023-12-03 at 6 47 17 PM

Which generates a file like:

<?php
use CRM_CivixAddsvc_ExtensionUtil as E;
use Civi\Core\Service\AutoService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * @service civix_addsvc.myService
 */
class CRM_CivixAddsvc_MyService extends AutoService implements EventSubscriberInterface {

  public static function getSubscribedEvents(): array {
    return [
      // '&hook_civicrm_alterContent' => ['onAlterContent', 0],
      // '&hook_civicrm_postCommit::Contribution' => ['onContribute', 0],
      // TIP: For hooks based on GenericHookEvent, the "&" will expand arguments.
    ];
  }

  // /**
  //  * @see \CRM_Utils_Hook::alterContent
  //  */
  // public function onAlterContent(&$content, $context, $tplName, &$object) { ... }

  // /**
  //  * @see \CRM_Utils_Hook::postCommit
  //  */
  // public function onContribute($op, $objectName, $objectId, $objectRef = NULL) { ... }

}

Comment

This generator supports the Civi\ namespace, but (for existing extensions) the CRM_ namespace is the default. Note that:

  • The default namespace comes from info.xml
    <civix>
       <namespace>CRM/Myextension</namespace>
    </civix>
  • The command prompts interactive users, giving them the chance to rename the class.
  • For non-interactive usage, the command accepts option --naming=Civi or --naming=CRM.

@totten
Copy link
Owner Author

totten commented Dec 4, 2023

Test failure unrelated.

@totten totten merged commit 62007c1 into master Dec 4, 2023
@totten totten deleted the gen-svc branch December 4, 2023 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant