You are here

function mailsystem_create_class in Mail System 8.2

Same name and namespace in other branches
  1. 6.2 mailsystem.module \mailsystem_create_class()
  2. 7.2 mailsystem.module \mailsystem_create_class()

Creates and registers a new MailSystemInterface class.

The newly-created class gets its name and each of its class methods from the other classes specified by the $class parameter.

Parameters

$class An associative array of ($method_name => $class_name) tuples,: where each $method_name is the name of a class method to be created, and each $class_name is the name of a class to use for that method.

Return value

The name of the newly-created class if successful; otherwise FALSE.

2 calls to mailsystem_create_class()
mailsystem_admin_settings_submit in ./mailsystem.admin.inc
Processes mailsystem_admin_settings form.
mailsystem_set in ./mailsystem.module
Helps other modules safely set their own key within mail_system. This function should be called from hook_enable() implementations.

File

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

Code

function mailsystem_create_class($classes) {

  // Merge in defaults.
  $classes += mailsystem_default_methods();
  ksort($classes);

  // Do not create a new class whose methods all derive from the same class.
  if (count(array_unique($classes)) === 1) {
    return FALSE;
  }
  $class_name = implode('__', $classes);

  // Ensure that the mailsystem directory exists.
  $class_dir = file_build_uri('mailsystem');
  if (!file_prepare_directory($class_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    return FALSE;
  }

  // Build the class filename.
  $class_file = drupal_realpath($class_dir) . DIRECTORY_SEPARATOR . "{$class_name}.mail.inc";

  // Strip DRUPAL_ROOT.
  $drupal_root = drupal_realpath(DRUPAL_ROOT) . DIRECTORY_SEPARATOR;
  $class_file = preg_replace('#^' . preg_quote($drupal_root, '#') . '#', '', $class_file);

  // Build the class implementation as a string.
  $class_contents = '<?php
class ' . $class_name . ' implements MailSystemInterface {';

  // Create a protected variable to hold each method class.
  foreach (array_keys($classes) as $method) {
    $class_contents .= '
  protected $' . $method . 'Class;';
  }

  // Create a class construction function to populate the variables.
  $class_contents .= '
  public function __construct() {';
  foreach ($classes as $method => $class) {
    $class_contents .= '
    if (drupal_autoload_class(\'' . $class . '\')) {
      $this->' . $method . 'Class = new ' . $class . ';
    }
    else {
      $this->' . $method . 'Class = new ' . mailsystem_default_value() . ';
    }';
  }
  $class_contents .= '
  }';

  // Create each class method.
  foreach (array_keys($classes) as $method) {
    $class_contents .= '
  public function ' . $method . '(array $message) {
    return $this->' . $method . 'Class->' . $method . '($message);
  }';
  }
  $class_contents .= '
}
';
  if (file_unmanaged_save_data($class_contents, $class_file, FILE_EXISTS_REPLACE)) {

    // Remove any conflicting registry entries to avoid a database error.
    $class_condition = db_and()
      ->condition('name', $class_name)
      ->condition('type', 'class');
    $file_condition = db_and()
      ->condition('filename', $class_file);
    db_delete('registry_file')
      ->condition($file_condition);
    db_delete('registry')
      ->condition(db_or()
      ->condition($class_condition)
      ->condition($file_condition));

    // Make sure that registry functions are available.
    require_once 'includes/registry.inc';

    // Parse the newly-created class file and add it to the registry.
    _registry_parse_file($class_file, $class_contents, 'mailsystem');

    // Clear the mailsystem cache so that it will pick up the new class.
    drupal_static_reset('mailsystem_get_classes');
    drupal_set_message(t('Class <code>%class</code> written to <code>%file</code>.', array(
      '%class' => $class_name,
      '%file' => $class_file,
    )));
  }
  return $class_name;
}