You are here

function coder_upgrade_add_file_entries in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/end.inc \coder_upgrade_add_file_entries()

Adds file entries to .info files.

Module .info files must now specify all loadable code files explicitly. http://drupal.org/node/224333#registry

Parameters

string $dirname: Module directory path.

array $files: Contents of directory $dirname.

array $items: List of registry files.

string $path: Subdirectory whose parent directory contains a module.

1 call to coder_upgrade_add_file_entries()
coder_upgrade_upgrade_end_alter in coder_upgrade/conversions/end.inc
Implements hook_upgrade_end_alter().

File

coder_upgrade/conversions/end.inc, line 90
Provides conversion routines applied to the directory after routines are applied to the files.

Code

function coder_upgrade_add_file_entries($dirname = '', $files = array(), &$items = array(), $path = '') {
  if (empty($files)) {
    return;
  }
  global $_coder_upgrade_dirname, $_coder_upgrade_class_files;

  // Extensions that indicate a module is present.
  $extensions = array(
    'info',
    'module',
  );
  $info_file = '';
  if ($path != 'new' && ($path != '' || $dirname != $_coder_upgrade_dirname)) {

    // Scan subdirectory for .info file indicating presence of another module.
    foreach ($files as $file) {
      if (!is_array($file)) {
        $extension = pathinfo($file, PATHINFO_EXTENSION);
        if (in_array($extension, $extensions)) {

          // This subdirectory contains a module - so recurse.
          $dirname .= $path != '' ? '/' . $path : '';
          $path = 'new';
          $items2 = array();
          coder_upgrade_add_file_entries($dirname, $files, $items2, $path);
          return;
        }
      }
    }
  }

  // Clear path if we are starting another module.
  $path = $path == 'new' ? '' : $path;

  // Create list of files for registry.
  // TODO Allow for custom extensions!!!
  $extensions = array(
    'inc',
    'install',
    'module',
    'php',
    'profile',
    'test',
    'theme',
    'upgrade',
  );
  $dirname .= $path != '' ? '/' . $path : '';
  $path = $path != '' ? $path . '/' : $path;
  foreach ($files as $file) {
    if (is_array($file)) {
      coder_upgrade_add_file_entries($dirname, $file['entries'], $items, $file['dir']);
    }
    else {
      $extension = pathinfo($file, PATHINFO_EXTENSION);
      if ($extension == 'php' && substr($file, -8) == '.tpl.php') {

        // Exclude template files.
      }
      elseif (in_array($extension, $extensions) && in_array($dirname . '/' . $file, $_coder_upgrade_class_files)) {
        $items[] = $dirname . '/' . $file;
      }
      elseif ($extension == 'info') {

        // Save the name of the .info file.
        $info_file = $dirname . '/' . $file;
      }
    }
  }
  if ($info_file != '') {
    $new = file_get_contents($info_file);

    // Remove any file entries from .info file.
    $from = "/^\\s*files\\[\\]\\s*=.*?\$/m";
    $to = '';
    $new = preg_replace($from, $to, $new);

    // Add sorted entries to .info file.
    $new .= "\n";
    foreach ($items as $item) {
      $item = str_replace($dirname . '/', '', $item);
      $new .= "files[] = {$item}\n";
    }
    if (file_put_contents($info_file, $new) === FALSE) {
      clp(t('File could not be written: @file', array(
        '@file' => $info_file,
      )));
    }
  }
}