You are here

function module_builder_callback_doc_hooks in Module Builder 6.2

Same name and namespace in other branches
  1. 7 drush/module_builder.drush.inc \module_builder_callback_doc_hooks()

Callback to add doc headers to existing hooks.

1 string reference to 'module_builder_callback_doc_hooks'
module_builder_drush_command in drush/module_builder.drush.inc
Implementation of hook_drush_command().

File

drush/module_builder.drush.inc, line 629
Module builder drush commands.

Code

function module_builder_callback_doc_hooks() {
  $commands = func_get_args();

  // The first argument is the module machine name.
  $module_root_name = array_shift($commands);
  $module_files = module_builder_get_module_files($module_root_name);

  // Include component files.
  module_builder_include('process');
  module_builder_include('generate');
  $hook_names = module_builder_get_hook_names('short');
  $pattern = '[(?<! \\* / \\n )' . "function \\ image_gallery _ ( \\w * )  # function declaration: capture hook name\n     ]mx";
  foreach ($module_files as $filename) {
    $code = file_get_contents($filepath . '/' . $filename);

    //print $code;

    // Get functions that have no docs.
    preg_match_all($pattern, $code, $function_names);

    // Get only those that are actual hooks.
    $bad_hooks = array_intersect($function_names[1], $hook_names);

    // For each hook that has no documentation.
    foreach ($bad_hooks as $hook_name) {
      $doc = module_builder_generate_hook_doxy("hook_{$hook_name}");
      $pattern2 = "[(?= function \\ image_gallery _ {$hook_name} )]x";
      $code = preg_replace($pattern2, $doc, $code);
    }
    if (!drush_get_option('quiet')) {
      print $code;
    }
    print 'Added hook documentation headers for: ' . implode(', ', $bad_hooks) . "\n";
    if (!drush_confirm(dt('Are you sure you want to overwrite ' . $filename . '?'))) {
      continue;
    }
    file_put_contents($filepath . '/' . $filename, $code);
  }
}