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

Class naming convention #124

Closed
jurgenhaas opened this issue Feb 15, 2024 · 6 comments
Closed

Class naming convention #124

jurgenhaas opened this issue Feb 15, 2024 · 6 comments

Comments

@jurgenhaas
Copy link

We're building code generators for some of our Drupal modules (e.g. ECA, DRD and others) and we just found out, that the class name needs to follow the *Generator name pattern.

Is that really necessary, or could that requirement be removed?

I mean, the classes are properly namespaced and come with all the PHP attributes to declare and make them discoverable. Enforcing the generator being part of the class name feels redundant, doesn't it?

@Chi-teck
Copy link
Owner

DCG itself does not force Generator suffix. It might come from Drush generator discovery.

@jurgenhaas
Copy link
Author

OK, I'll post a link to this issue in the Drush Slack channel to see if one of the experts over there can help. Thanks @Chi-teck

@jurgenhaas
Copy link
Author

Sounds like has been introduced by drush due to discoverability. However, if we could use PHP to inspect for attributes on classes that have not been loaded, if they are autoloadable, then that could be removed.

Do you know if that's possible somehow?

@Chi-teck
Copy link
Owner

Chi-teck commented Feb 15, 2024

Yo'll have to implement own command discovery mechanism. That could be done through GeneratorInfo event.

/**
 * {@selfdoc}
 */
final class GeneratorInfoSubscriber implements EventSubscriberInterface {

  /**
   * {@selfdoc}
   */
  public function onGeneratorDiscovery(GeneratorInfo $event): void {
    // @todo Implement generator auto-discovery here.
    $event->addGenerators(
      // Here we are registering just one command with known class name.
      // Use Drupal class loader service for dependency injection.
      new Example(),
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [GeneratorInfo::class => 'onGeneratorDiscovery'];
  }

}

For auto discovery you may use reflection like Drush 12 does or tagged services like Drush 11 did.

@jurgenhaas
Copy link
Author

Yeah, it sounds like the drush maintainers don't seem to be keen to go for that, see the thread in slack where you also commented.

@Chi-teck
Copy link
Owner

Closing as there is nothing to do here.

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

No branches or pull requests

2 participants