You are here

function mailsystem_get_classes in Mail System 7.2

Same name and namespace in other branches
  1. 8 mailsystem.admin.inc \mailsystem_get_classes()
  2. 8.2 mailsystem.module \mailsystem_get_classes()
  3. 6.2 mailsystem.module \mailsystem_get_classes()
  4. 6 mailsystem.admin.inc \mailsystem_get_classes()
  5. 7.3 mailsystem.module \mailsystem_get_classes()
  6. 7 mailsystem.admin.inc \mailsystem_get_classes()

Returns a list of classes which implement MailSystemInterface.

2 calls to mailsystem_get_classes()
mailsystem_admin_settings in ./mailsystem.admin.inc
@file Administrative form for setting the mail_system variable.
mailsystem_init in ./mailsystem.module
Implements hook_init().
1 string reference to 'mailsystem_get_classes'
mailsystem_create_class in ./mailsystem.module
Creates and registers a new MailSystemInterface class.

File

./mailsystem.module, line 269
Provide UI for controlling the mail_system variable.

Code

function &mailsystem_get_classes() {

  // Load static cache.
  $mailsystem_classes =& drupal_static(__FUNCTION__);

  // Check persistent cache if necessary.
  if (!isset($mailsystem_classes) && ($cache = cache_get('mailsystem_get_classes'))) {
    $mailsystem_classes = $cache->data;
  }

  // Load from db if no cache was hit.
  if (!isset($mailsystem_classes)) {
    $mailsystem_classes = array();

    // @todo Is there a better way to find all mail-related classes?
    $declared_classes = get_declared_classes();
    $all_classes = array_combine($declared_classes, array_fill(0, count($declared_classes), 0));
    $mail_classes = db_select('registry', 'registry')
      ->distinct()
      ->fields('registry', array(
      'name',
      'filename',
    ))
      ->where("type=:type AND ( filename like :filename OR name like :name )", array(
      ':type' => 'class',
      ':name' => '%MailSystem',
      ':filename' => '%.mail.%',
    ))
      ->execute()
      ->fetchAllKeyed();
    foreach ($mail_classes as $classname => $classfile) {
      if (file_exists($classfile) && drupal_autoload_class($classname)) {
        $all_classes[$classname] = 1;
      }
    }
    foreach ($all_classes as $classname => $autoload) {
      if (($autoload || preg_match('/MailSystem/', $classname)) && ($object = new $classname()) && $object instanceof MailSystemInterface) {
        $mailsystem_classes[$classname] = $classname;
      }
      elseif ($autoload) {

        // Clear classes that are no longer available.
        db_delete('registry')
          ->condition('name', $classname)
          ->execute();
      }
    }
    foreach (array_unique(mailsystem_get()) as $classname) {
      if (class_exists($classname)) {
        $mailsystem_classes[$classname] = $classname;
      }
      else {
        mailsystem_clear(array(
          mailsystem_default_id() => $classname,
        ));
      }
    }
    ksort($mailsystem_classes);

    // Store in persistent cache.
    cache_set('mailsystem_get_classes', $mailsystem_classes, 'cache', CACHE_TEMPORARY);
  }
  return $mailsystem_classes;
}