You are here

function module_builder_generate_module in Module Builder 7

Same name and namespace in other branches
  1. 6.2 includes/generate.inc \module_builder_generate_module()

Generate module code.

Parameters

$module_data: An associative array of data for the module, passed by reference so data on generated files can be added. The keys can *mostly* be taken straight from form values. They are as follows:

  • 'module_root_name'
  • 'module_readable_name'
  • 'module_short_description'
  • 'module_help_text'
  • 'hooks': An associative array whose keys are full hook names (eg 'hook_menu'), where requested hooks have a value of TRUE. Unwanted hooks may also be included as keys provided their value is FALSE.
  • 'module_dependencies': a string of dependencies, eg 'forum views'.
  • 'module_package': the module package.
  • 'module_files': added by this function. A flat array of filenames that have been generated.

$bare: If true, omit header and footers and output only hook code.

Return value

An array of code, indexed by filename. Eg, 'modulename.module' => CODE

3 calls to module_builder_generate_module()
module_builder_callback_module in drush/module_builder.drush.inc
Handles the callback for module code generation.
module_builder_page_generate in includes/module_builder.pages.inc
page 2: generate code
Xmodule_builder_page_module in ./module_builder.module
Module form: 'module' step. Generate the module code.

File

includes/generate.inc, line 34
Module builder code generating code.

Code

function module_builder_generate_module(&$module_data, $bare = FALSE) {

  // Get a set of hook declarations and function body templates for the hooks we want.
  // $hook_data is of the form:
  //   'hook_foo' => array( 'declaration' => DATA, 'template' => DATA )
  $hook_file_data = module_builder_get_templates($module_data);
  if (is_null($hook_file_data)) {
    return NULL;
  }

  //print_r($module_data);

  //dsm($hook_file_data);

  // Iterate over our data array, because it's in a pretty order.
  // by each needed file of code.
  foreach ($hook_file_data as $filename => $hook_data) {

    // Begin code generation for this file.
    $code = '';
    foreach ($hook_data as $hook_name => $hook) {

      // Display PHP doc.
      $code .= "\n" . module_builder_code_hook_doxy($hook_name);

      // function declaration: put in the module name, add closing brace, decode html entities
      $declaration = str_replace('hook', $module_data['module_root_name'], $hook['declaration']) . ' {';
      $code .= htmlspecialchars_decode($declaration);

      // See if function bodies exist; if so, use function bodies from template
      if ($hook['template']) {

        // Strip out INFO: comments for advanced users
        if (!variable_get('module_builder_detail', 0)) {

          // Used to strip INFO messages out of generated file for advanced users.
          $pattern = '#\\s+/\\* INFO:(.*?)\\*/#ms';
          $hook['template'] = preg_replace($pattern, '', $hook['template']);
        }

        //dsm($hook);
        $code .= $hook['template'];
      }
      else {
        $code .= "\n\n";
      }
      $code .= "}\n\n";
    }

    // foreach hook
    // Replace variables
    $variables = array(
      '%module' => $module_data['module_root_name'],
      '%description' => str_replace("'", "\\'", $module_data['module_short_description']),
      '%name' => !empty($module_data['module_readable_name']) ? str_replace("'", "\\'", $module_data['module_readable_name']) : $module_data['module_root_name'],
      '%help' => !empty($module_data['module_help_text']) ? str_replace("'", "\\'", $module_data['module_help_text']) : t('TODO: Create admin help text.'),
      '%readable' => str_replace("'", "\\'", $module_data['module_readable_name']),
    );
    $code = strtr($code, $variables);

    // Replace full-blown Id tag with just starter
    // (excuse the weird concatenation stuff; CVS hijacks it otherwise :))

    //$code = preg_replace(MODULE_BUILDER_FULL_ID_PATTERN, MODULE_BUILDER_ID_COMMENT, $code);

    // what is this for?
    // Prepare final code
    // Header and footer
    if (!$bare) {
      $module_data['header'] = "<?php\n" . module_builder_code_header($filename);
      $module_data['footer'] = variable_get('module_builder_footer', '');
    }
    $code = $module_data['header'] . $code . $module_data['footer'];

    //dsm($code);

    //print $code;
    $return[$filename] = $code;

    // Add the generated filename to the module data for the info generation to find.
    $module_data['module_files'][] = $filename;
  }

  // foreach file

  //print_r($module_data);
  return $return;
}