function mailsystem_create_class in Mail System 6.2
Same name and namespace in other branches
- 8.2 mailsystem.module \mailsystem_create_class()
- 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 114 - 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_directory_path() . '/mailsystem';
if (!file_check_directory($class_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
return FALSE;
}
// Build the class filename.
$class_file = realpath($class_dir) . DIRECTORY_SEPARATOR . "{$class_name}.mail.inc";
// Strip DRUPAL_ROOT.
$drupal_root = realpath('') . 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 (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_save_data($class_contents, $class_file, FILE_EXISTS_REPLACE)) {
// Remove any conflicting registry entries to avoid a database error.
db_query('DELETE FROM {autoload_registry_file} WHERE filename = "%s"', $class_file);
db_query('DELETE FROM {autoload_registry} WHERE filename = "%s" OR (name = "%s" and type = "class")', $class_file, $class_name);
// Make sure that registry functions are available.
module_load_include('inc', 'autoload', 'autoload.registry');
// Parse the newly-created class file and add it to the registry.
_autoload_registry_parse_file($class_file, $class_contents, 'mailsystem');
// Clear the mailsystem cache so that it will pick up the new class.
mailsystem_get_classes(TRUE);
drupal_set_message(t('Class <code>%class</code> written to <code>%file</code>.', array(
'%class' => $class_name,
'%file' => $class_file,
)));
}
return $class_name;
}